Base solution for your next web application
Welcome
Spring Sale!
Limited
Time Offer!
XXdays : XXhrs : XXmin : XXsec
Open Closed

Custom repository DI in appService #569


0
doubledp created

Hi,

I would like to know if it is possible for dependency injection to work in the ApplicationService, if one would implement generics within the custom repository?

Something like this...

Custom repository interface:

public interface ICustomRepository<TEntity> : IRepository<TEntity, int>, ITransientDependency
        where TEntity : class, IEntity<int>
    {
        List<TEntity> GetRepositoryList();
    }

Custom repository:

public class CustomRepository<TEntity> : ApplicationRepositoryBase<TEntity>, ICustomRepository<TEntity>
        where TEntity : class, IEntity<int>
    {
        public CustomRepository(IDbContextProvider<ApplicationDbContext> dbContextProvider)
            : base(dbContextProvider)
        {

        }

        public List<TEntity> GetRepositoryList()
        {

        }
    }
}

Custom application service:

public class CustomAppService : CaerusAppServiceBase, ICustomAppService
    {
        private readonly ICustomRepository<CustomEntity> _customEntityRepository;
        
        public CustomAppService (ICustomRepository<CustomEntity> customEntityRepository)
        {
            _customEntityRepository = customEntityRepository;
        }
}

4 Answer(s)
  • 0
    hikalkan created
    Support Team

    You can register it like that:

    IocManager.Register(typeof(ICustomRepository<>), typeof(CustomRepository<>), DependencyLifeStyle.Transient);
    IocManager.Register(typeof(ICustomRepository<,>), typeof(CustomRepository<,>), DependencyLifeStyle.Transient);
    

    in your Module's Initialize method.

  • 0
    doubledp created

    Thank you very much.

    So does this then mean that the repository interface and implementation of it must reside within the Core assembly?

    Currently I have the interface in the Core assembly and the implementation of it in the EntityFramework assembly. I am running into the problem of not being able to reference the custom repository in the core module class because of that.

    What is the correct way to implement this?

  • 0
    hikalkan created
    Support Team

    Hi,

    Your interface should be in .Core project. Your implementation should be in .EntityFramework project. And finally, your IocManager registration code should be in your .EntityFramework project's module class's Initialize method.

    Have a nice day.

  • 0
    doubledp created

    Thank you Halil :)

    Have a great day!