Hello,
Could you explain how SetDefaultRepository functionality is supposed to work. For me SetDefaultRepository seems to do nothing. (Tried this in different projects and also tried this on Acme.BookStore tutorial project)
How do I create/set default repository for all entities in dbContext?
Looking at documentation I create classes MyRepositoryBase<TEntity> and MyRepositoryBase<TEntity, TKey>. Then following documentation set them with SetDefaultRepository. context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options.SetDefaultRepositoryClasses( typeof(MyRepositoryBase<,>), typeof(MyRepositoryBase<>) ); }); Base class MyRepositoryBase is ignored.
I get expected result when I set base class for specific entity context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options.AddRepository<Authors.Author, MyRepositoryBase<Authors.Author, Guid>>(); });
Am I not misunderstanding something? I would like to set base class for all entities. How do I accomplish this?
5 Answer(s)
-
0
It registers your custom class as
IRepository<>
andIBasicRepository<>
. So, if some class injects one of them, your custom class will be used.The following Unit Test should explain clearly: https://github.com/abpframework/abp/blob/e3e1779de6df5d26f01cdc8e99ac9cbcb3d24d3c/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs#L169
-
0
It registers your custom class as
IRepository<>
andIBasicRepository<>
. So, if some class injects one of them, your custom class will be used.The following Unit Test should explain clearly: https://github.com/abpframework/abp/blob/e3e1779de6df5d26f01cdc8e99ac9cbcb3d24d3c/framework/test/Volo.Abp.Ddd.Tests/Volo/Abp/Domain/Repositories/RepositoryRegistration_Tests.cs#L169
UnitTest shows that class is indeed registered.
But same happens to my projects, class is registered but not injected. For example we will have two entities Author and Book and context BookStoreDbContext.
Example 1:
context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options .AddDefaultRepositories<BookStoreDbContext>(true) .SetDefaultRepositoryClasses( typeof(MyRepositoryBase<,>), typeof(MyRepositoryBase<>) ); }
If we look through registered services we can find that MyRepositoryBase was indeed registered for each entity. (There is 8 registered instances for each entity with interfaces IReadOnlyBasicRepository, IReadOnlyRepository, IBasicRepository, IRepository with and without key)
BUT when I inject IRepository<Author, Guid> my base repository MyRepositoryBase is ignored.
Example 2:
context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options.AddRepository<Author, MyRepositoryBase<Author, Guid>>(); }
If we look through registered services we can find that MyRepositoryBase was indeed registered for entity Author.
When I inject IRepository<Author, Guid> eveything works as expected. MyRepositoryBase is not ignored and is injected.
Example 3:
context.Services.AddAbpDbContext<BookStoreDbContext>(options => { options .AddDefaultRepositories<BookStoreDbContext>(true) .SetDefaultRepositoryClasses( typeof(MyRepositoryBase<,>), typeof(MyRepositoryBase<>) ); } options.AddRepository<Book, MyRepositoryBase<Book, Guid>>(); is converted to options.AddRepository<>();
Same as before MyRepositoryBase registered for every entity.
When I inject IRepository<Author, Guid> my base repository MyRepositoryBase is ignored. When I inject IRepository<Book, Guid> eveything works as expected. MyRepositoryBase is not ignored and is injected.
Testing done on ABP Framework version: 4.4.3 and 5.0.1
As I told before I do not understand how I am supposed to set up base repository.
Could you provide working example project with ef core where SetDefaultRepositoryClasses is working. Or some workaround? Or explanation what I am not understanding.
-
0
How did you implement MyRepositoryBase ?
Is it extended from EfCoreRepository ?
-
0
MyRepositoryBase<TEntity> : EfCoreRepository<BookStoreDbContext, TEntity> where TEntity : class, IEntity
andMyRepositoryBase<TEntity, TKey> : EfCoreRepository<BookStoreDbContext, TEntity, TKey> where TEntity : class, IEntity<TKey>
also tried to extend RepositoryBase directly. Nothing worked for me and as I stated in examples everything works fine when using AddRepository for specific entity.
If you can provide me with simple ef core example project where SetDefaultRepositoryClasses is working it would be great. With actual implementation since MyRepositoryBase gets registered to services, but with wrong state or something. Because it is not injected and is ignored.
-
0
hi Kaspars
Please share your example project with me, I will check it. liming.ma@volosoft.com