简体   繁体   English

模型和部分模型,如何避免冗余代码?

[英]Model and partial model, how to avoid the redundant code?

I have a model and a partial model which contains only the properties that I need to expose in JSON. 我有一个模型和一个部分模型,它只包含我需要在JSON中公开的属性。

But the properties between the model and his partial model are redundant. 但是模型和他的部分模型之间的属性是多余的。

How can I avoid that or improve my approach? 我怎样才能避免这种情况或改进我的方法?

namespace Dashboard.Models.UserModels
{
    public class UserModel
    {
        public int id { get; set; }
        public string dbName { get; set; }
        public string firstname { get; set; }
        public string lastname { get; set; }
        public int idExternal { get; set; }
        public int idInstance { get; set; }
        public string login { get; set; }
        public string password { get; set; }
        public DateTime? dtContractStart { get; set; }
        public DateTime? dtContractEnd { get; set; }
        public string emailPro { get; set; }
        public string emailPerso { get; set; }

        public LuccaUserModel()
        {
            idInstance = -1;
        }

        // partial model for json result
        // not sure is the best way or have to be here
        public class PartialUserModel
        {
            public int id { get; set; }
            public string firstname { get; set; }
            public string lastname { get; set; }
            public string emailPro { get; set; }
            public string emailPerso { get; set; }
            public DateTime? dtContractStart { get; set; }
            public DateTime? dtContractEnd { get; set; }
            public string url { get; set; }
        }

        // UserModel Methods
    }
}

You can rename PartialUserModel UserModelBase class (or leave it as is... it just makes better logical sense to do so) and make UserModel to inherit from it: 您可以重命名PartialUserModel UserModelBase类(或保持原样......这样做更符合逻辑意义)并使UserModel继承它:

public class UserModel : UserModelBase
{
    ...
}

Of course you'll need to remove all duplicate properties from UserModel in this case. 当然,在这种情况下,您需要从UserModel中删除所有重复的属性。

It's a thin line between doing a proper design and building an overkill design. 在做正确设计和建立过度杀伤设计之间,这是一条细线。 Answer depends on many inputs, among which I chose to have project and model breadth most important. 答案取决于许多输入,其中我选择了最重要的项目和模型广度。

In hope to have my answer clearer, I have to say I use different terminology. 希望我的答案更清楚,我不得不说我使用不同的术语。 Data which is adopted for use in UI is usually called ViewModel. 在UI中使用的数据通常称为ViewModel。 In your case, you would build UserViewModel which contains necessary subset of information. 在您的情况下,您将构建包含必要信息子集的UserViewModel

If I'm working on a one-off project, I'll reuse model as a ViewModel. 如果我正在进行一次性项目,我将重用模型作为ViewModel。 I'll do this by having helper method which removes sensitive information, loads up or cuts off data which is lazy loaded from database and does other preparation on data. 我将通过帮助方法来删除敏感信息,加载或切断从数据库延迟加载的数据并对数据进行其他准备工作。 All this is done with same model class. 所有这些都是使用相同的模型类完成的。

If it's not a short term project, I look to create separate ViewModel classes which I map from model data. 如果它不是一个短期项目,我希望创建单独的ViewModel类,我从模型数据映射。 Then, if I'm working with mostly flat data I use AutoMapper tool to have data automatically copied, instead of writing my own mappers. 然后,如果我正在使用大多数平面数据,我使用AutoMapper工具自动复制数据,而不是编写自己的映射器。

As another answer here states, you write a basic class with data you need in UI and extend it with other model data, however this is not a good approach for several reasons. 正如另一个答案所述,您在UI中编写了一个包含所需数据的基本类,并将其与其他模型数据一起扩展,但由于多种原因,这不是一个好方法。

  1. If violates separation of concerns. 如果违反了关注点的分离。 Project dealing with model and persistance should not know about your ViewModel 处理模型和持久性的项目不应该知道您的ViewModel
  2. You may need to flatten data from related objects into ViewModel objects. 您可能需要将相关对象中的数据展平为ViewModel对象。 In that case, your model objects would have fields which should not be there, or would be redundant. 在这种情况下,您的模型对象将具有不应存在的字段,或者将是多余的。
  3. You may need calculated fields and helper methods in ViewModel which would again end up in model, confusing everyone that is not updated about design. 您可能需要ViewModel中的计算字段和辅助方法,这些方法和辅助方法将再次出现在模型中,使每个未更新设计的人感到困惑。
  4. You could want to adopt several unrelated model classes to same ViewModel class 您可能希望将几个不相关的模型类应用于同一个ViewModel类

To try and put it shortly, either reuse model class or create ViewModels. 要尽快尝试,请重用模型类或创建ViewModels。 There is unfortunately no clever solution. 遗憾的是没有聪明的解决方案。 If you find one, please post a comment as I'd like to hear about it :) 如果你找到一个,请发表评论,因为我想听听它:)

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

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