Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Can't create component ... dependencies to be satisfied #4148


User avatar
1
abdourahmani created

Hi ! I'm trying to build a generic DomainService. Here is my code :

Domain Service

public interface IImportDataManager<TEntity, TType, in TEntityDto> : IDomainService
    {
        string TableName { get; set; }

        IImportDataManager<TEntity, TType, TEntityDto> LoadDataInput(IEnumerable<TEntityDto> input);

        IImportDataManager<TEntity, TType, TEntityDto> InitDbTable(bool init);
    }

    public class ImportDataManager<TEntity, TType, TEntityDto> : DomainService, IImportDataManager<TEntity, TType, TEntityDto>
    where TEntity : class, IEntity<TType>
    where TEntityDto : class, IEntityDto<TType>
    {
        private readonly IMapper _iMapper;
        private readonly DbContext _db;
        public string TableName { get; set; } = "";

        public ImportDataManager(IRepository<TEntity, TType> repository, IMapper iMapper)
        {
            _iMapper = iMapper;
            _db = repository.GetDbContext();
        }

        public IImportDataManager<TEntity, TType, TEntityDto> LoadDataInput(IEnumerable<TEntityDto> input)
        {
            var list = input.Select(custAtmTrans => _iMapper.Map<TEntity>(custAtmTrans)).ToList();
            _db.AddRange(list);
            _db.SaveChanges();
            return this;
        }

        public IImportDataManager<TEntity, TType, TEntityDto> InitDbTable(bool init)
        {
            if (!init) return this;
            var sql = $"Delete from {TableName};";
            _db.Database.ExecuteSqlCommand(sql);
            return this;
        }
    }

Application Service (Consumer)

public class AcquereurAppService : RapproDabAppServiceBase, IAcquereurAppService
    {
        private readonly IImportDataManager<Acquereur, long, AcquereurDto> _importManager;

        public AcquereurAppService(IImportDataManager<Acquereur, long, AcquereurDto> importManager)
        {
            _importManager = importManager;
            _importManager.TableName = "Acquereur";
        }

        public void LoadAcquereurInput(IEnumerable<AcquereurDto> input, bool init)
        {
            _importManager
                .InitDbTable(init)
                .LoadDataInput(input);
        }
    }

Test Class

public class AcquereurAppServiceTests : AppTestBase
    {
        private readonly IAcquereurAppService _appService;
        private readonly List<AcquereurDto> _dataListOk;
        private readonly List<AcquereurDto> _dataListNotOk;
        private readonly DateTime _transDate;

        public AcquereurAppServiceTests()
        {
            _transDate = DateTime.Now.Date;
            _appService = Resolve<IAcquereurAppService>();
            var data = new TestDataClass();
            _dataListOk = data.GetGoodAcquereurTrans();
            _dataListNotOk = data.GetBadAcquereurTrans(); 
        }

        [Fact]
        public void Should_Create_Acquereur_With_Valid_Arguments()
        {
            //Act
            _appService.LoadAcquereurInput(_dataListOk, true);

            //Assert
            UsingDbContext(context =>
            {
                var number = context.Acquereurs.Count(p => p.TrnDt == _transDate);
                number.ShouldBe(10);
            });
        }

        [Fact]
        public void Should_Not_Create_Acquereur_With_Invalid_Arguments()
        {
            //Act and Assert
            Assert.Throws<AbpValidationException>(() =>
            {
                _appService.LoadAcquereurInput(_dataListNotOk, true);
            });
        }
    }

But I receive this error.

Castle.MicroKernel.Handlers.HandlerException : Can't create component 'Lbi.RapproDab.AppService.Import.AcquereurAppService' as it has dependencies to be satisfied.

'Lbi.RapproDab.AppService.Import.AcquereurAppService' is waiting for the following dependencies:
- Service 'Lbi.RapproDab.AppBusiness.Import.IImportDataManager`3[[Lbi.RapproDab.AppEntities.Acquereur, Lbi.RapproDab.Core, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null],[System.Int64, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[Lbi.RapproDab.AppDto.AcquereurDto, Lbi.RapproDab.Application, Version=4.1.0.0, Culture=neutral, PublicKeyToken=null]]' which was not registered.

What am I doing wrong ?

Please help.

Abdourahmani


3 Answer(s)
  • User Avatar
    0
    aaron created
    Support Team

    Generic types are not registered by convention.

    You need to register it manually:

    IocManager.Register(typeof(IImportDataManager<,,>), typeof(ImportDataManager<,,>), DependencyLifeStyle.Transient);
    
  • User Avatar
    0
    abdourahmani created

    I've got it working.

    Thank you !

  • User Avatar
    0
    freshkampo created

    Good Day, I have the same problem, but i dont understand where i need to register the generic type manually.

    IocManager.Register(typeof(IImportDataManager<,,>),typeof(ImportDataManager<,,>),DependencyLifeStyle.Transient);

    In my solution ¿where are this section?