Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8279953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:29:48+00:00 2026-06-08T09:29:48+00:00

I am trying to build a class that interacts with the database in my

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:29:50+00:00Added an answer on June 8, 2026 at 9:29 am

    Use Automatic proerties because your private fields does the same in your code.

    I think, Save is an Operation which can be done on an object of Person Entity. So i wont keep it as a Static method. I would move your Save code as a method of the Person object. So that i will call it like obj.Save(). To load the data, I would use an overloaded version of my class constructor.

    public class Person
    {
        int personId;      
    
        public int PersonId
        {
            get { return personId; }
        }    
        public string Name { set;get;}   
        public string LastName { set;get;}        
    
        public Person() {}
    
        public Person(int person_id)
        {
            //call to datalayer here
            //get the person from database and return a person class          
            personId = 10;
            Name= "Alex";  // set the public property value here           
        }
        public bool Save()
        {
            //datalayer here
            //save the person class and return
          // true/false /or new ID (change return type)
        }    
    
    }
    

    And when calling,

    Person p = new Person(19);  //get existing person
    p.Name = "New Name";
    p.Save();
    

    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

    public class Person
    {
      public int ID { set;get;}
      public string Name { set;get;}
    }
    

    And have a Repository which does the data operations for you. So your repository may have methods like this

    public interface IRepository
    { 
       Person GetPerson(int id);
       bool SavePerson(Person person);
    }
    

    You may implement this Interface in a class to do your Data Access operations

    public class Repository:IRepository
    {
      //implementation of your DA methods here
    }
    

    Now you may call it from different layer(business layer) like this

    IRepository repo = new Repository();
    
    var person=repo.GetPerson(19);  
    person.Name="Updated Name";
    repo.Save(person);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to build an ASP.NET 3.5 website that allows users to log
I'm trying to build a utility class that I can re-use, that converts a
I am trying to build a php class that will let me include other
I'm trying to build a small class-like container that will make it a little
I'm trying to build the proxy class for a web service using the wsdl
Here's what I am trying to do: Build a custom control that essentially is
I'm trying to build a static class called Logger that will upload the log
I'm trying to understand the Repository Pattern , while developing an ASP.NET MVC application
I am trying to build a class that does many photo operations, one method
I am trying to build a cross platform (Mac or iOS) class that needs

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.