UmbracoMapper, Injecting services
# help-with-umbraco
n
With UmbracoMapper, how can I inject IPublishedContentCache service into a IMapDefinition file?
s
Inject in tyour
IMapDefinition
🙂
Copy code
cs
    public class MyMappingDefinition : IMapDefinition
    {
        private readonly IMyCoolService _mycoolService;

        public MyMappingDefinition(IMyCoolService mycoolService)
        {
            _mycoolService = mycoolService;
        }

        public void DefineMaps(IUmbracoMapper mapper)
        {
            mapper.Define<ISomething, SomethingElse>((_, _) => new SomethingElse(), Map);
        }

        private void Map(ISomething source, SomethingElse target, MapperContext)
        {
            _mycoolService.DoSomethingReallyCool();

            // do mapping
        }
    }
n
Well i want to inject IPublishedContentCache from Umbraco
but i am not allowed to do that
basically, i want to be able to fetch something from the published content in my mapping function.
s
If you REALLY want to, then it should be possible with the UmbracoContextAccessor - but I'd argue that your mapping method is doing too much if you are getting more content to populate a model
n
Well i never used mappers, in the past i always created a helper function in my object, to fill everything from the database. But i taught the mappers were intended for this purpose? So basically a mapper is only used for simpel transfer of data from one objec tto another object without filling objects in the object? or what would be a good solution for this in your opinion?
2 Views