dotnet-architecture / eBooks

.NET Architecture e-Books
http://dot.net/architecture
1.08k stars 245 forks source link

[Book Feedback] #106

Open dlevitan6623 opened 3 years ago

dlevitan6623 commented 3 years ago

Please provide the title of the book - BLAZOR For ASP.Net Web Forms Developer

What do you think could be improved in this book? The section for working with data could be improved. It devotes most of the section to Entity Framework, but EF is not the only ORM that is out there as well as it is not the only ORM that can be used with BLAZOR. In fact, most ASP.Net applications that would be migrated would not be using EF. Why not demonstrate how an ORM such as Dapper could be used instead?

Also, a section on how to implement unit testing would be nice.

What do you like about this book? What I like about the book, is that it is a single point of looking up how to do this or that in BLAZOR, without going on the Web and reading out of date information.

Additional context As I have already started a BLAZOR project using Dapper and AutoMapper connected to the North Wind database.

Dapper – AutoMapper

Adding Dapper To a Project

  1. Install the software into the Project that will have the Data Services

image

Install the / a Dapper package into the project image

Install System Data SQL Client

  1. Configure The Connection String for the project image

Add the connection string to the “appsettings.json” file. You can get the base connection information from the SQL Server Object Explorer, under the properties for the server. Then modify it for the name of the database.

In this example, we are using integrated security. In a real world application, the ID and Password will need to be stored into something like a key safe. image

In the Startup file, add a public variable to hold the connection string, and the code to read it in from the app settings file.

public string NothWindConnection = string.Empty; NothWindConnection = Configuration.GetConnectionString("NorthWind"); image

In the Startup file you need to configure the DI for not only the Data Base Services, but also to add the connection string to those services. image

For the Database Services, the connection string can be stored in a private variable.

  1. Use Dapper for Reading and Writing to the Database image

In the example above, “Query” is like an extension method of the ADO “Connection” object. In one line, the query is run and the data is returned as data object. image

In this example, a stored procedure is executed and we are using a single parameter.

Adding AutoMapper to the Project

  1. Install the software into the Project that will have the Application Services image

  2. Configure The Environment image

Add AutoMapper as a service to the service collection image

Add a configuration file for all of the mappings image

Here a base class is created, as all application services will be using AutoMapper

image

In the example above, this application service has two services injected. The database service and AutoMapper.

  1. Use AutoMapper for copying the DTO to the application service image

In this simple example, data is passed through without any processing such as filtering or sorting.

erjain commented 1 year ago

Hi @dlevitan6623, thank you so much for sharing your feedback. Due to bandwidth and maintainability, we have kept this item a low priority. But if anyone in the community wants to give this a try, we will be happy to link the sample in the wiki.