小编Rog*_*ger的帖子

注入AutoMapper

我一直在努力将AutoMapper注入控制器.我喜欢Code Camp Server的实现.它创建了一个围绕AutoMapper的IMappingEngine的包装器.依赖注入使用StructureMap完成.但我需要使用Castle Windsor作为我的项目.那么,我们如何使用Windsor实现以下依赖注入和设置?我不是在寻找Castle Windsor中的逐行等效实现.如果你想这样做,请随意.相反,什么是Windsor相当于StructureMap的注册表和配置文件?我需要Profile来定义CreateMap <>,如下所示.

谢谢.

会议控制器:

public MeetingController(IMeetingMapper meetingMapper, ...)
Run Code Online (Sandbox Code Playgroud)

会议映射器:

public class MeetingMapper : IMeetingMapper
{

    private readonly IMappingEngine _mappingEngine;

    public MeetingMapper(IMappingEngine mappingEngine)
    {
      _mappingEngine = mappingEngine;
    }

    public MeetingInput Map(Meeting model)
    {
        return _mappingEngine.Map<Meeting, MeetingInput>(model);    
    }

    ......
}
Run Code Online (Sandbox Code Playgroud)

自动映射器注册表:

public class AutoMapperRegistry : Registry
{

    public AutoMapperRegistry()
    {
        ForRequestedType<IMappingEngine>().TheDefault.Is.ConstructedBy(() => Mapper.Engine);
    }
}
Run Code Online (Sandbox Code Playgroud)

会议映射器配置文件:

public class MeetingMapperProfile : Profile
{

    public static Func<Type, object> CreateDependencyCallback = (type) => Activator.CreateInstance(type);

    public T CreateDependency<T>()
    { …
Run Code Online (Sandbox Code Playgroud)

structuremap castle-windsor automapper

8
推荐指数
1
解决办法
3731
查看次数

标签 统计

automapper ×1

castle-windsor ×1

structuremap ×1