Vlad
Vlad

Reputation: 233

UnitOfWork pattern with Depedency Injection in asp.net core

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?

Upvotes: 1

Views: 743

Answers (1)

wheeler
wheeler

Reputation: 687

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();
    ...
}

Upvotes: 1

Related Questions