1

I have Core in which Interface is declared as

    public interface IRequestProvider
    {
        int SomeId { get; set; }
    }

Implementation also define in same layer

and then I have another layer Repo layer in which I am calling another external nuget packages called DataAccess layer

in which I have declared

    public interface IRequestProvider
    {
        int SomeId { get; set; }
        int SomeOtherId { get; set; }
    }

so In core and DataAccess both layer I have defined IRequestProvider

Lamar code

 public static class SomeRegistry
    {
        public static void RegisterDISome(this ServiceRegistry services, IConfigurationRoot configurationRoot)
        {

            services.For<IRequestProvider>().Use<RequestProvider>().Scoped();

            services.For<DataAccessInterfaces.IRequestProvider>().Use<DataAccessModel.RequestProvider>().Scoped();

        }
    }

Scoped use to pass the same instance throughout the request

Automapper is enable

 public class DomainToRepoMappingsProfile : Profile
    {
        public DomainToRepoMappingsProfile()
        {
            this.CreateMap<IRequestProvider, DataAccess.IRequestProvider>()
                .ForMember(dst => dst.SomeOtherId, opt => opt.Ignore());
        }
    }

My expectation is when I change something in Core.IRequestProvider from any layer it should auto reflected in DataAccess.IRequestProvider layer

Currently I am calling IDomainToRepoMappingRequestProvider.map() each time to set DataAccess.IRequestProvider

 public class DomainToRepoMappingRequestProvider : IDomainToRepoMappingRequestProvider
    {
        private readonly IMapper _mapper = null;
        private readonly IRequestProvider _requestProvider = null;
        private DataAccess.IRequestProvider _dataAccessRequestProvider = null;
        public DomainToRepoMappingRequestProvider(IRequestProvider requestProvider, DataAccess.IRequestProvider dataAccessRequestProvider, IMapper mapper)
        {
            _mapper = mapper;
            _requestProvider = requestProvider;
            _dataAccessRequestProvider = dataAccessRequestProvider;
        }
        public void Map()
        {
            _mapper.Map(_requestProvider, _dataAccessRequestProvider);
        }
    }

I finding a solution to reflect changes automatically when something is changed without calling map()

Sushant
  • 375
  • 1
  • 2
  • 5

1 Answers1

0

How about having a property setter in the implementation of IRequestProvider call the mapper for you? The property getters and setters can be used to do much more than just setting a private backing field. An example:

public class RequestProvider : IRequestProvider
{
    private readonly _mappingProvider;
    private int _someId;

    public RequestProvider(IDomainToRepoMappingRequestProvider mappingProvider)
    {
        _mappingProvider = mappingProvider
    }

    public int SomeId
    {
        get;
        set 
        {
            _someId = value;
            _mappingProvider.Map();
        }
    }
}
  • I can do this way... but I am looking way through Dependency Injection – Sushant Jun 11 '19 at 10:02
  • I'm not sure that I understand what you are trying to achieve then. From how I understood it, you want to reflect changes to properties of an IRequestProvider object to another object of type DataAccess.IRequestProvider using an AutoMapper. Now you have to call the mapper every time you modify the source object. You want to automate that, which can be done by calling the mapper inside setters of source properties. But this doesn't have anything to do with DI except for wiring everything up. DI isn't meant to do data mapping between layers. At least as far as I know... – Tomas Vitha Jun 11 '19 at 18:24