AutoMapper 应如何访问我的 DAL?
我有一个带有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。如果 AutoMapper 去其他地方获取数据,那么它就不是松散耦合的。
无论如何,您都不会修改此特定视图中的
Project
- 为什么您需要设置与Project
的关系,nHibernate 是否不够智能,无法看到该属性没有' t改变了,什么也没做?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 toProject
, isn't nHibernate smart enough to see that property hasn't changed, and not do anything?我个人在视图模型中拥有实体而不是 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