. net设计模式MVVM与ViewModelService

本文关键字:ViewModelService MVVM 设计模式 net | 更新日期: 2023-09-27 18:17:54

在研究MVVM模式时,我经常在这个模式中遇到一个"services"组件,特别是在新的。net核心文档,webcast &;学习材料。

样本:

  • 索引。cshtml(视图)
  • HomeController(控制器)
  • ViewModelService (?)
  • IndexViewModel (ViewModel)
  • IndexModel(模型)

使用额外的抽象层(ViewModelService)是MVVM模式的扩展和良好实践,还是该模式有另一个名称?对我来说,代码看起来很好也很干净,但是大多数MVVM模式的解释都没有使用ViewModelService来创建ViewModel并将其传递给控制器。

根据下面的代码,有一个ViewModelService层,而不是将ViewModel直接传递给控制器。

谢谢。


更新代码

Index.cshtml

@model FishTank.ViewModels.DashboardViewModel

HomeController.cs

public class HomeController: Controller
{
    private readonly IViewModelService viewModelService;
    public HomeController(IViewModelService viewModelService)
    { 
        this.viewModelService = viewModelService;
    }
    public IActionResult Index()
    {
        ViewBag.Title = "Fish tank dashboard";
        return View(viewModelService.GetDashboardViewModel());
    }
}

ViewModelService.cs

    public class ViewModelService : IViewModelService
{
    private readonly ISensorDataService sensorDataService;
    private readonly IUrlHelper urlHelper;
    public ViewModelService(ISensorDataService sensorDataService, IUrlHelperFactory urlHelperFactory, IActionContextAccessor actionContextAccessor)
    {
        this.sensorDataService = sensorDataService;
        urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
    }
    public DashboardViewModel GetDashboardViewModel()
    {
        return new DashboardViewModel
        {
            LastFed = "unknown",
            WaterTemperatureTile = new SensorTileViewModel
            {
                Title = "Water temperature",
                Value = sensorDataService.GetWaterTemperatureFahrenheit().Value,
                ColorCssClass = "panel-primary",
                IconCssClass = "fa-sliders",
                Url = urlHelper.Action("GetWaterTemperatureChart", "History")
            }
            }
        };
    }
}

DashboardViewModel.cs

public class DashboardViewModel
{ 
    public SensorTileViewModel WaterTemperatureTile { get; set; }
    public SensorTileViewModel FishMotionTile { get; set; }
    public SensorTileViewModel WaterOpacityTile { get; set; }
    public SensorTileViewModel LightIntensityTile { get; set; }
    [Display(Name = "Please enter the food amount:")]
    public int FoodAmount { get; set; }
    [Display(Name = "Last feeding was at: ")]
    public string LastFed { get; set; }
}

. net设计模式MVVM与ViewModelService

是一个额外的抽象层(ViewModelService)的使用是MVVM模式的扩展和良好实践,还是有其他的这个模式的名称?

不,这不是mvvm模式本身的一部分,但它是常见的面向对象编程。

在我看来,保持你的控制器尽可能轻量级是一个最佳实践。控制器的"单一职责"是控制东西。因此,您不希望控制器与逻辑混淆。

另外,我可能会将ViewModel的创建移动到工厂(这是一个创建模式),并让服务获取数据以放入构造的ViewModel中。再强调一遍:努力实现类的单一职责。

像这样:

工厂:

public static class ViewModelFactory
{
    public static DashboardViewModel CreateDashboardViewModel()
    {
        return new DashboardViewModel
        {
            LastFed = "unknown",    
            WaterTemperatureTile = CreateSensorTileViewModel()
        };
    }
    public static SensorTileViewModel CreateSensorTileViewModel()
    {
        return new SensorTileViewModel
        {
            Title = "Water temperature",
            ColorCssClass = "panel-primary",
            IconCssClass = "fa-sliders"
        };
    }
}

ViewModelService:

public class ViewModelService : IViewModelService
{
    private readonly ISensorDataService sensorDataService;
    private readonly IUrlHelper urlHelper;
    public ViewModelService(
               ISensorDataService sensorDataService, 
               IUrlHelperFactory urlHelperFactory, 
               IActionContextAccessor actionContextAccessor)
    {
        sensorDataService = sensorDataService;
        urlHelper = urlHelperFactory.GetUrlHelper(actionContextAccessor.ActionContext);
    }
    public DashboardViewModel GetDashboardViewModel()
    {
        var model = ViewModelFactory.CreateDashboardViewModel();
        var url = urlHelper.Action("GetWaterTemperatureChart", "History")
        var waterTemp = sensorDataService.GetWaterTemperatureFahrenheit().Value;
        model.Value = waterTemp;
        model.url = url;
        return model;
    }
}

另外,我不喜欢构造函数注入,但这是一个品味问题:)

相关文章:
  • 没有找到相关文章