question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

upgrade 2.0.2 --> 2.2.1 my custom repository can not be injected

See original GitHub issue

my repository code:

    public abstract class EsdServiceRepositoryBase<TEntity, TPrimaryKey> : EfCoreRepositoryBase<EsdServiceDbContext, TEntity, TPrimaryKey>
        where TEntity: class, IEntity<TPrimaryKey>
    {
        protected EsdServiceRepositoryBase(IDbContextProvider<EsdServiceDbContext> dbContextProvider)
            : base(dbContextProvider) { }
    }

    public class EsdServiceRepository<TEntity, TPrimaryKey>
        : EsdServiceRepositoryBase<TEntity, TPrimaryKey>
        where TEntity : class, IEntity<TPrimaryKey>
    {
        public EsdServiceRepository(IDbContextProvider<EsdServiceDbContext> db) : base(db) { }
    }

    public class EsdServiceRepository<TEntity>
        : EsdServiceRepositoryBase<TEntity, string>
        where TEntity : class, IEntity<string>
    {
        public EsdServiceRepository(IDbContextProvider<EsdServiceDbContext> db) : base(db) { }
    }

my application service code:

    public class ApplyService : EsdServiceAppServiceBase, IApplyService
    {
        private readonly IRepository<Apply, string> _applyRepository;

        public ApplyService(IRepository<Apply, string> applyRepository)
        {
            _applyRepository = applyRepository;
        }

        public async Task<GetApplyOutput> Get(string id)
        {
            var apply = await _applyRepository.GetAsync(id);
            return apply.MapTo<GetApplyOutput>();
        }
    }

my EsdServiceDbContext code:

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            GetModelTypes().ToList().ForEach(
                (type) =>
                {
                    modelBuilder.Entity(type);
                });
            base.OnModelCreating(modelBuilder);
        }

        private IEnumerable<Type> GetModelTypes()
        {
            Assembly assembly = typeof(EsdServiceCoreModule).GetTypeInfo().Assembly;
            return assembly.GetTypes().Where(IsModelType);
        }

        public static bool IsModelType(Type type)
        {
            Type baseType = typeof(IEntity<>);
            return Array.Exists(
                   type.GetInterfaces(),
                   t =>
                       t.GetTypeInfo().IsGenericType &&
                       t.GetTypeInfo().GetGenericTypeDefinition() == baseType
                   ) &&
                   !type.GetTypeInfo().IsAbstract &&
                   !type.GetTypeInfo().IsGenericType &&
                   type.GetTypeInfo().IsClass &&
                   type.GetTypeInfo().IsPublic;
        }

I did not use the IDbSet <> attribute in DbContext , and I tried to use “[AutoRepositoryTypes( typeof(IRepository<>), typeof(IRepository<,>), typeof(EsdServiceRepository<>), typeof(EsdServiceRepository<,>))]” on DbContext is also invalid

I use a single database, so I do not want to use IDbSet <>, please give a solution, thanks!

Exception Message:

Can’t create component ‘Zac.EsdService.OrderInfos.ApplyService’ as it has dependencies to be satisfied.\r\n\r\n’Zac.EsdService.OrderInfos.ApplyService’ is waiting for the following dependencies:\r\n- Service ‘Abp.Domain.Repositories.IRepository`2[[Zac.EsdService.OrderInfos.Apply, Zac.EsdService.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]’ which was not registered.\r\n"

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
devsharpercommented, Aug 1, 2017

Define a custom DbContextEntityFinder,

    public class DbContextEntityFinder : IDbContextEntityFinder
    {
        public IEnumerable<EntityTypeInfo> GetEntityTypeInfos(Type dbContextType)
        {
            var dbcontext = Activator.CreateInstance(dbContextType);
            var metaData = ((IObjectContextAdapter) dbcontext).ObjectContext.MetadataWorkspace;
            var objectItemCollection = (ObjectItemCollection) metaData.GetItemCollection(DataSpace.OSpace);
            var entityTypes = metaData.GetItems<EntityType>(DataSpace.OSpace).Select(et => objectItemCollection.GetClrType(et)).ToList();

            return entityTypes.Select(t => new EntityTypeInfo(t, dbContextType)).ToList();
        }
    }

replace the default one in Module PreInitialize,

    public override void PreInitialize()
    {
        Configuration.ReplaceService<IDbContextEntityFinder, DbContextEntityFinder>();  
    }
0reactions
zengqingleicommented, Aug 7, 2017

@hikalkan

Yes, you understand that!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Custom repository auto register doesn't work · Issue #2280
ABP 2.1.3, here is my code: I define an abstract RepositoryBase ... upgrade 2.0.2 --> 2.2.1 my custom repository can not be injected...
Read more >
javascript - Can't inject custom repository
I have a few ideas about how to do this, and my tie it in to typeorm-transactional-cls-hooked. In the meantime, I created simple...
Read more >
Upgrading your build from Gradle 4.x to 5.0
This chapter provides the information you need to migrate your older Gradle 4.x builds to Gradle 5.0. In most cases, you will need...
Read more >
Can't download dependency's source code - why? Follow
When I jump to one of the library's files, I can't download the sources. Why? Here is the dependency from my module's POM::...
Read more >
Older Release Notes - Nexus Repository Manager 2
See the complete release notes for all resolved issues. This release fixes a critical security issue. All Nexus Repository Manager instances should upgrade. ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found