简体   繁体   English

MVC模型的C#最佳实践

[英]C# Best Practice With MVC Models

Just looking for some guidance on best practices here. 只是在这里寻找最佳实践的指导。 I have an order manifest for a product order system, which looks like the following:- 我有一个产品订单系统的订单清单,如下所示:-

public class CurrentOrderManifestViewModel
{
    public int OrderID { get; set; }
    public List<CurrentOrderItem> CurrentOrderItems { get; set; }
    public decimal? MinimumOrderSurcharge { get; set; }
    public decimal? OrderSurcharge { get; set; }
}

public class CurrentOrderItem
{
    public int OrderProductID { get; set; }
    public string ProductDescription { get; set; }
    public string ProductCode { get; set; }
    public decimal? UnitPrice { get; set; }
    public int Quantity { get; set; }
    public decimal? VATRate { get; set; }
    public decimal? VATValue { get; set; }
    public decimal? GoodsValue { get; set; }
    public bool IsOnPromotion { get; set; }
}

I am now in a different area of the application to where this was originally created for and I find myself with very similar properties in the same format. 我现在位于应用程序的最初创建区域之外的其他区域,我发现自己在相同格式下具有非常相似的属性。 But all of the fields above to do with prices are not going to be filled, as I am looking purely at product names and descriptions. 但是以上所有与价格有关的领域都不会被填充,因为我纯粹是在看产品名称和描述。

Should I create a different model with just the properties I need or use the same model and have lots of NULL values in the list of order items? 我是否应该仅使用我需要的属性来创建其他模型,还是使用同一模型并且在订单项列表中有很多NULL值?

Thanks! 谢谢!

I am now in a different area of the application to where this was originally created for and I find myself with very similar properties in the same format. 我现在位于应用程序的最初创建区域之外的其他区域,我发现自己在相同格式下具有非常相似的属性。 But all of the fields above to do with prices are not going to be filled, as I am looking purely at product names and descriptions. 但是以上所有与价格有关的领域都不会被填充,因为我纯粹是在看产品名称和描述。

The DRY principle is not "never write code twice", but rather that " every piece of knowledge must have a single, unambiguous, authoritative representation in the system ". DRY原则不是“永远不会两次编写代码”,而是“ 每条知识在系统中都必须具有唯一,明确,权威的表示形式 ”。

Just because two areas of an application have similar properties does not, by itself, mean that they should share a class. 仅仅因为应用程序的两个区域具有相似的属性,并不意味着它们应该共享一个类。 Anytime the business wants to add or modify a field to one area, it will affect the other, and you will always have to remember that . 无论何时企业要在一个区域中添加或修改字段,都会影响另一个区域,因此您始终必须记住这一点 Sure they have the same properties now , but will they be the same forever ? 当然它们现在具有相同的属性,但是它们会永远相同吗?

Should I create a different model with just the properties I need or use the same model and have lots of NULL values in the list of order items? 我是否应该仅使用我需要的属性来创建其他模型,还是使用同一模型并且在订单项列表中有很多NULL值?

I don't recommend using the same model for your case, from my understanding of it. 从我的理解来看,我不建议为您的案例使用相同的模型。 It's going to make your life easier down the road if you build a model that has a meaningful name, contains properties that it actually has, and can be changed as needed later without having to figure out where else in the application it's used. 如果您构建的模型具有有意义的名称,包含其实际拥有的属性,并且以后可以根据需要进行更改而不必弄清楚它在应用程序中的其他位置,那么这将使您的生活更轻松。 I'm certain you could also find a name that would better represent this area of the application. 我敢肯定,您还可以找到一个可以更好地代表应用程序这一领域的名称。

To put it a different way, it would be a bad idea to use CurrentOrderItem in a place where it isn't actually an item for a current order. CurrentOrderItem在实际上不是当前订单商品的地方使用CurrentOrderItem是一个坏主意。

It's not correct or incorrect. 这是不正确或不正确的。 A ViewModel should have all the information required to render the view, which is the case. 一个ViewModel应该具有呈现视图所需的所有信息,这种情况就是这种情况。

If the other properties were two heavy, there could be a performance reason to create a custom object for this purpose. 如果其他两个属性很重,则可能出于性能原因而为此目的创建自定义对象。 However, int this case the properties are simple, and that's not a concern. 但是,在这种情况下,属性很简单,这不是问题。

Which is the concern them? 他们关心的是什么? That someone who maintains your code in the future knows clearly that some properties of those objects are not filled wit data, but left null. 将来会维护您的代码的人清楚地知道这些对象的某些属性未填充机密数据,而是保留为空。

Obviously, if you create a class without those properties, noone can make a mistake thinking that those variables are set. 显然,如果您创建的类没有这些属性,那么没有人会认为设置了这些变量会出错。

The other options is to give the List a very clear name, and comment it so that someone later will understand that fact, for example: 其他选择是给List一个非常清楚的名称,并对其进行注释,以便以后有人可以理解该事实,例如:

///<summary>
/// This list includes the order items with all the price properties
/// set to null.
///</summary>
public List<CurrentOrderItem> CurrentOrderItemsWithoutPrices { get; set; }

So the decision has to do with a balance between writing more code (create a new class without that properties, do some mapping, manual, or automatic) or rely on good property naming and commenting. 因此,决策必须在编写更多代码(创建没有该属性的新类,进行一些映射,手动或自动)或依靠良好的属性命名和注释之间取得平衡。 In this particular case I think that the intention of the view model is extremely clear, and a simple comment can make it even more clear for future maintenance, so you can spare writing additional code (that would have to be maintained). 在这种特殊情况下,我认为视图模型的意图非常明确,并且简单的注释可以使它对于将来的维护更加清晰,因此您可以省去编写其他代码(必须维护)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM