Hi Jimmy,
Thanks a lot for your response!
My app is basically a four layered app -
1. Presentation (Web/MVC)
2. Application (AutoMapper DTO profile and DTOs, App Services)
3. Domain
4. Infrastructure/Data (EF)
There is also a Infrastructure/Cross-Cutting layer with DTO Adapter (Automapper), DI container (Unity) etc.
I hope the following details will be helpful.
In my MVC Web app I am consuming the DTOs, which are configured and supplied by the Application layer. The configuration in Application layer is similar to the following -
using AutoMapper;
class PublicPortalModuleProfile : Profile
{
protected override void Configure()
{
var cityMappingExpression = Mapper.CreateMap<Restaurant, RestaurantDTO>();
}
}
And then mapped like this:
public RestaurantDTO GetRestaurantDetail(Guid restaurantId)
{
var restaurant = _restaurantRepository.GetRestaurantByRestaurantId(restaurantId);
return restaurant.ProjectedAs<RestaurantDTO>();
}
public static TProjection ProjectedAs<TProjection>(this Entity item)
where TProjection : class,new()
{
//TypeAdapterFactory uses AutomapperTypeAdapterFactory (configured/injected using Unity, shown later)
//and calls the Create() method which returns AutomapperTypeAdapter here
var adapter = TypeAdapterFactory.CreateAdapter();
return adapter.Adapt<TProjection>(item);
}
In the Infrastructure/Cross-Cutting layer:
In AutomapperTypeAdapterFactory class:
public AutomapperTypeAdapterFactory()
{
//scan all assemblies finding Automapper Profile
var profiles = AppDomain.CurrentDomain
.GetAssemblies()
.SelectMany(a => a.GetTypes())
.Where(t => t.BaseType == typeof(Profile));
Mapper.Initialize(cfg =>
{
foreach (var item in profiles)
{
if (item.FullName != "AutoMapper.SelfProfiler`2")
cfg.AddProfile(Activator.CreateInstance(item) as Profile);
}
});
}
public ITypeAdapter Create()
{
return new AutomapperTypeAdapter();
}
In AutomapperTypeAdapter class:
public TTarget Adapt<TTarget>(object source) where TTarget : class, new()
{
return Mapper.Map<TTarget>(source); // Error in this line
}
In Unity I have the following configurations -
container.RegisterType<ITypeAdapterFactory, AutomapperTypeAdapterFactory>(new ContainerControlledLifetimeManager());
var typeAdapterFactory = container.Resolve<ITypeAdapterFactory>();
TypeAdapterFactory.SetCurrent(typeAdapterFactory);
The complete error detail is as follows:
Missing type map configuration or unsupported mapping.
Mapping types:
Restaurant_DD4DBDDB13217B3C5A757FACC3ADBFF7FFBCE0547A6080879A4366BD1D0B08D8 -> RestaurantDTO
System.Data.Entity.DynamicProxies.Restaurant_DD4DBDDB13217B3C5A757FACC3ADBFF7FFBCE0547A6080879A4366BD1D0B08D8 ->
DhakaFoodies.Application.MainBoundedContext.DTO.RestaurantDTO
Destination path:
RestaurantDTO
Source value:
System.Data.Entity.DynamicProxies.Restaurant_DD4DBDDB13217B3C5A757FACC3ADBFF7FFBCE0547A6080879A4366BD1D0B08D8
Again, thanks for all your time and support. Keep up the great works.
Aftab.