I'm currently trying to use custom AutoMapper profiles in v8. I'm registering the profile in a custom IUserComposer with composition.Register<Profile, CalendarToCalendarProfile>(Lifetime.Transient);.
My profile looks currently like this:
public class CalendarToCalendarProfile : Profile
{
public CalendarToCalendarProfile()
{
var map = CreateMap<CalendarDto, Models.Calendar>(MemberList.Destination);
//map.ForMember()
}
public override string ProfileName => "CalendarDtoToCalendar";
}
When I now try to map my classes it can't find a registered mapper.
I tried using Mapper.Map() and injecting IMapper to my class.
I have been setting up the profiles as part of the Composer .
so with for example in the Compose function of an IComposer class :
composition.Register<Profile, MyCustomProfile>();
with a profile class like :
internal class MyCustomProfile : Profile
{
public MyCustomProfile()
{
// dto -> model
CreateMap<MyCustomModelDto, MyCustomModel>();
// model -> dto
CreateMap<MyCustomModel, MyCustomModelDto>();
}
}
this seems to be working for me.
the only other thing to consider is order - I set up the profile mappings first in the Composer so that they are then available in all the services/classes that might be setup later in the function
Ok after a bit of debugging I found out that the problem is not actually caused by automapper but by IQuery that I try to use. As these are also using mappers which you can't implement yourself as they need an attribute which is internal. And tbh I'm not sure why it is needed.
The way Automapper 9 works with Umbraco 8.6.1 for me.
You need something like this in Composer for DI.
However i am not sure if i have to use factory somehow.
public class Composer : IUserComposer
{
public void Compose(Composition composition)
{
composition.Register(GetMapper, Lifetime.Singleton);
}
public IMapper GetMapper(IFactory factory)
{
var config = new MapperConfiguration(cfg =>
{
var profiles = GetType().Assembly.GetTypes().Where(x => typeof(Profile).IsAssignableFrom(x));
foreach (var profile in profiles)
{
cfg.AddProfile(Activator.CreateInstance(profile) as Profile);
}
});
config.AssertConfigurationIsValid();
return new Mapper(config);
}
}
Using AutoMapper
Hi all,
I'm currently trying to use custom AutoMapper profiles in v8. I'm registering the profile in a custom IUserComposer with
composition.Register<Profile, CalendarToCalendarProfile>(Lifetime.Transient);
.My profile looks currently like this:
When I now try to map my classes it can't find a registered mapper.
I tried using Mapper.Map() and injecting IMapper to my class.
Any suggestions?
Hi,
I have been setting up the profiles as part of the Composer .
so with for example in the
Compose
function of anIComposer
class :with a profile class like :
this seems to be working for me.
the only other thing to consider is order - I set up the profile mappings first in the Composer so that they are then available in all the services/classes that might be setup later in the function
Hi Kevin,
how do you actually do the mapping? Do you try to get them via DI or just calling the static Mapper.Map() method?
Hi,
calling the static Mapper.Map often in a repository.
e.g
Ok after a bit of debugging I found out that the problem is not actually caused by automapper but by IQuery that I try to use. As these are also using mappers which you can't implement yourself as they need an attribute which is internal. And tbh I'm not sure why it is needed.
The way Automapper 9 works with Umbraco 8.6.1 for me. You need something like this in Composer for DI. However i am not sure if i have to use factory somehow.
is working on a reply...