How to get DbContext instance in application layer?
I tried:
1.
public class SeedingTestDataAppService : MyAppServiceBase
{
private readonly MyDbContext _ctx;
public SeedingTestDataAppService
(
MyDbContext context // Error
)
{
_ctx = context;
}
}
2.
public class SeedingTestDataAppService : MyAppServiceBase
{
private readonly MyDbContext _ctx;
public SeedingTestDataAppService
(
IDbContextProvider<MyDbContext> dbContextProvider
)
{
_ctx = dbContextProvider.GetDbContext();
// Error: unitOfWork is null
}
}
I am not good at ABP. Where and how can I take a DbContext instance and inject it?
Do not call dbContextProvider.GetDbContext() in the constructor.
Implement _ctx as a getter and use it wherever you actually need the context.
public class SeedingTestDataAppService : MyAppServiceBase
{
private MyDbContext _ctx => _dbContextProvider.GetDbContext();
private readonly IDbContextProvider<MyDbContext> _dbContextProvider;
public SeedingTestDataAppService(IDbContextProvider<MyDbContext> dbContextProvider)
{
_dbContextProvider = dbContextProvider;
}
}
Related
i created my own dbcontextfactory and now i don't know how to correctly register it in di. Can you somebody help me please? IApplicationDbContext is just interfaces with db sets.
I have register ma DbContext as pooled db context factory
builder.Services.AddPooledDbContextFactory<MyContext>(options =>
{
....
});
Interface of my db factory
interface IApplicationDbContextFactory
{
IApplicationDbContext CreateDbContext();
}
Implementation db factory
public class MyContextFactory<TContext> : IApplicationDbContextFactory where TContext : DbContext, IApplicationDbContext
{
private readonly IDbContextFactory<TContext> _dbContextFactory;
public MyContextFactory(IDbContextFactory<TContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
public IApplicationDbContext CreateDbContext()
{
return _dbContextFactory.CreateDbContext();
}
}
How can i correctly register my factory to di?
Thank you
Lifetime for the factory registered by AddPooledDbContextFactory is Singleton. Just register it with builder.Services.AddSingleton<IApplicationDbContextFactory, MyContextFactory<MyContext>>(); (though Scoped and Transient should also work just as fine):
var serviceCollection = new ServiceCollection();
serviceCollection.AddPooledDbContextFactory<SomeContext>(builder => builder.UseSqlite($"Filename={nameof(SomeContext)}.db"));
serviceCollection.AddSingleton<IApplicationDbContextFactory, MyContextFactory<SomeContext>>();
var serviceProvider = serviceCollection.BuildServiceProvider();
var dbContextFactory = serviceProvider.GetRequiredService<IDbContextFactory<SomeContext>>();
using (var scope = serviceProvider.CreateScope())
{
var applicationDbContextFactory = serviceProvider.GetRequiredService<IApplicationDbContextFactory>();
var applicationDbContext = applicationDbContextFactory.CreateDbContext();
}
public class SomeContext : DbContext, IApplicationDbContext
{
public SomeContext(DbContextOptions<SomeContext> options) : base(options)
{
}
public DbSet<MyEntity> Entities { get; set; }
}
interface IApplicationDbContextFactory
{
IApplicationDbContext CreateDbContext();
}
public interface IApplicationDbContext
{
}
public class MyContextFactory<TContext> : IApplicationDbContextFactory where TContext : DbContext, IApplicationDbContext
{
private readonly IDbContextFactory<TContext> _dbContextFactory;
public MyContextFactory(IDbContextFactory<TContext> dbContextFactory)
{
_dbContextFactory = dbContextFactory;
}
public IApplicationDbContext CreateDbContext()
{
return _dbContextFactory.CreateDbContext();
}
}
I have implemented unit of work in the next way in Entity Framework Core.
Context:
public class DaleContext : DbContext, IDaleContext
{
private readonly IConnectionStringProvider _connectionStringProvider;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(_connectionStringProvider.ConnectionString);
base.OnConfiguring(optionsBuilder);
}
public DaleContext(IConnectionStringProvider connectionStringProvider)
{
_connectionStringProvider = connectionStringProvider;
}
public DbSet<ProductProducts { get; set; }
public override int SaveChanges()
{
ChangeTracker.DetectChanges();
var modifiedEntries = ChangeTracker.Entries()
.Where(x =x.State == EntityState.Added || x.State == EntityState.Modified).ToList();
return base.SaveChanges();
}
}
Unit of work:
public class UnitOfWork : IUnitOfWork
{
public UnitOfWork(DbContext dbContext)
{
DbContext = dbContext;
}
public DbContext DbContext { get; set; }
public int Commit()
{
return DbContext.SaveChanges();
}
public async Task<intCommitAsync()
{
return await DbContext.SaveChangesAsync();
}
}
Repository:
public class Repository<TEntity: IDisposable, IRepository<TEntity>
where TEntity : class
{
private readonly UnitOfWork _unitOfWork;
public Repository(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork as UnitOfWork;
}
public void Dispose()
{
_unitOfWork.DbContext.Dispose();
}
public void Create(TEntity entity)
{
_unitOfWork.DbContext.Entry(entity).State = EntityState.Added;
}
}
I have injected all with autofac:
public static class Container
{
public static ContainerBuilder RegisterInfraestructure(this ContainerBuilder containerBuilder)
{
containerBuilder.RegisterType<UnitOfWork>().As<IUnitOfWork>();
return containerBuilder;
}
}
public static class Container
{
public static ContainerBuilder RegisterDataResources(this ContainerBuilder containerBuilder)
{
var configurationBuilder = new ConfigurationBuilder()
.AddJsonFile("appsettings.json");
var configuration = configurationBuilder.Build();
containerBuilder.Register(x =new ConnectionStringProvider(configuration.GetConnectionString("Fgcm.Dale"))
).As<IConnectionStringProvider>();
containerBuilder.RegisterType<DaleContext>().As<DbContext>().As<IDaleContext>();
return containerBuilder;
}
}
public static class Container
{
public static ContainerBuilder RegisterRepository(this ContainerBuilder containerBuilder)
{
containerBuilder.RegisterType<CustomerRepository>().As<ICustomerRepository>();
containerBuilder.RegisterType<ProductRepository>().As<IProductRepository>();
containerBuilder.RegisterType<SaleDetailRepository>().As<ISaleDetailRepository>();
containerBuilder.RegisterType<SaleRepository>().As<ISaleRepository>();
containerBuilder.RegisterDataResources();
containerBuilder.RegisterInfraestructure();
return containerBuilder;
}
}
public static class Container
{
public static ContainerBuilder RegisterApplicationServiceResources(this ContainerBuilder
containerBuilder)
{
containerBuilder.RegisterRepository();
containerBuilder.RegisterType<DaleApplicationService>().As<IDaleApplicationService>();
return containerBuilder;
}
}
When I try to save data it doesn't works (doesn't insert data) ... I would like to know why ? Here are when I try to save:
public Product Create(Product product)
{
try
{
_productRepository.Create(product);
_unitOfWork.Commit();
return product;
}
catch (Exception e)
{
Debug.WriteLine(e);
return null;
}
}
And of course all are injected:
private readonly ICustomerRepository _customerRepository;
private readonly IProductRepository _productRepository;
private readonly ISaleDetailRepository _saleDetailRepository;
private readonly ISaleRepository _saleRepository;
private readonly IUnitOfWork _unitOfWork;
public DaleApplicationService(IProductRepository productRepository, ICustomerRepository customerRepository,
ISaleRepository saleRepository, ISaleDetailRepository saleDetailRepository, IUnitOfWork unitOfWork)
{
_productRepository = productRepository;
_customerRepository = customerRepository;
_saleRepository = saleRepository;
_saleDetailRepository = saleDetailRepository;
_unitOfWork = unitOfWork;
}
What am I missing?
PS: all this works with .NET Core Web Api.
As #zolty13 sort of hinted the Instance scope of your DbContext (DaleContext) is probably incorrect. By default Autofac sets the instance scope to Instance per dependency (also known as a "transient" lifetime) which means a new instance of DaleContext is created for every class that depend on it. So your UnitOfWork receives a different instance of DaleContext than IProductRepository. So changes in IProductRepository are not reflected in UnitOfWork.
One way to solve this is to avoid this convoluted wrapping of your DbContext like #Igor suggest. Do you really need this UnitOfWork? Instead, use a repository class that has one instance of DaleContext and make all the DB changes in there and save them.
Alternatively (if you really think you need a UnitOfWork) you can register your DaleContext with an instance per request scope. Do note: Entity Framework's DbContext is not thread safe, so if you need to do concurrent work, this is not a safe approach.
Otherwise, read up on Instance scope.
My ApplictionDbContextClass looks like this :-
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
//private static ApplicationDbContext _context;
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
public DbSet<Trip> Trips { get; set; }
public DbSet<Place> Places { get; set; }
public DbSet<UserTripDetail> UserTripDetails { get; set; }
public DbSet<TripPicture> TripPictures { get; set; }
}
My TripPictureController looks like this:-
//private readonly ApplicationDbContext _db = new ApplicationDbContext();
private readonly IUnitOfWork _unitOfWork;
private readonly ITripPictureRepository _tripPictureRepository;
public TripPicturesController(IUnitOfWork unitOfWork, ITripPictureRepository tripPictureRepository)
{
_unitOfWork = unitOfWork;
_tripPictureRepository = tripPictureRepository;
}
It also contains a post Action:-
[HttpPost]
public ActionResult Create(TripPicture model, HttpPostedFileBase ImageData)
{
if (ImageData != null)
{
model.TripId = 1;
model.Image = this.ConvertToBytes(ImageData);
}
_tripPictureRepository.Add(model);
_unitOfWork.Commit();
//_db.TripPictures.Add(model);
//_db.SaveChanges();
return View(model);
}
When ever i hit the post request, the model is not pushed into database. I am using dependency injection here. My guess is somewhere there is creation of different context object. i saw the following code in startup class :-
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(**ApplicationDbContext.Create**);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);}
My unitOfWork class is:-
public class UnitOfWork : IUnitOfWork
{
private DbContext _context;
public UnitOfWork(DbContext dbContext)
{
_context = dbContext;
}
public void Commit()
{
_context.SaveChanges();
}
}
and my repository class is :-
public class Repository<T> : IRepository<T> where T : class
{
protected DbSet<T> _dbSet;
public Repository(DbContext context)
{
_dbSet = context.Set<T>();
}
public void Add(T entity)
{
_dbSet.Add(entity);
}
}
The object is saved when i don't use unitOfWork. What is the problem!?
You are implementing Unit Of Work pattern incorrectly.
You are adding an item to _tripPictureRepository instance DbContext and calling _unitOfWork.Commit() on _unitOfWork instance which has another DbContext instance that has no idea about the added item (Item isn't tracked by the _unitOfWork's DbContext) which means it saves nothing.
The correct implementation of Unit of work is that your repositories should be exposed as properties, a DbContext injected to your Unit of work class and DbSet<T> of the repository will be populated from the DbContext like this:
public class UnitOfWork : IUnitOfWork
{
private DbContext _context;
public ITripPictureRepository TripsRepository{ get; }
public UnitOfWork(DbContext dbContext)
{
_context = dbContext;
Trips = new Repository<Trip>(_context.Trips)
}
public void Commit()
{
_context.SaveChanges();
}
}
Then in your controller inject an IUnitOfWork instance:
private readonly IUnitOfWork _unitOfWork;
public TripPicturesController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
_tripPictureRepository = tripPictureRepository;
}
And now use the _unitOfWork instance to do your CRUD operations:
[HttpPost]
public ActionResult Create(TripPicture model, HttpPostedFileBase ImageData)
{
if (ImageData != null)
{
model.TripId = 1;
model.Image = this.ConvertToBytes(ImageData);
}
_unitOfWork.TripsRepository.Add(model);
_unitOfWork.Commit();
return View(model);
}
You can read more about Repository and Unit Of Work patter together from this Microsoft Docs page.
I found the issue. My DbContextObjects were different whenever generated by ninject. In my ninject file i was registering services in kernel as following :-
kernel.Bind<DbContext>().To<ApplicationDbContext>();
This by default uses Transient scope, i.e. new object is created every time required.
hence i needed to change that to following :-
kernel.Bind<DbContext>().To<ApplicationDbContext>().InRequestScope();
The requested scope means - Only a single instance of the type will be created, and the same instance will be returned for each subsequent request.
More can be found here :- https://github.com/ninject/ninject/wiki/Object-Scopes
I am trying to implement the repository pattern in asp core. Everything seems to work fine with a few adjustments,except adding it to the controller:
public class HomeController : Controller
{
private IDocumentRepository _context;
public HomeController()
{
_context = new DocumentRepository(new myContext());
}
}
DocumentRepository.cs
public class DocumentRepository : IDocumentRepository, IDisposable
{
private myContext context;
public DocumentRepository(myContext context) : base()
{
this.context = context;
}
public IEnumerable<Document> GetDocuments()
{
return context.Document.ToList();
}
public Document GetDocumentByID(int id)
{
return context.Document.FirstOrDefault(x => x.Id == id);
}
IDocumentRepository.cs
public interface IDocumentRepository : IDisposable
{
IEnumerable<Document> GetDocuments();
Document GetDocumentByID(int documentId);
void InsertDocument(Document student);
void DeleteDocument(int documentID);
void UpdateDocument(Document document);
void Save();
}
The error
There is no argument given that corresponds to the required formal
parameter 'options' of
'myContext.myContext(DbContextOptions)
dotnetcore..NETCoreApp,Version=v1.0
Simply resolve IDocumentRepository from the DI container using constructor injection instead of manually instantiating it and it should work:
public class HomeController : Controller {
private IDocumentRepository _repository;
public HomeController(IDocumentRepository repository) {
_repository = repository;
}
}
For that, you'll need to ensure IDocumentRepository is correctly registered in ConfigureServices:
public void ConfigureServices(IServiceCollection services) {
services.AddScoped<IDocumentRepository, DocumentRepository>();
}
I'm learning on doing repository with unit of work. I also know how to do DI/IOC. But my problem is I can't figure out where to apply Unit of Work in my code. Below is a Generic Repository.
public abstract class Repository<T, C> : //IDisposable,
IRepository<T> where T : class
where C : DbContext, new()
{
private C entities = new C();
public C Context
{
get
{
return this.entities;
}
set
{
this.entities = value;
}
}
public virtual void Insert(T entity)
{
this.entities.Set<T>().Add(entity);
}
// remove some code for brevity
}
What I had tried so far:
Make a Unit of Work class
public class UnitOfWork : IUnitOfWork
{
private readonly FooContext _dbContext;
public UnitOfWork()
{
_dbContext = new DataContext;
}
public void Save()
{
_dbContext.SaveChanges();
}
// Dispose method
}
In my service:
public class ProductService : Repository<Product, FooContext>, IProductService
{
private readonly IProductRepository _prodRepo;
private readonly IUnitOfWork _uow;
public ProductService(IUnitOfWork uow, IProductRepository prodRepo)
{
_uow = uow;
_prodRepo = prodRepo;
}
public override void Insert(Item entity)
{
base.Insert(entity);
Save();
}
public void Save()
{
uow.Save();
}
// remove some code for brevity
}
There's no error when I build it. And when I try it apply in my Controller it doesn't give me some error. But when I try to run and debug it, in the Intellitrace, It doesn't give me an Insert statement and again, it does not give me an error.. Where have I gone wrong?
Any help would be much appreciated. Thanks!
We saw together you should separate your service from your repository.
Here, your problem seems to come from the fact you use two different dbcontext instances.
one in your repository and one in your UnitOfWork.
Instead, you should inject the same Dbcontext instance in your repository and your UnitOfWork.
EDIT:
You should write your different layers like this:
public class ProductService
{
private readonly IProductRepository productRepository;
private readonly IUnitOfWork unitOfWork;
public ProductService(IProductRepository productRepository, IUnitOfWork unitOfWork)
{
this.productRepository = productRepository;
this.unitOfWork = unitOfWork;
}
public IEnumerable<Product> GetCurrentProductsOnOrderForCustomer(int customerId)
{
// etc.
}
}
The controller layer should do this:
public class ProductController : Controller
{
private readonly IProductService prodService;
public ProductController(IProductService prodService)
{
this.prodService = prodService;
}
}
Here is your corrected UnitOfWork:
public class UnitOfWork : IUnitOfWork
{
private readonly IDbContext _dbContext;
public UnitOfWork(IDbContext dbContext)
{
_dbContext = dbContext;
}
public void Save()
{
_dbContext.SaveChanges();
}
// Dispose method
}
Here is an example of Repository
public class ProductRepository
{
private readonly IDbContext _context;
public ProductRepository(IDbContext dbContext)
{
this._context = context;
}
public virtual void Insert(T entity)
{
this._context.Products.Add(entity);
}
// remove some code for brevity
}
And you create a context class that inherits from DbContext, and that you inject in UoW and Repos.
public class MyApplicationContext : DbContext
{
public MyApplicationContext(string connectionString)
{
// Configure context as you want here
}
public DbSet<Product> Products { get; set; }
}