Entity Framework 6 Code First - Is Repository Implementation a Good One?

后端 未结 7 1744
醉话见心
醉话见心 2020-11-30 16:09

I am about to implement an Entity Framework 6 design with a repository and unit of work.

There are so many articles around and I\'m not sure what the best advice is:

7条回答
  •  一向
    一向 (楼主)
    2020-11-30 17:08

    Repository with unit of work pattern implementation is a bad one to answer your question.

    The DbContext of the entity framework is implemented by Microsoft according to the unit of work pattern. That means the context.SaveChanges is transactionally saving your changes in one go.

    The DbSet is also an implementation of the Repository pattern. Do not build repositories that you can just do:

    void Add(Customer c)
    {
       _context.Customers.Add(c);
    }
    

    Create a one-liner method for what you can do inside the service anyway ???

    There is no benefit and nobody is changing EF ORM to another ORM nowadays...

    You do not need that freedom...

    Chris Hardie is argumenting that there could be instantiated multiple context objects but already doing this you do it wrong...

    Just use an IOC tool you like and setup the MyContext per Http Request and your are fine.

    Take ninject for example:

    kernel.Bind().To().InRequestScope().WithConstructorArgument("context", c => new ITMSContext());
    

    The service running the business logic gets the context injected.

    Just keep it simple stupid :-)

提交回复
热议问题