I am trying to build a class that interacts with the database in my asp.net web application. I need your opinion on how to design it, here’s an example of what I have in mind
public class Person
{
int personId;
string name;
string lastName;
public int PersonId
{
get { return personId; }
}
public string Name
{
get { return name; }
set { name = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public Person()
{
}
public static void Save(Person p)
{
//datalayer here
//save the person class
}
public static Person GetPerson(int person_id)
{
//datalayer here
//get the person from database and return a person class
Person p = new Person();
p.personId = 10;
p.name = "Alex";
return p;
}
}
So that i can use the database methods without having to instantiate the class:
Person p = Person.GetPerson(19);
p.Name = "Alex...";
Person.Save(p);
I appreciate your help.
Use
Automaticproerties because your private fields does the same in your code.I think,
Saveis an Operation which can be done on an object ofPersonEntity. So i wont keep it as a Static method. I would move yourSavecode as a method of thePersonobject. So that i will call it likeobj.Save(). To load the data, I would use an overloaded version of my classconstructor.And when calling,
EDIT : Another (better) approach is to keep your entity classes as simple POCO’s. that means no data acccess / BL code there. It will simply look like
And have a
Repositorywhich does the data operations for you. So your repository may have methods like thisYou may implement this
Interfacein a class to do your Data Access operationsNow you may call it from different layer(business layer) like this