ASP.NET MVC设计模式的最佳实践与服务
我有一个ASP.NET MVC 3应用程序。
I have an ASP.NET MVC 3 application.
我有一个模式
,视图模型
,查看
,控制器
。
我用 Ninject
为国际奥委会。
我的控制器
使用视图模型
将数据传递到查看
。
My Controller
uses a ViewModel
to pass data to the View
.
我已经开始使用服务
S(混凝土和接口类型)采取从信息视图模型
和查询它针对数据库对其进行操作。
I've started to use Service
s (concrete and interface types) to take information from the ViewModel
and query it against the database to manipulate it.
我可以使用相同的服务
设置的视图模型
?或者,这是怎么回事对设计模式的粮食?
Can I use the same Service
to setup the ViewModel
? Or is this going against the grain of the design pattern?
即。我可以抽象设置了视图模型
在服务
图层?
I.e. Can I abstract setting up the ViewModel
in the Service
layer?
场景的
Scenario
这种情况的出现;我的模式
有大量引用了其他模式
,所以当我设置了视图模型
控制器是要啰嗦了,我觉得控制器
做太多。所以我想能够只是做一些事情,如:
The scenario is; my Model
has lots of references to other Models
, so when I setup the ViewModel
in the controller it's to verbose, and I feel the Controller
is doing too much. So I want to be able to just do something like:
VAR VM = _serviceProvider.SetupViewModel(GUID model1Id中GUID model2Id,/ *等* /)
而在 SetupViewModel
功能的ServiceProvider
是这样的:
public MyModelViewModel SetupViewModel(Guid model1Id, Guid model2Id, /*etc...*/)
{
var vm = new MyModelViewModel();
var model1 = _repository.Model1s.FirstOrDefault(x => x.Id.Equals(model1Id));
var model2 = _repository.Model2s.FirstOrDefault(x => x.Id.Equals(model2Id));
// etc....
vm.Model1 = model1;
vm.Model2 = model2;
return vm;
}
通过这样做,我也可以添加一些空
条件好,不担心让我的控制器
真的真正的大!
By doing this I could also add some null
conditions as well, not worrying about making my Controller
really really really big!!
我用1 视图模型
为创建/编辑操作。我不重用视图模型
在别处。
I use 1 ViewModel
for the Create/Edit actions. I don't reuse the ViewModel
elsewhere.
我先给服务层控制器返回一个域模型和映射到一个视图模型。
I would let the service layer return a Domain Model and map it to a ViewModel in the controller.
此方式,您可以使用多个的ViewModels服务方法,例如桌面和移动视图。
This way you can use a service method with multiple ViewModels, for a desktop and mobile view for example.
您可以让 AutoMapper 为你做的辛勤工作,或做手工,通过创建一个构造函数在ViewModel更是把域模型。
You can let AutoMapper do the hard work for you or do it manually, by creating a constructor in the ViewModel which takes the Domain Model.
域模型:
public class Customer
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public virtual ICollection<Order> Orders { get; set; }
}
视图模型:
public class CustomerWithOrdersModel
{
public CustomerWithOrdersModel(Customer customer)
{
Id = customer.Id;
FullName = string.Format("{0}, {1}", customer.LastName, customer.FirstName);
Orders = customer.Orders.ToList();
}
public int Id { get; set; }
public string FullName { get; set; }
public IEnumerable<Order> Orders { get; set; }
}
编辑:AutoMapper例如:
AutoMapper example:
含有从客户
来映射的AutoMapper配置文件的 CustomerWithOrdersModel
:
The AutoMapper profile containing the mapping from a Customer
to a CustomerWithOrdersModel
:
public class ViewModelProfile : Profile
{
public override string ProfileName
{
get { return "ViewModel"; }
}
protected override void Configure()
{
CreateMap<Customer, CustomerWithOrdersModel>()
.ForMember(dest => dest.FullName, opt => opt.MapFrom(src => string.Format("{0}, {1}", src.LastName, src.FirstName)))
.ForMember(dest => dest.Orders, opt => opt.MapFrom(src => src.Orders.ToList()));
}
}
编号
按照惯例映射。
有关扩展方法 ViewModelProfile
:
public static class ViewModelProfileExtensions
{
public static CustomerWithOrdersModel ToModel(this Customer customer)
{
return Mapper.Map<CustomerWithOrdersModel>(customer);
}
public static Customer ToEntity(this CustomerWithOrdersModel customerWithOrdersModel)
{
return Mapper.Map<Customer>(customerWithOrdersModel);
}
}
控制器动作:
public ActionResult Details(int customerId)
{
Customer customer = _customerService.GetById(customerId);
CustomerWithOrdersModel customerWithOrders = customer.ToModel();
return View(customerWithOrders);
}
如果您从 CustomerWithOrdersModel
到客户映射
,您可以用 customerWithOrdersModel。 ToEntity()
来映射回它的域模型。
If you create a mapping from CustomerWithOrdersModel
to Customer
, you can use customerWithOrdersModel.ToEntity()
to map it back to the Domain Model.
这就是它!您可以使用客户
从视图模型域模型取出的构造。
Thats it! You can remove the constructor with the Customer
domain model from the ViewModel.