【问题标题】:Should I use ViewModels and how to handle many-to-many relationships in ViewModels?我应该使用 ViewModel 吗?如何处理 ViewModel 中的多对多关系?
【发布时间】:2023-02-24 19:31:05
【问题描述】:

我是 .NET 的初学者,如果您发现我的问题很奇怪,我深表歉意。我还检查了其他 Stackoverflow 上关于我遇到的类似问题的帖子,但它们并没有真正帮助我理解这一点,所以我决定直接在这里提问。

我正在设计一个 API,我计划稍后将其与 Angular 一起用于订餐和送餐应用程序(类似于 HelloFresh 和其他此类服务)。我学习了关于 API 和 .NET 的非常基础的 Pluralsight 课程,导师使用 ViewModels 进行验证。所以,我的问题是,我真的需要它们吗?如果需要,我该如何处理实体的多对多关系部分?例如,我有这个 Meal 实体,它以多对多的方式连接到实体 Ingredient、Category、Size、User(用于喜欢和不喜欢一顿饭)和 Bundle 所有这些实体。这是 Meal 实体类的代码:

    public class Meal {
        public int Id { get; set; }
        public string Name { get; set; }
        public int SingularPrice { get; set; }
        public string Description { get; set; }
        public int Discount { get; set; }
        public ICollection<MealSize> Sizes { get; set; }
        public ICollection<MealCategory> Categories { get; set; }
        public ICollection<MealIngredient> Ingredients { get; set; }
        public ICollection<LikedMeals> LikedByUser { get; set; }
        public ICollection<DislikedMeals> DislikedByUser { get; set; }
        public ICollection<MealBundle> Bundles { get; set; }
    }

这是 Meal 连接到的其他实体之一的代码(随机选择作为成分之一):

    public class Ingredient {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Distributor { get; set; }
        public ICollection<IngredientType> Types { get; set; }
        public ICollection<MealIngredient> Meals { get; set; }
    }

这是他们加入实体的代码:

    public class MealIngredient {
        public int MealId { get; set; }
        public Meal Meal { get; set; }
        public int IngredientId { get; set; }
        public Ingredient Ingredient { get; set; }
        public int Quantity { get; set; }
        public string Measurement { get; set; }
    }

这是创建新餐点的 API 代码:

[HttpPost]
        public async Task<IActionResult> CreateMeal([FromBody] Meal meal){
            if (meal.Ingredients.Count() > 1){
                if (meal.Name != "" && meal.SingularPrice > 0 && meal.Description != ""){
                    await _dbContext.AddAsync(meal);
                    await _dbContext.SaveChangesAsync();
                    return Created("Successfully created a new meal!",meal);
                }
                else return BadRequest(meal);
            }
            else return BadRequest(meal);
        }

除了告诉我 Meal 实体的 ViewModel 应该是什么样子之外,如果有人也为 Ingredient 实体做同样的事情,我也会非常感激,所以我看到了硬币的两面,因为我计划为另一个实现创建方法提到的实体。

感谢任何提前对此做出回应的人!

从我在问题中发布的代码块中可以看出,我尝试直接在控制器方法中使用验证,但我不知道是否应该这样做。

编辑 1添加了一个代码块作为对@Klamsi 的(第一条)评论的响应的一部分。

    [HttpGet]
        [Route("{id}")]
        public async Task<IActionResult> GetMeal(int id){
            var result = await _dbContext.Meals
                .Include(meal => meal.Categories)
                .Include(meal => meal.Sizes)
                .Include(meal => meal.Ingredients)
                .Include(meal => meal.LikedByUser)
                .Include(meal => meal.DislikedByUser)
                .Include(meal => meal.Bundles)
                .FirstOrDefaultAsync(meal => meal.Id == id);
            if (result == null) 
                return NotFound();
            else return Ok(result);
        }

【问题讨论】:

  • 我会以另一种方式思考。 ViewModel 不是你的模型类,而是你的视图。想一想:视图需要什么才能正确显示和“管理”一顿饭。
  • 我什至没有专门的模型类,这些 ViewModels 本质上是导师在他的课程中使用的模型类。我编辑了上面的问题并添加了一个 GET 调用作为对我想在用餐时显示的内容的响应。我的计划是只使用 Blazor 将与用户相关的内容(身份验证、谁登录等)传输到视图,然后在 Angular 组件中使用 Observables 来获取我需要的其他实体所需的数据。 @克拉姆西

标签: c# asp.net-mvc .net-core asp.net-apicontroller


【解决方案1】:

创建配料类列表并将膳食模型类用作多对多用途。

public class Meal {
    public int Id { get; set; }
    public string Name { get; set; }
    public int SingularPrice { get; set; }
    public string Description { get; set; }
    public int Discount { get; set; }
    public List<Ingredient > IngredientData { get; set; }       
}

 public class Ingredient {
    public int Id { get; set; }
    public string Name { get; set; }
    public string Distributor { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 2015-05-27
    • 1970-01-01
    • 2021-05-21
    • 2019-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多