Results 1 to 3 of 3

Thread: [.NET 8] Dependency Injection on an N-Tier solution

Threaded View

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,989

    [.NET 8] Dependency Injection on an N-Tier solution

    I am creating a solution that has an API (Microsoft.NET.Sdk.Web), domain (Microsoft.NET.Sdk), and MVC projects (Microsoft.NET.Sdk.Web).

    In my domain I have two classes that look like this:
    Code:
    public class ApplicationUserQuery
    {
    
        private readonly string _connectionString;
    
        public ApplicationUserQuery(string connectionString)
        {
            _connectionString = connectionString;
        }
    
        // ...
    }
    
    public class ApplicationUserService
    {
    
        private readonly ApplicationUserQuery _applicationUserQuery;
    
        public ApplicationUserService(ApplicationUserQuery applicationUserQuery)
        {
            _applicationUserQuery = applicationUserQuery;
        }
    
        // ...
    }
    To use the classes in my API project I am doing the following:
    Code:
    var applicationDatabaseConnectionString = builder.Configuration.GetConnectionString("ApplicationDatabase");
    if (applicationDatabaseConnectionString == null)
    {
        throw new ArgumentNullException(nameof(applicationDatabaseConnectionString));
    }
    builder.Services.AddScoped(_ => new ApplicationUserQuery(applicationDatabaseConnectionString));
    builder.Services.AddScoped<ApplicationUserService>();
    This works, but it seems like a lot of boilerplate just for two classes. Is there anyway to simplify this using the built-in .NET core stuff? I'm wanting to avoid using a 3rd party library like Ninject because of issues we recently started running into at work. This solution I'm building is a personal project.
    Last edited by dday9; Jan 22nd, 2024 at 05:28 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width