UnitOfWork pattern with Depedency Injection in asp.net core

asp.net-core c# entity-framework-core

Question

I want to move my project to ASP.NET Core framework. I'm new in asp.net core so don't know how to work with UnitOfWork pattern there. When I worked with asp.net mvc, I divided solution into 3-tier architecture(DAl,BLL,WEB layers). In BLL tier I used Ninject injection, so I didn't need to add reference to DLL from my WEB layer.

public class ServiceModule : NinjectModule
{
    private string connectionString;
    public StandardKernel _kernel;
    IUnitOfWork uow;
    public ServiceModule(string connection)
    {
        connectionString = connection;
        uow = new EFUnitOfWork(connectionString);
    }
    public override void Load()
    {
        Bind<IUnitOfWork>().To<EFUnitOfWork>().WithConstructorArgument(connectionString);
    }
}

But all tutorials say in ASP.NET Core applications should use core injection in ConfigureServices(WEB layer) method like that:

services.AddTransient<IUnitOfWork, EFUnitOfWork>();

However, it's bad idea to add reference to add a reference to DAL layer from WEB.

So, what should I do?Add reference(don't think so) or edit my structure?Or maybe something else?

1
1
3/21/2018 2:31:21 PM

Popular Answer

You can use an extension method and put it into your BLL Library:

namespace Microsoft.Extensions.DependencyInjection
{
    public static class BllServiceCollectionExtensions
    {
        public static IServiceCollection AddBll(this IServiceCollection services)
        { 

            services.AddTransient<IUserService, UserService>();
            services.AddTransient<ITransactionService, TransactionService>();
        }
    }
}

in you web Project you can add it to your Startup.cs like this

public void ConfigureServices(IServiceCollection services)
{
    ...
    serivices.AddBll();
    ...
}
1
3/21/2018 4:07:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow