简体   繁体   English

getter,setter C#

[英]getter , setter c#

i have got various custom datatypes in my web application to map some data from the database. 我的Web应用程序中有各种自定义数据类型,可以映射数据库中的某些数据。

something like: 就像是:

Person
Id
Name
Surname

and i need a List of persons in most of my application's pages 我需要在应用程序的大多数页面中列出人员列表

i was thinking to create a getter property that gets the list of persons from the database and store into cache in this way i do not have to call the database each time 我正在考虑创建一个getter属性,该属性将从数据库中获取人员列表并以这种方式存储到缓存中,而不必每次都调用数据库

something Like (pseudo code) 类似于(伪代码)的东西

public List<Person> Persons 
{
   get { return if cache != null  return List of Persons from cache  else get from the database;}
}

Where shall i put this getter? 我该把吸气剂放在哪里? in my Person class definition or into my base page( page from which all the others pages inherit) 在我的Person类定义中或在我的基本页面中(所有其他页面都继承自该页面)

Thanks 谢谢

I don't think that you should put it in your Person class since it accesses the database and HttpContext.Current.Cache . 我不认为您应该将其放在Person类中,因为它访问数据库 HttpContext.Current.Cache Furthermore I think you should make it a method and not a property, to imply that this may be a "lengthy" operation. 此外,我认为您应该将其设为方法而不是属性,以暗示这可能是“冗长的”操作。 So, of the two options, I would put it on the base Page class. 因此,在这两个选项中,我将其放在基础Page类上。

I think putting it in your base page would be better option. 我认为将其放在您的基本页面中将是更好的选择。

Depending on your application architecture, putting process related code in your domain classes might be an issue. 根据您的应用程序体系结构,将与过程相关的代码放入您的域类中可能是一个问题。 Some use it in DDD (domain-driven design) type applications though. 但是有些人在DDD(域驱动设计)类型的应用程序中使用它。

Better even, I usually try to hide those implementation details in a service class. 更好的是,我通常尝试在服务类中隐藏那些实现细节。 You could have a PersonService class that would contain your above method and all person related operations. 您可能有一个PersonService类,其中包含您的上述方法以及所有与人相关的操作。 This way, any page requiring person information would simply call the PersonService ; 这样,任何需要人员信息的页面都可以简单地调用PersonService and you can concentrate your page code on GUI related code. 您可以将页面代码集中在与GUI相关的代码上。

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

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