- Implementation of UnitOfWork pattern
- While using the Repository pattern
- Using Entity Framework Core 2.0
- UoW is responsible for creating repositories
- Not depending on any specific DbContext, you can create your own
You can find it in the official nuget package server by searching for: WaCore.Data.Ef
Follow this step-by-step guide to make use of this module or look at the WaCore.Sample
project in the source code.
For example you create a LibraryDbContext
class, which derives from DbContext
:
[!code-csharpMain]
In your project, which represents the data-layer, add a reference to the module WaCore.Data.Ef
.
You create your Repository class by implementing the interface IWacRepository<TEntity>
. Again, there is already an abstract class WacRepository<TEntity, TDbContext
, which implements that interface and uses EF Core 2.0 under the hood.
[!code-csharpMain]
You create your UnitOfWork
class, by implementing the interface IWacUnitOfWork
. You can actually derive from the abstract class WacEfUnitOfWork
, which already implements that interface using EF Core 2.0 under the hood. If you want to use any other ORM, then you just need to implement aforementioned interface.
[!code-csharpMain]
In the ConfigureServices
method of your Startup
class use the extension method AddUnitOfWork
to register your Unit of Work class and your repositories in the IoC container.
Specify your Unit of Work class in the type parameters of AddUnitOfWork
and add your repositories using the configuration function argument. You can add specific repositories using AddRepository<TRepoInterface, TRepoImplementation>
or all repositories defined in specific assembly using AddRepositoriesFromAssemblyOf<TAssemblySelector>
.
[!code-csharpMain]
You can get a UnitOfWork instance using dependency injection via constructor arguments. For instance, you can get an instance in the constructor and then use it as follows in your controller: [!code-csharpMain]