AutoMapper 应如何访问我的 DAL?

发布于 2024-10-31 12:58:51 字数 1527 浏览 1 评论 0原文

我有一个带有 ProjectId 属性的 InvoiceInputModel ,它是对 Project 实体的引用。理想情况下,我希望 AutoMapper 能够从 InvoiceInputModel 映射整个 Invoice 实体,如下所示:

public class InvoiceInputModel
{
    public Guid Id { get; set; }
    public DateTime Date { get; set; }
    public string Reference { get; set; }
    public Guid ProjectId { get; set; }
}

显然,以下内容是不好的:

Mapper.CreateMap<InvoiceInputModel, Invoice>()
    .ForMember(src => src.Project, opt => opt.MapFrom(
        dest => _unitOfWork.CurrentSession.Get<Project>(dest.ProjectId)
    )
);

我如何告诉 AutoMapper < code>invoice.Project 应该映射到基于 InvoiceInputModel 中的 ProjectId 属性的 Project 实体,同时保持松散耦合?

在我的 InvoiceController 中进行发票/编辑:

[HttpPost]
[Authorize]
public ActionResult Edit(InvoiceInputModel invoiceInputModel)
{
    var invoice = _unitOfWork.CurrentSession.Get<Invoice>(invoiceInputModel.Id);

    Mapper.Map<InvoiceInputModel, Invoice>(invoiceInputModel, invoice);

    invoice.Project = _unitOfWork.CurrentSession.Get<Project>(invoiceInputModel.ProjectId);
    // I want AutoMapper to do the above.

    _unitOfWork.CurrentSession.SaveOrUpdate(invoice);
    _unitOfWork.Commit();

    return View(invoice);
}

我发现了一些有关“Resolvers”和 ResolveUsing 的内容,但我没有使用它的经验。

如何告诉 AutoMapper 执行此操作,同时保持实体模型、输入模型和视图模型之间的松散耦合?或者有更好的方法吗?

I have an InvoiceInputModel with a ProjectId property which is a reference to a Project entity. Ideally, I want AutoMapper to be able to map an entire Invoice entity from an InvoiceInputModel, which looks like this:

public class InvoiceInputModel
{
    public Guid Id { get; set; }
    public DateTime Date { get; set; }
    public string Reference { get; set; }
    public Guid ProjectId { get; set; }
}

Obviously the following is bad:

Mapper.CreateMap<InvoiceInputModel, Invoice>()
    .ForMember(src => src.Project, opt => opt.MapFrom(
        dest => _unitOfWork.CurrentSession.Get<Project>(dest.ProjectId)
    )
);

How do I tell AutoMapper that invoice.Project should be mapped to a Project entity based off of the ProjectId property in InvoiceInputModel while preserving loose coupling?

Invoice/Edit in my InvoiceController:

[HttpPost]
[Authorize]
public ActionResult Edit(InvoiceInputModel invoiceInputModel)
{
    var invoice = _unitOfWork.CurrentSession.Get<Invoice>(invoiceInputModel.Id);

    Mapper.Map<InvoiceInputModel, Invoice>(invoiceInputModel, invoice);

    invoice.Project = _unitOfWork.CurrentSession.Get<Project>(invoiceInputModel.ProjectId);
    // I want AutoMapper to do the above.

    _unitOfWork.CurrentSession.SaveOrUpdate(invoice);
    _unitOfWork.Commit();

    return View(invoice);
}

I spotted something about "Resolvers" and ResolveUsing, but I have no experience using it.

How do I tell AutoMapper to do this while preserving loose coupling between my entity models, input models and view models? Or is there a better way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿! 2024-11-07 12:58:52

如何告诉 AutoMapper,invoice.Project 应根据 InvoiceInputModel 中的 ProjectId 属性映射到 Project 实体,同时保持松散耦合?

你不能。如果 AutoMapper 去其他地方获取数据,那么它就不是松散耦合的。

无论如何,您都不会修改此特定视图中的 Project - 为什么您需要设置与 Project 的关系,nHibernate 是否不够智能,无法看到该属性没有' t改变了,什么也没做?

How do I tell AutoMapper that invoice.Project should be mapped to a Project entity based off of the ProjectId property in InvoiceInputModel while preserving loose coupling?

You can't. If AutoMapper is going somewhere else to fetch data, then it's not loose coupled.

You're not modifying the Project in this particular View anyway - why do you need to set the relationship to Project, isn't nHibernate smart enough to see that property hasn't changed, and not do anything?

怪我太投入 2024-11-07 12:58:52

我个人在视图模型中拥有实体而不是 ID,因此绑定会自动发生。

http://sprokhorenko.blogspot.com/2011/03/bind -to-entity-id.html

I personally have entities in viewmodels instead of IDs, so that binding happens automatically.

http://sprokhorenko.blogspot.com/2011/03/bind-to-entity-id.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文