1

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?

7
  • 2
    Three things: "it's bad idea to add reference to add a reference" is not true, because unless you reference it the DLL is not copied so you cannot deploy your app without manual work or some build event. Second: Unit Of Work and Entity Framework Core is a bad idea, so there's no need for that. Third: that's the .NET Core container, you can use it or not, that's up to you Mar 21, 2018 at 14:31
  • 2
  • @Camilo Terevinto: Can you elaborate on "Second: Unit Of Work and Entity Framework Core is a bad idea, so there's no need for that."?
    – RWC
    Jul 18, 2018 at 11:10
  • @RWC It's pretty broad for a comment, but, basically, Entity Framework is already a UoW and Repository-based library, hiding it behind another UoW makes your code far more complex and doesn't actually give you anything in return. Unless of course you are planning on ditching EF in the future, at which point I wouldn't consider starting with it. Transactions, complex and reusable queries are much more complex when you have to deal with a UoW abstracting the power of Entity Framework Jul 18, 2018 at 12:00
  • @Camilo Terevinto: I understand what you say, but I do not thing it is totally correct. Entity Framework implements the Unit of Work and Repository patterns. With Entity Framework, the DbContext is the Unit of Work and each DbSet is a repository. UoW and EF Core do not exclude eachother. Look at Microsoft own's example: learn.microsoft.com/en-us/aspnet/mvc/overview/older-versions/….
    – RWC
    Jul 20, 2018 at 12:21

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.