You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 10, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: docs/2. Build out BackEnd and Refactor.md
+98-94Lines changed: 98 additions & 94 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -16,21 +16,25 @@ In this session, we'll add the rest of our models and controllers that expose th
16
16
### Adding the ConferenceDTO project via the Command Line
17
17
1. Open a command prompt and navigate to the root `ConferencePlanner` directory.
18
18
1. Run the following command:
19
-
```
19
+
```console
20
20
dotnet new classlib -o ConferenceDTO -f netstandard2.0
21
21
```
22
22
1. Next we'll need to add a reference to the ConferenceDTO project from the BackEnd project. From the command line, navigate to the BackEnd project directory and execute the following command:
## Refactoring the Speaker model into the ConferenceDTO project
32
32
1. Copy the `Speaker.cs` class from the *BackEnd* application into the root of the new ConferenceDTO project, and change the namespace to `ConferenceDTO`.
33
-
1. The data annotations references can't be resolved to a missing NuGet package. Add a reference to `System.ComponentModel.Annotations` version `4.5.0`. When the package restore completes, you should see that your data annotations are now resolved.
33
+
1. The data annotations references should be broken at this point, to resovle it, we need to add a nuget the missing NuGet package into the `ConferenceDTO` project.
34
+
1. Add a reference to the NuGet package `System.ComponentModel.Annotations` version `4.5.0`.
35
+
> This can be done from the command line using `dotnet add package System.ComponentModel.Annotations --version 4.5.0`
36
+
37
+
1. When the package restore completes, you should see that your data annotations are now resolved.
34
38
1. Go back to the *BackEnd* application and modify the code in `Speaker.cs` as shown:
35
39
```csharp
36
40
publicclassSpeaker : ConferenceDTO.Speaker
@@ -123,10 +127,11 @@ We've got several more models to add, and unfortunately it's a little mechanical
123
127
```
124
128
125
129
## Creating Derived Models in the BackEnd project
126
-
We're not going to create our EF models directly from the *ConferenceDTO* classes. Instead, we'll create some composite classes such as *SessionSpeaker*, since these will map more closely to what our application will be working with.
130
+
We're not going to create our EF models directly from the `ConferenceDTO` classes. Instead, we'll create some composite classes such as `SessionSpeaker`, since these will map more closely to what our application will be working with.
127
131
128
132
We're also going to take this opportunity to rename the `Models` directory in the *BackEnd* project to `Data` since it no longer just contains models.
129
133
1. Right-click the `Models` directory and select `Rename`, changing the name to `Data`.
134
+
> Note: If you are using Visual Studio, you can use refactoring to rename the namespace.
130
135
1. Add a `SessionSpeaker.cs` class to the `Data` directory with the following code:
131
136
```csharp
132
137
usingSystem;
@@ -148,9 +153,10 @@ We're also going to take this opportunity to rename the `Models` directory in th
148
153
}
149
154
}
150
155
```
151
-
1. Add an `SessionAttendee.cs` class with the following code:
156
+
1. Add an `SessionAttendee.cs` class with the following code:
157
+
152
158
```csharp
153
-
usingSystem;
159
+
usingSystem;
154
160
usingSystem.Collections.Generic;
155
161
usingSystem.Linq;
156
162
usingSystem.Threading.Tasks;
@@ -200,31 +206,30 @@ We're also going to take this opportunity to rename the `Models` directory in th
200
206
}
201
207
}
202
208
```
203
-
1.Modify the `Speaker.cs` class we wrote previously to make the following two changes: update to the namespace to match our directory rename, and add a referece to the `SessionSpeaker` composite class:
1. Add a `Track.cs` class with the following code:
223
+
1. Modify the `Speaker.cs` class we wrote previously to make the following two changes: update to the namespace to match our directory rename, and add a referece to the `SessionSpeaker` composite class:
@@ -277,7 +282,7 @@ Okay, now we need to update our `ApplicationDbContext` so Entity Framework knows
277
282
## Add a new database migration
278
283
279
284
### Visual Studio: Package Manager Console
280
-
1. Run the following commands in the Package Manager Console
285
+
1. Run the following commands in the Package Manager Console (specify the `BackEnd` project)
281
286
```console
282
287
Add-Migration Refactor
283
288
Update-Database
@@ -291,100 +296,99 @@ Okay, now we need to update our `ApplicationDbContext` so Entity Framework knows
291
296
```
292
297
1. Now take a deep breath and run the application and navigate to `/swagger`. You should see the Swagger UI.
293
298
294
-
>Save point for above code changes is [here](/save-points/2a-Refactor-to-ConferenceDTO/ConferencePlanner)
295
-
296
299
## Updating the Speakers API controller
297
300
298
301
1. Modify the query for the `GetSpeakers()` method as shown below:
299
302
```csharp
300
-
varspeakers=await_context.Speakers.AsNoTracking()
301
-
.Include(s=>s.SessionSpeakers)
302
-
.ThenInclude(ss=>ss.Session)
303
-
.ToListAsync();
304
-
returnspeakers;
303
+
varspeakers=await_context.Speakers.AsNoTracking()
304
+
.Include(s=>s.SessionSpeakers)
305
+
.ThenInclude(ss=>ss.Session)
306
+
.ToListAsync();
307
+
returnspeakers;
305
308
```
306
-
1. While the above will work, this is directly returning our model class. A better practice is to return an output model class. Create a `SpeakerResponse.cs` class in the *ConferenceDTO* project with the following code:
309
+
1. While the above will work, this is directly returning our model class. A better practice is to return an output model class. Create a `SpeakerResponse.cs` class in the `ConferenceDTO` project with the following code:
1. Now we'll add a utility method to map between these classes. In the *BackEnd* project, create an `Infrastructure` directory. Add a class named `EntityExtensions.cs` with the following mapping code:
1. Now we can update the `GetSpeakers()` method of the *SpeakersController* so that it returns our response model. Update the last few lines so that the method reads as follows:
1. Remove the other actions (`PutSpeaker`, `PostSpeaker`, `DeleteSpeaker`), on the `SpeakersController`.
384
+
385
+
## Adding the remaining API Controllers and DTOs
381
386
382
-
## Adding the remaining API Controllers
383
-
1. Add the following response DTO classes from [the save point folder](/save-points/2b-BackEnd-completed/ConferencePlanner/ConferenceDTO)
387
+
1. Add the following response DTO classes from [the save point folder](/save-points/2-BackEnd-completed/ConferencePlanner/ConferenceDTO)
384
388
-`AttendeeResponse`
385
389
-`SessionResponse`
386
-
1. Update the `EntityExtensions` class with the extra mapping methods from [the save point folder](/save-points/2b-BackEnd-completed/ConferencePlanner/BackEnd/Infrastructure)
387
-
1. Copy the following controllers from [the save point folder](/save-points/2b-BackEnd-completed/ConferencePlanner/BackEnd/Controllers) into the current project's `BackEnd/Controllers` directory:
390
+
1. Update the `EntityExtensions` class with the extra mapping methods from [the save point folder](/save-points/2-BackEnd-completed/ConferencePlanner/BackEnd/Infrastructure)
391
+
1. Copy the following controllers from [the save point folder](/save-points/2-BackEnd-completed/ConferencePlanner/BackEnd/Controllers) into the current project's `BackEnd/Controllers` directory:
388
392
-`SessionsController`
389
393
-`AttendeesController`
390
394
@@ -466,7 +470,7 @@ Okay, now we need to update our `ApplicationDbContext` so Entity Framework knows
466
470
## Adding Conference Upload support
467
471
1. Copy the `DataLoader.cs` class from [here](/src/BackEnd/Data/DataLoader.cs) into the `Data` directory of the `BackEnd` project.
468
472
1. Copy the `SessionizeLoader.cs` and `DevIntersectionLoader.cs` classes from [here](/src/BackEnd/Data/) into the current project's `/src/BackEnd/Data/` directory.
469
-
> Note: We have data loaders from the two conference series where this workshop has been presented most; you can update this to plug in your own conference file format.
473
+
> Note: We have data loaders from the two conference series where this workshop has been presented most; you can update this to plug in your own conference file format.
470
474
1. To improve the UI for upload, turn on the option to display enums as strings by changing `AddSwaggerGen` in `Startup.cs` to the following:
0 commit comments