将数据库表建模为类

Age*_*rum 0 .net c# database

我正在设计一个主要由数据库驱动的个人项目.我正在尝试为如何处理数据库互操作提出一个很好的设计,我想在StackOverflow这里有经验的人的输入.

以下是好的设计,还是有更标准的方式来处理与数据库的通信?

编辑
我主要是寻找关于是否通常将连接细节分解为他们自己的基类,以及逻辑单元/类的子类的反馈.
为此,我修改了下面的代码,添加了一个检索方法,以补充原始问题中的插入方法.我还修改了它以显示它需要/返回(取决于方法)业务对象.
例如,在示例中,我们在整个应用程序中使用Employee类,但是有一个EmployeeDb类(继承自Database),它处理与数据库之间的持久性.

我喜欢这个,因为它将存储实现细节保留在业务对象之外,但不喜欢它,因为它强烈地耦合了Employee和EmployeeDB类.

// Abstract Base Class to handle specifics of the database connection
abstract class Database : IDisposable  
{  
    protected OleDbConnection m_Conn;  

    public bool Open()  
    {  
        // Open a connection to the database  
    }  

    public void Dispose()  
    {  
        if (m_Conn != null)  
        {  
           m_Conn.Dispose();  
        }
    }  
}  

// Specific classes for each table, with methods for CRUD functions
class EmployeeDB : Database  
{  
    public bool AddTestData(Employee emp)  
    {  
        // Construct SQL to add Employee class members to the DB, breaking
        // them out into their component tables as needed  
    }  

    public List<Employee> GetEmployeeByProject(string project)  
    {  
        // Retrieve recordset of all employees on the project,
        // breaking them out into instances of the Employee class

        // Add each new Employee object to a list, and return the list
        // to the caller.
    }  
}  

// Specific classes for each table (or logical unit, since obviously 
// most of the time we'll need to join a few tables to get what 
// we want), with methods for CRUD functions
    void AddSomethingToTheDatabase()
    {
        using (TestDataDB td = new TestDataDB())
        {
            td.Open(Application.StartupPath);
            string NewID = td.AddTestData(txtAddMe.Text);
        }
    }
Run Code Online (Sandbox Code Playgroud)

// Abstract Base Class to handle specifics of the database connection
abstract class Database : IDisposable  
{  
    protected OleDbConnection m_Conn;  

    public bool Open()  
    {  
        // Open a connection to the database  
    }  

    public void Dispose()  
    {  
        if (m_Conn != null)  
        {  
           m_Conn.Dispose();  
        }
    }  
}  

// Specific classes for each table, with methods for CRUD functions
class EmployeeDB : Database  
{  
    public bool AddTestData(Employee emp)  
    {  
        // Construct SQL to add Employee class members to the DB, breaking
        // them out into their component tables as needed  
    }  

    public List<Employee> GetEmployeeByProject(string project)  
    {  
        // Retrieve recordset of all employees on the project,
        // breaking them out into instances of the Employee class

        // Add each new Employee object to a list, and return the list
        // to the caller.
    }  
}  

// Specific classes for each table (or logical unit, since obviously 
// most of the time we'll need to join a few tables to get what 
// we want), with methods for CRUD functions
    void AddSomethingToTheDatabase()
    {
        using (TestDataDB td = new TestDataDB())
        {
            td.Open(Application.StartupPath);
            string NewID = td.AddTestData(txtAddMe.Text);
        }
    }
Run Code Online (Sandbox Code Playgroud)

Rob*_*vey 7

您确定不想尝试对象关系映射器吗?

也许Linq to SQLnHibernate