Created
March 25, 2016 14:34
-
-
Save mikeobrien/6bb72126cf916b99da57 to your computer and use it in GitHub Desktop.
Automapper 5 + StructureMap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class GetHandler | |
{ | |
private readonly IRepository<Job> _jobs; | |
private readonly IMapper _mapper; | |
public class Response | |
{ | |
... | |
public List<JobModel> Jobs { get; set; } | |
} | |
public class JobModel | |
{ | |
public Guid Id { get; set; } | |
... | |
} | |
public GetHandler( | |
IRepository<Job> jobs, | |
IMapper mapper) | |
{ | |
_jobs = jobs; | |
_mapper = mapper; | |
} | |
public Response Execute() | |
{ | |
... | |
return new Response | |
{ | |
... | |
Jobs = _mapper.Map<List<JobModel>>(_jobs.ToList()) | |
}; | |
} | |
public class Mapping : Profile | |
{ | |
protected override void Configure() | |
{ | |
CreateMap<Job, JobModel>(); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Mapper : IMapper | |
{ | |
private readonly IMapper _mapper; | |
public Mapper(IEnumerable<Profile> profiles) | |
{ | |
_mapper = new MapperConfiguration(config => | |
{ | |
profiles.ForEach(config.AddProfile); | |
}).CreateMapper(); | |
} | |
public IConfigurationProvider ConfigurationProvider => _mapper.ConfigurationProvider; | |
public TDestination Map<TDestination>(object source) | |
{ | |
return _mapper.Map<TDestination>(source); | |
} | |
public TDestination Map<TDestination>(object source, Action<IMappingOperationOptions> opts) | |
{ | |
return _mapper.Map<TDestination>(source, opts); | |
} | |
public TDestination Map<TSource, TDestination>(TSource source) | |
{ | |
return _mapper.Map<TSource, TDestination>(source); | |
} | |
public TDestination Map<TSource, TDestination>(TSource source, Action<IMappingOperationOptions<TSource, TDestination>> opts) | |
{ | |
return _mapper.Map(source, opts); | |
} | |
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination) | |
{ | |
return _mapper.Map(source, destination); | |
} | |
public TDestination Map<TSource, TDestination>(TSource source, TDestination destination, Action<IMappingOperationOptions<TSource, TDestination>> opts) | |
{ | |
return _mapper.Map(source, destination, opts); | |
} | |
public object Map(object source, object destination, Type sourceType, Type destinationType, Action<IMappingOperationOptions> opts) | |
{ | |
return _mapper.Map(source, destination, sourceType, destinationType, opts); | |
} | |
public object Map(object source, Type sourceType, Type destinationType, Action<IMappingOperationOptions> opts) | |
{ | |
return _mapper.Map(source, sourceType, destinationType, opts); | |
} | |
public object Map(object source, object destination, Type sourceType, Type destinationType) | |
{ | |
return _mapper.Map(source, destination, sourceType, destinationType); | |
} | |
public object Map(object source, Type sourceType, Type destinationType) | |
{ | |
return _mapper.Map(source, sourceType, destinationType); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Registry : StructureMap.Configuration.DSL.Registry | |
{ | |
public Registry() | |
{ | |
... | |
For<IMapper>().Use<Mapper>(); | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment