2
我正在学习AutoMapper并了解它用于对象映射。但是现在EFCodeFirst,Dapper和Petpoco都冷却了这些东西,这将允许我们直接使用POCO和数据库吗?我们真的需要Automapper吗?
那么有谁能让我知道为什么我们仍然需要automapper吗?
在此先感谢
最好的问候, Jalpesh
我正在学习AutoMapper并了解它用于对象映射。但是现在EFCodeFirst,Dapper和Petpoco都冷却了这些东西,这将允许我们直接使用POCO和数据库吗?我们真的需要Automapper吗?
那么有谁能让我知道为什么我们仍然需要automapper吗?
在此先感谢
最好的问候, Jalpesh
我通常使用Automapper映射域模型查看mdoels。如果进行DDD,通常认为在您的视图中使用您的域模型并不是一个好主意 - 视图通常对域有不同的关注点。
例如,你可能有一个用户模式在您的域:
public class User
{
public int Id {get;set;}
public string EmailAddress {get;set;}
public string FirstName {get;set;}
public string Surname {get;set;}
public string HashedPassword {get;set;}
public string EyeColour {get;set;}
}
而且你可能有一个用户汇总页面,显示这些项目的一个子集:
public class UserSummary
{
public string EmailAddress {get;set;}
public string Surname {get;set;}
}
你可以使用该视图上的UserSummary类,但您可能会从数据库中获取域用户模型。在这种情况下,你可以使用Automapper到Domain.User映射到ViewModel.UserSummary
var user = _repository.Get(1);
var viewmodel = Automapper.Map<Domain.User, ViewModel.UserSummary>(user);
return View(viewmodel);
它会有什么性能开销,因为我打算它作为非常大的网站? –
我真的不认为这会造成问题。我在大型网站上使用没有问题。实际上设置地图可能需要一些时间(Mapper.CreateMap调用),并且应该在应用程序启动时完成一次。以下是关于Automapper性能的分析:http://lostechies.com/jimmybogard/2009/08/05/analyzing-automapper-performance/ – Paul
如果您的View正在进行分页或过滤,则可能会有性能损失。 Automapper不能与IQueryable对象一起工作,因此您的查询将在映射时执行。如果您使用Telerik等网格,则在映射后返回viewdata时,它将应用其过滤和排序。你会想看看投影解决这个问题。 – Brian