tags:

views:

200

answers:

2
+2  Q: 

getter , setter c#

Hi i have got various custom datatypes in my web application to map some data from the database.

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

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)

Thanks

+2  A: 

I don't think that you should put it in your Person class since it accesses the database and 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.

klausbyskov
+2  A: 

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.

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. This way, any page requiring person information would simply call the PersonService; and you can concentrate your page code on GUI related code.

Etienne