简体   繁体   English

C#在对象中加载初始数据的位置?

[英]C# Where to load initial data in an object?

Similar to this question: C# Constructor Design but this question is slight different. 与此问题类似: C#Constructor Design但这个问题略有不同。

I have a class Customer and a class CustomerManager. 我有一个Customer类和一个CustomerManager类。 When an instance is created of the CustomerManager class I want to load all the customers. 当创建CustomerManager类的实例时,我想加载所有客户。 And this is where I got stuck. 这就是我陷入困境的地方。 I can do this several ways: 我可以通过以下几种方式做到:

  1. Load all the customers in the constructor (I don't like this one because it can take a while if I have many customers) 在构造函数中加载所有客户(我不喜欢这个,因为如果我有很多客户可能需要一段时间)
  2. In every method of the CustomerManager class that performs database related actions, check the local list of customers is loaded and if not, load the list: 在执行数据库相关操作的CustomerManager类的每个方法中,检查加载的本地客户列表,如果没有,请加载列表:

     public method FindCustomer(int id) { if(_customers == null) // some code which will load the customers list } 
  3. Create a method which loads all the customers. 创建一个加载所有客户的方法。 This method must be called before calling methods which performs database related actions: 必须在调用执行数据库相关操作的方法之前调用此方法:

    In the class: 在课堂里:

     public LoadData() { // some code which will load the customers list } 

    In the form: 形式如下:

     CustomerManager manager = new CustomerManager(); manager.LoadData(); Customer customer = manager.FindCustomer(int id); 

What is the best way to do this? 做这个的最好方式是什么?

EDIT: 编辑:

I have the feeling that I am misunderstood here. 我觉得我在这里被误解了。 Maybe it is because I wasn't clear enough. 也许是因为我不够清楚。 In the CustomerManager class I have several methods which depends on the local list (_customers). 在CustomerManager类中,我有几种方法取决于本地列表(_customers)。 So, my question is, where should I fill that list? 所以,我的问题是,我应该在哪里填写该清单?

What you are describing is "lazy loading". 你所描述的是“延迟加载”。

A simple approach is to have a private property like this: 一个简单的方法是拥有这样的私有财产:

private Lixt<Customer> _customers;
private List<Customer> Customers
{
  get
  {
    if(_customers == null)
      _customers = LoadData();
    return _customers;
  }
}

Then, you refer to Customers internally. 然后,您在内部引用Customers The customers will be loaded the first time they are needed but no earlier. 客户将在第一次需要时加载,但不会更早。

This is such a common pattern that .Net 4.0 added a Lazy<T> class that does this for you. 这是一种常见的模式,.Net 4.0添加了一个Lazy<T>类,为您完成此操作。

I that case, you just define it as a private like this: 在这种情况下,您只需将其定义为私有,如下所示:

private Lazy<List<Customer>> _customers = new Lazy<List<Customer>>(LoadData);

Then, you simply refer to your customers in code: 然后,您只需在代码中引用您的客户:

_customers.Value

The class will initialize the value with your LoadData() method. 该类将使用您的LoadData()方法初始化该值。

If you are not on .Net 4.0 yet, the Lazy<T> class is very easy to implement. 如果你还没有使用.Net 4.0,那么Lazy<T>类很容易实现。

Use a property for accessing the customers. 使用属性访问客户。 Have that check if the customers are loaded. 检查客户是否已加载。

Well, it depends. 这要看情况。 All your options have advantages and disadvantages. 您的所有选择都有优点和缺点。

The good thing about options 1 and 3 is that the user has full control over when the (lengthy) data loading operation is performed. 选项1和3的好处是用户可以完全控制何时执行(冗长的)数据加载操作。 Whether option 1 or 3 is better depends on whether it makes sense to create the Manager and load the data later or not. 选项1或3是否更好取决于创建Manager并在以后加载数据是否有意义。 Personally, I prefer a separate LoadData method if it's a lengthy operation, but that might be a matter of taste. 就个人而言,如果这是一个冗长的操作,我更喜欢单独的LoadData方法,但这可能是一个品味问题。

The good thing about option 2 is that the data will not be loaded if it is not needed. 选项2的好处是如果不需要,将不会加载数据。 The drawback is that the (lengthy) load occurs as a side-affect of the first access, which makes your program "less deterministic". 缺点是(冗长)负载作为第一次访问的副作用发生,这使您的程序“不那么确定”。

In principle, all the options you have presented are fine and valid choices. 原则上,您提供的所有选项都是精确有效的选择。 It really depends on your requirements. 这实际上取决于您的要求。

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

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