在我的情况下哪种方法更好? [英] Which method is better in my situation?

查看:107
本文介绍了在我的情况下哪种方法更好?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为GeneralClass的对象列表,它只有三个属性:Mathpoint,Englishpoint和要插入数据库的Name。如果名称不在数据库中的askTable中,我还必须将MusicPoint,地址,电话号码,性别和年龄插入数据库。我有两种方法可以解决这个问题。如果我有很多对象,并且askTable中的名字数量是一小部分(可能是30:1),哪个更好?

I have a List of objects called GeneralClass, which has only three properties: Mathpoint, Englishpoint, and Name to insert into the database. If the name is not in the the askedTable in database, I also must insert the MusicPoint, address, phonenumber, sex and age into the database. I have two ways to solve this problem. If I have many objects, and the count of name in askedTable is a small proportion (may be 30:1), which is better?

Method 1:define two models

 public class SimpleModel{
     public decimal Name{ get; set; }
     public decimal Mathpoint{ get; set; }
     public decimal Englishpoint{ get; set; }
 }
 public class ComplexModel{
     public decimal Name{ get; set; }
     public decimal Mathpoint{ get; set; }
     public decimal Englishpoint{ get; set; }
     public decimal? MusicPoint{ get; set; }
     public string Address{ get; set; }
     public decimal? PhoneNumber{ get; set; }
     public decimal? Age{ get; set; }
 }

 public void InserDB(List<SimpleModel> simpleModels)
 {
     foreach(var item in simpleModels){
     if(!checkiteminaskname(item))
         insert(item);
     else {
         //this method is change item to complexitem
         var complexitem=changeitem(item);
         insert(complexitem)
     }
 }

Method 2:define only complexmodel

//it means at first I only set the Mathpoint and Englishpoint,
//and set the other null
public void InserDB(List<ComplexModel> complexModels)
{
    foreach(var item in complexModels){
        if(checkiteminaskname(item))
        //fill the other properties
        {
            item.Address="abc";
            ...
            item.age=18;
        }
        insert(complexitem)
    }
} 



如果我使用complexModel,可能是我总是传递7参数但事实上我只需要3个paramer。如果我使用simplemodel,可能需要更改模型(事实上它据说创建了另一个模型)。什么更好?为什么使用IEnumerable而不是List?


If I use complexModel,may be I alway pass 7 parameter but in fact I only need 3 paramer.If I use simplemodel,may be I need to change the model(in fact it is said to create another model).What is better?Why do you use IEnumerable instead of List?

推荐答案

两种方式都不好。在第一种情况下,您重复名称 Mathpoint Englishpoint 。这不仅违反了OOP,这是整个编程的中提琴,其中一个主要原则是:不要重复自己。第二类应该来自第一类。请参阅: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself [ ^ ]。



第二堂课不用考虑。如果你需要一个简单的模型,不仅是复杂的模型,为什么放弃它。



最后,我不知道你是否要添加所有属性在编程期间,所有类都在编译时完成。如果您需要在运行时添加属性,则方法应该非常不同。我不相信这是这种情况,但最好提一下。



-SA
Both ways are bad. In first case, you repeat Name, Mathpoint, Englishpoint. This is not just the violation of OOP, this is violaton of whole programming, one of the main principles: "Don't Repeat Yourself". Second class should be derived from the first one. Please see: http://en.wikipedia.org/wiki/Don%27t_repeat_yourself[^].

Second class is needless to consider. If you reall need a simple model, not only complex one, why giving it up.

And finally, I don't know if you are going to add all properties during programming and have all classes finalized at compile time. If you want a need to add properties during runtime, the approaches should be very different. I don't believe this is the case, but it's just good to mention.

—SA


在我看来,如果你估计只有三分之一的类实例没有完整的参数集,你肯定应该使用完整的复杂类一组参数。我相信使用一个一致类类型的好处远远超过数据库中一些额外空字段的成本(内部内存使用,运行时性能)。
In my opinion, if you estimate that only one in thirty of the instances of your Class will not have the full set of parameters, you should definitely use the "complex" Class which has the full set of parameters. The benefits of working with one consistent Class Type will, I believe, far outweigh any "costs" (internal memory use, run-time performance) of some extra null fields in the DB.


这篇关于在我的情况下哪种方法更好?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆