AutoMapper应该如何访问我的DAL? [英] How should AutoMapper access my DAL?

查看:165
本文介绍了AutoMapper应该如何访问我的DAL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 InvoiceInputModel 以这是一个项目的引用的专案编号属性实体。理想情况下,我想AutoMapper要能够从 InvoiceInputModel ,它看起来像这样映射整个发票实体:

 公共类InvoiceInputModel
{
    公众的Guid标识{搞定;组; }
    公众的DateTime日期{搞定;组; }
    公共字符串参考{搞定;组; }
    公众的Guid专案编号{搞定;组; }
}

显然,以下是坏的:

  Mapper.CreateMap< InvoiceInputModel,发票>()
    .ForMember(SRC => src.Project,选择=> opt.MapFrom(
        DEST => _unitOfWork.CurrentSession.Get<项目>(dest.ProjectId)
    )
);

我怎么告诉AutoMapper是 invoice.Project 应根据被映射到一个项目实体关闭的专案编号 InvoiceInputModel 属性,而preserving松耦合?

在发票/编辑我的 InvoiceController

  [HttpPost]
[授权]
公众的ActionResult编辑(InvoiceInputModel invoiceInputModel)
{
    VAR发票= _unitOfWork.CurrentSession.Get<发票和GT;(invoiceInputModel.Id)​​;    Mapper.Map< InvoiceInputModel,发票>(invoiceInputModel,发票);    invoice.Project = _unitOfWork.CurrentSession.Get<项目>(invoiceInputModel.ProjectId);
    //我想AutoMapper做以上。    _unitOfWork.CurrentSession.SaveOrUpdate(发票);
    _unitOfWork.Commit();    返回查看(发票);
}

我看到一些有关解析器和ResolveUsing,但我使用它没有经验。

我如何告诉AutoMapper做我的实体模具,输入模型和视图模型之间的,而preserving松耦合?还是有更好的办法?


解决方案

  

我如何告诉AutoMapper是invoice.Project应该映射到基于该专案编号财产的InvoiceInputModel项目实体,而preserving松耦合?


您不能。如果AutoMapper是去别的地方来获取数据,然后它不是松耦合。

您不能修改项目在这期特别无论如何 - 为什么你需要设置为项目的关系 ,是不是NHibernate的足够聪明地看到,物业并没有改变,并没有做什么?

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?

解决方案

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.

Your 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?

这篇关于AutoMapper应该如何访问我的DAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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