AutoMapper应该如何访问我的DAL? [英] How should AutoMapper access my 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 aProjectId
property which is a reference to aProject
entity. Ideally, I want AutoMapper to be able to map an entireInvoice
entity from anInvoiceInputModel
, 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 aProject
entity based off of theProjectId
property inInvoiceInputModel
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 toProject
, isn't nHibernate smart enough to see that property hasn't changed, and not do anything?这篇关于AutoMapper应该如何访问我的DAL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!