DBContext Middleware

Ronald Rex 1,666 Reputation points
2022-07-13T20:06:56.727+00:00

I am having trouble understanding the Program.cs class. I can't find any articles on how everything fits together. For example, why do we enclose in angle brackets the IUser Interface and the class that implements it UserManager likeso <IUser, UserManager>? Where does the builder class fit into all of this? And I also don't understand the options => statement. I know this question is loaded but I am having trouble understanding so that I can apply this to something I can build on my own or maybe someone has an example of where this same syntax is used in injecting a service into a container that is used as Middleware. I don't like using code that I don't fully understand. How does this code ultimately result in exploiting the concept of Dependency Injection.

builder.Services.AddDbContext<DatabaseContext>
(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

builder.Services.AddTransient<IUser, UserManager>();

Developer technologies .NET Entity Framework Core
Developer technologies ASP.NET ASP.NET Core
0 comments No comments
{count} votes

Accepted answer
  1. Bruce Barker 801 Reputation points
    2022-07-14T02:00:02.923+00:00

    You should learn c#. The net 6 makes heavy use if the following c# features

    Interfaces
    generics
    lambda (closure) functions
    Expression trees (used with binding)

    Then there are several design patterns you should know very well

    Interface design pattern
    Dynamic dependency injection pattern (DI)
    The builder pattern

    Once you learn the above it will be simple to read. <> are used with generic interfaces. The builder pattern and DI is key to understanding the program.cs file.


2 additional answers

Sort by: Most helpful
  1. Bruce Barker 801 Reputation points
    2022-07-14T02:15:26.417+00:00

    You should learn c#. The net 6 makes heavy use if the following c# features

    Interfaces
    generics
    lambda (closure) functions
    Expression trees (used with binding)

    Then there are several design patterns you should know very well

    Interface design pattern
    Dynamic dependency injection pattern (DI)
    The builder pattern

    Once you learn the above it will be simple to read. <> are used with generic interfaces. The builder pattern and DI is key to understanding the program.cs file.

    For example

    // register a dbcontext for DI using a generic method to define the actual type  
    // then pass a lambda to initial the DatabaseContext parameters  
    builder.Services.AddDbContext<DatabaseContext>  
    (options =>  
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));  
      
    // register a concrete  type of IUser to be available to be injected  
    // transient says it created on every request  
    // AddTransient is a generic method requiring two type definitions   
    // it’s actually overload so there are several signatures   
    builder.Services.AddTransient<IUser, UserManager>();  
    
    0 comments No comments

  2. Anonymous
    2022-07-14T05:32:58.747+00:00

    Hi @Ronald Rex ,

    You can refer the following tutorials to learn how the use Dependency injection in asp.net core:

    Dependency injection in ASP.NET Core.

    And refer to the Tutorial: Get started with EF Core in an ASP.NET MVC web app or Part 5, work with a database in an ASP.NET Core MVC app to learn how to use the EF core in asp.net core:

    220613-image.png

    Then, to use the UserManager and Asp.net core Identity, you can refer to the following links:

    Introduction to Identity on ASP.NET Core

    Scaffold Identity in ASP.NET Core projects


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards
    Dillion

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.