谁拥有数据加载中负有责任? [英] Who has the responsibilty of loading data?

查看:113
本文介绍了谁拥有数据加载中负有责任?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC模型,其中不会加载视图模型谎言的责任?

In the MVC model, where does the responsibility of loading the view model lie?

如果控制器加载数据?
如果视图模型本身加载数据ALA:
   MyViewModel视图模型= MyViewModel.Create(someValue中);
如果一个服务层加载ALA:
   MyViewModel视图模型= MembershipService.Instance.Load(someValue中);

Should the Controller load the data? Should the View Model itself load the data ala: MyViewModel viewModel = MyViewModel.Create(someValue); Should a Service Layer load it ala: MyViewModel viewModel = MembershipService.Instance.Load(someValue);

推荐答案

查看真正的清洁技术的这个例子:
<一href=\"http://www.lostechies.com/blogs/jimmy%5Fbogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx\" rel=\"nofollow\">http://www.lostechies.com/blogs/jimmy%5Fbogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx

See this example of the really clean technique: http://www.lostechies.com/blogs/jimmy%5Fbogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx

另外,您也可以手动做到这一点:请参见ASP.NET MVC在行动的书或的 codeCampServer 来源的例子。基本上你注入IViewModelMapper {公共视图模型图(数据); }到控制器。整洁的事情是,它让你的IoC自动传递服务和库到您的视图模型映射器。然而,这可真让控制器是臃肿映射器接口,所以像吉米·博加德的技术,即使没有AutoMapper,而是用行动过滤器比不挑IViewModelMapper,效果会更好。

Alternatively you can do it manually: see "ASP.NET MVC In Action" book or CodeCampServer sources for examples. Basically you inject IViewModelMapper { public ViewModel Map(data); } to the controller. The neat thing is that it makes your IoC automatically pass services and repositories to your ViewModel mapper. However this can really make controllers be bloated with mapper interfaces so something like Jimmy Bogard's technique, even without AutoMapper, but with action filters than do pick IViewModelMapper, would be better.

如果你不能做到这一点,那么我建议坚持使用视图模型处理映射,马蒂亚斯建议。

If you can't do this, then I'd suggest stick with ViewModel handling mapping as Mathias suggested.

更新:这是automapper状的外形与C $ cCampServer方式$位的例子。不知道这是否将工作是(或有用的话),只是一个示范。

UPDATE: here's an example of automapper-like configuration with bits of CodeCampServer way. Not sure if it will work as is (or useful at all), just a demonstration.

public abstract class ViewModelMapper<Source, ViewModel> where Source: class, ViewModel: IViewModel
{
  public abstract ViewModel Map(Source source);
}

public class ProductDetailsViewModel
{
  public ProductViewModel Product { get; set; }
  punlic IList<Language> AvailableProductLanguages { get; set; }
}

public class ProductDetailsViewMapper: ViewModelMapper<Product, ProductDetailsViewModel>
{
  private ILanguageRepository languages;
  public ProductDetailsViewMapper(ILanguageRepository languages)
  {
     this.languages = languages;
  }
  public override ProductDetailsViewModel Map(Product source)
  {
     var vm = new ProductDetailsViewModel();
     AutoMapper.Map<Product, ProductDetailsViewModel>(product, vm);
     vm.AvailableProductLanguages = languages.GetAppropriateFor(product);
  }
}

public class ViewModelMapperActionFilter: ActionFilter
{
  Type mapperType;
  public ViewModelMapperActionFilter()
  {
  }
  public ViewModelMapperActionFilter(Type mapperType)
  {
  }
  public void OnActionExecuted(ControllerContext context)
  {
    var model = context.Result.ViewData.Model;
    var mapperType = this.MapperType ?? this.GetMapperTypeFromContext(context);
    // this is where magic happens - IoC grabs all required dependencies
    var mapper = ServiceLocator.GetInstance(mapperType);
    var method = mapperType.GetMethod("Map");
    Check.Assert(method.GetArguments()[0].ArgumentType == model.GetType());
    context.Result.ViewData.Model = method.Invoke(mapper, new[]{model});
  }
}

public class ProductsController: Controller
{
  [ViewModelMapper(typeof(ProductDetailsViewMapper))]
  // alternatively [ViewModelMapper()] will auto-pick mapper name by controller/action
  public ActionResult Details(EntityViewModel<Product> product)
  {
    // EntityViewModel is a special type, see 
    // http://stackoverflow.com/questions/1453641/my-custom-asp-net-mvc-entity-binding-is-it-a-good-solution
    return View(product.Instance); 
  }
}

//Global.asax.cs:
IoC.Register(AllTypes.DerivedFrom(typeof(ViewModelMapper<>)));

这篇关于谁拥有数据加载中负有责任?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆