可以automapper映射的外键使用的存储库的对象? [英] Can automapper map a foreign key to an object using a repository?

查看:205
本文介绍了可以automapper映射的外键使用的存储库的对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试实体框架code首先CTP4。假设我有:

I'm trying out Entity Framework Code first CTP4. Suppose I have:

public class  Parent
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Child
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Parent Mother { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<Parent> Parents { get; set; }
    public DbSet<Child> Children { get; set; }
}

public class ChildEdit
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int MotherId { get; set; }
}

Mapper.CreateMap<Child, ChildEdit>();

映射到编辑模式是没有问题的。在我的屏幕我通过一些控制(DropDownList的,autocompleter等),母亲的ID选择母亲被张贴在后面:

Mapping to the Edit model is not a problem. On my screen I select the mother through some control (dropdownlist, autocompleter, etc) and the Id of the mother gets posted in back:

[HttpPost]
public ActionResult Edit(ChildEdit posted)
{
    var repo = new TestContext();

    var mapped = Mapper.Map<ChildEdit, Child>(posted);  // <------- ???????
}

我应该如何解决最后映射?我不想把Mother_Id的子对象。现在我用这个解决办法,但我希望它可以在Automapper得到解决。

How should I solve the last mapping? I don't want to put Mother_Id in the Child object. For now I use this solution, but I hope it can be solved in Automapper.

        Mapper.CreateMap<ChildEdit, Child>()
            .ForMember(i => i.Mother, opt => opt.Ignore());

        var mapped = Mapper.Map<ChildEdit, Child>(posted);
        mapped.Mother = repo.Parents.Find(posted.MotherId);

修改
这工作,但现在我需要做的,对于每个外键(BTW:背景将是最终的解决方案注入):

EDIT This works, but now I have to do that for each foreign key (BTW: context would be injected in final solution):

        Mapper.CreateMap<ChildEdit, Child>();
            .ForMember(i => i.Mother,
                       opt => opt.MapFrom(o => 
                              new TestContext().Parents.Find(o.MotherId)
                                         )
                      );

我真正喜欢的是:

What I'd really like would be:

        Mapper.CreateMap<int, Parent>()
            .ForMember(i => i, 
                       opt => opt.MapFrom(o => new TestContext().Parents.Find(o))
                      );

        Mapper.CreateMap<ChildEdit, Child>();

这是可能与Automapper?

Is that possible with Automapper?

推荐答案

首先,我会假设你有一个像库接口 IRepository&LT; T&GT;

First, I'll assume that you have a repository interface like IRepository<T>

然后创建下面的类:

public class EntityConverter<T> : ITypeConverter<int, T>
{
    private readonly IRepository<T> _repository;
    public EntityConverter(IRepository<T> repository)
    {
        _repository = repository;
    }
    public T Convert(ResolutionContext context)
    {
        return _repository.Find(System.Convert.ToInt32(context.SourceValue));       
    }
}

基本上,这个类将被用来做一个int和域实体之间的转换。它采用了实体的ID从库加载它。该IRepository将被注入到使用IoC容器转换器,但更多的后来。

Basically this class will be used to do all the conversion between an int and a domain entity. It uses the "Id" of the entity to load it from the Repository. The IRepository will be injected into the converter using an IoC container, but more and that later.

让我们使用配置AutoMapper映射:

Let's configure the AutoMapper mapping using:

Mapper.CreateMap<int, Mother>().ConvertUsing<EntityConverter<Mother>>();

我建议创建这个通用的映射,而不是这样,如果你有母亲对其他类他们没有额外的努力自动映射。

I suggest creating this "generic" mapping instead so that if you have other references to "Mother" on other classes they're mapped automatically without extra-effort.

关于依赖注入的IRepository,如果你使用温莎城堡,在AutoMapper的配置也应该有:

Regarding the Dependency Injection for the IRepository, if you're using Castle Windsor, the AutoMapper configuration should also have:

IWindsorContainer container = CreateContainer();
Mapper.Initialize(map => map.ConstructServicesUsing(container.Resolve));

我用这个方法,它工作得很好。

I've used this approach and it works quite well.

这篇关于可以automapper映射的外键使用的存储库的对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆