Do we really need Automapper?
I was learning AutoMapper and understand its use for object to object mapping. But now EFCodeFirst,dapper and Petpoco all cools stuff are开发者_Python百科 there which will allow us to use our POCO directly with database?
So can anybody let me know why we still need automapper?
Thanks in advance
Best Regards, Jalpesh
I usually use Automapper to map Domain models to view mdoels. If doing DDD it is often suggested that it's not a great idea to use your Domain models in you views - views often have a different set of concerns to the domain.
For example, you may have a User model in your domain:
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;}
}
And you may have a User summary page which shows a subset of these items:
public class UserSummary
{
public string EmailAddress {get;set;}
public string Surname {get;set;}
}
You could use the UserSummary class on the view, but you would probably fetch the domain user model from the db. In this case you could use Automapper to map the Domain.User to the ViewModel.UserSummary
var user = _repository.Get(1);
var viewmodel = Automapper.Map<Domain.User, ViewModel.UserSummary>(user);
return View(viewmodel);
精彩评论