简体   繁体   English

在类内部从数据库存储/检索数据时的最佳实践

[英]Best practice when storing/retrieving data from database inside class

I would like to know what's the best code design when storing and retrieving data from a database when working with objects and classes. 我想知道在处理对象和类时从数据库存储和检索数据时最好的代码设计是什么。

I could do this in two ways. 我可以通过两种方式做到这一点。

In the class constructur I query the database and stores all info in instance variables inside the class and retrieve them with getters/setters. 在类构造器中,我查询数据库并将所有信息存储在类内的实例变量中,并使用getter / setter检索它们。 This way I can always get any information I want, but in many cases wont be needing all the information all the time. 这样,我始终可以获得所需的任何信息,但是在许多情况下,并不需要一直都在使用所有信息。

public class Group {
    public int GroupID { get; private set; }
    public string Name { get; private set; }
    public Group(int groupID)
    {
        this.GroupID = groupID;
        this.Name = // retrieve data from database
    }
    public string getName()
    {
        // this is just an example method, I know I can retrieve the name from the getter :)
        return Name;
    }
}

The other way is to create some methods and pass in the groupID as a parameter, and then query the database for that specific information I need. 另一种方法是创建一些方法,并将groupID作为参数传递,然后在数据库中查询我需要的特定信息。 This could result in more querys but I will only get the information I need. 这可能会导致更多查询,但我只会得到我需要的信息。

public class Group {    
    public Group()
    {
    }
    public string getName(int groupID)
    {
        // query database for the name based on groupID
        return name;
    }
}

What do you think is the best way to go? 您认为最好的方法是什么? Is there a best practice to go with here or is it up to me what I think works the best? 这里是否有最佳实践,还是我认为最有效的方法取决于我?

You don't want to do heavy DB work in the constructor. 您不想在构造函数中进行繁重的数据库工作。 Heavy work should be done in methods. 应在方法上进行繁重的工作。

You also don't want to necessarily couple the DB work with the entity class that holds the data. 您也不想将数据库工作与保存数据的实体类耦合在一起。 What if you want a method to return two of those objects from the database? 如果您想要一个方法从数据库中返回其中两个对象怎么办? For example GetGroups() - you can't even construct one without doing DB work. 例如GetGroups()-您甚至不做数据库工作就无法构造一个。 For something that returns multiple, the storage and retrieval is decouple from the entity class. 对于返回多个值的事物,存储和检索与实体类分离。

Instead, decouple your DB work from your entity objects. 相反,应将数据库工作与实体对象分离。 One option is you can have a dataaccesslayer with methods like GetFoo or GetFoos etc... that query the database, populate the objects and return them. 一种选择是,您可以拥有一个数据访问层,其中包含GetFoo或GetFoos等方法,用于查询数据库,填充对象并返回它们。

If you use an ORM, see: 如果您使用ORM,请参阅:

https://stackoverflow.com/questions/3505/what-are-your-favorite-net-object-relational-mappers-orm https://stackoverflow.com/questions/3505/what-are-your-favorite-net-object-relational-mappers-orm

Lazy loading versus early loading, which is what this really boils down to, is best determined by usage. 真正归结为延迟加载与早期加载的最佳方式取决于使用情况。

Mostly this means related entities -- if you are dealing with an individual address for instance, splitting the read for the city from the read for the state would be crazy; 通常,这意味着相关的实体-例如,如果您要处理一个单独的地址,则将城市的读取内容与州的读取内容分开会很疯狂; OTOH when returning a list of company employee's reading their address information is probably a waste of time and memory. OTOH返回公司员工正在阅读其地址信息的列表时,可能会浪费时间和内存。

Also, these aren't mutually exclusive options -- you can have a constructor that calls the databases, and a constructor that uses provided data. 而且,这些也不是互斥的选项-您可以有一个调用数据库的构造函数和一个使用提供的数据的构造函数。

If it is a relational database the best way would be to do it with ORM (object-relational mapping). 如果它是一个关系数据库,最好的方法是使用ORM(对象-关系映射)进行操作。 See here for a list of ORM-mappers: 请参阅此处以获取ORM映射器的列表:

https://en.wikipedia.org/wiki/List_of_object-relational_mapping_software https://zh.wikipedia.org/wiki/List_of_object-relational_mapping_software

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

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