Who is responsible for downloading the data?

In the MVC model, where is the responsibility of loading the view model?

Should the controller load data? If the view model itself loads the data ala: MyViewModel viewModel = MyViewModel.Create (someValue); If the service level loads, do the following: MyViewModel viewModel = MembershipService.Instance.Load (someValue);

+3
source share
5 answers

See this example of a really clean technique: http://www.lostechies.com/blogs/jimmy_bogard/archive/2009/06/29/how-we-do-mvc-view-models.aspx

: . "ASP.NET MVC In Action" CodeCampServer . IViewModelMapper {public ViewModel Map (); } . , IoC MapMedia Map. , , AutoMapper, , IViewModelMapper.

, ViewModel, .

UPDATE: , automapper, CodeCampServer. , ( ), .

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<>)));
+2

- , . , . , , , , .

, , - , , .

, , . , " " , , .

Linq-to-SQL ASP.Net MVC , , . , (StructureMap ), ASP.Net MVC .

+2

, ViewModel .

ViewModels . , .

. ASP.NET MVC -

+1

. , .

, , , , / , , .

i

0

:

view- > - > - >

; .

; . - .

. , . , , , . - EJB, .

. . , , .

, . .

So, this is a service that works with a database. The controller and view collaborate to display information, nothing more.

0
source

Source: https://habr.com/ru/post/1720546/


All Articles