C# C中的Oracle数据库和存储库模式

C# C中的Oracle数据库和存储库模式,c#,oracle,C#,Oracle,我已经在oracle中创建了数据库。我通常使用以下代码连接到应用程序中的数据库: OracleConnectionStringBuilder sb = new OracleConnectionStringBuilder(); sb.DataSource = "localhost"; sb.UserID = "user"; sb.Password = "password"; OracleConnection conn = new OracleConnection(s

我已经在oracle中创建了数据库。我通常使用以下代码连接到应用程序中的数据库:

OracleConnectionStringBuilder sb = new OracleConnectionStringBuilder();
    sb.DataSource = "localhost";
    sb.UserID = "user";
    sb.Password = "password";
    OracleConnection conn = new OracleConnection(sb.ToString());
    conn.Open();
我还使用此代码执行任何语句:

OracleDataAdapter sd = new OracleDataAdapter("select, insert etc.", conn);
假设我的数据库中有2个表,1个。人,2。地方。现在我想为餐桌上的人创建存储库

那么,实现这一目标的步骤是什么,如果我错了,请纠正我

我创建了一个采用表属性的类:

    public class People
{        
    public int ID { get; set; }
    public string first_name { get; set; }
    public string last_name { get; set; }
}
然后,我创建接口:

    public interface IPeople
{
    void Insert(People person);
    void Update(People person);
    void Delete(People person);
    People GetById(People person);
    IEnumerable<People> SelectAll();
}
我不知道下一步是什么以及如何使用它。我很困惑,我在哪里连接到我的数据库?
很抱歉,也许我问了一些愚蠢而简单的问题,但我用谷歌搜索了存储库模式,我仍然不知道如何实现和正确使用它。谢谢您的帮助。

您可以这样实现它,但这种方式不太好。我试着对你必须做的事情发表意见。正确使用类和属性的名称,若有问题,可以使用它,这是一个非常有用的工具。正如其他人所写,更好的想法是使用一些ORM,如EntityFramework或NHibernate


底层的RDBMS在如何实现存储库模式方面没有任何区别。但是,不要从头开始构建它,而要有效地使用ORM,我想说的是,人应该是Person,iOple应该是PersonRepository,并且您肯定应该开始遵循.NET命名约定,例如FirstName而不是first_name。除此之外,如何实现存储库模式对于堆栈溢出来说是一个非常广泛的问题。听起来您可能应该在您提到的每个方法中创建/打开/使用/关闭连接。。。如果您确实需要自己实现它而不是使用ORM,那么使用而不是最终尝试如何?实际上,在函数的末尾处理连接而不在函数的开头创建连接看起来像是一种反操作-pattern@grek40基本上它给出了相同的结果,是的,基本上我评论的两句话是针对不同的关注点。第一个是关于使用,第二个是关于Dispose对连续两次调用GetById的不良影响,因为重新打开disposed连接可能会有问题。我在People GetByIdPeople person中有一个错误,不是所有代码路径都返回值。@ktos1234您需要返回collection person。
 //change class name and property names , You need to use coding conventions 
public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

//this is a person repository interface
public interface IPersonRepository
{
    void Insert(People person);
    void Update(People person);
    void Delete(People person);
    People GetById(People person);
    IEnumerable<People> SelectAll();
}

//this class implement your interface
class PersonRepository : IPersonRepository
{
    private OracleConnection conn;

    //add constructor which create connection to DB
    public PersonRepository()
    {
        OracleConnectionStringBuilder sb = new OracleConnectionStringBuilder();
        sb.DataSource = "localhost";
        sb.UserID = "user";
        sb.Password = "password";
        conn = new OracleConnection(sb.ToString());
    }


    public void Delete(Person person)
    {
        throw new NotImplementedException();
    }

    public Person GetById(Person person)
    {
        //Use try finally block to open and close connection
        try
        {
            conn.Open();
            //example select by id
            OracleDataAdapter sd = new OracleDataAdapter("SELECT * FROM PERSON WHERE PERSON.Id == " + person.Id,
                conn);
            return new Person();
        }
        finally
        {
            conn.Dispose();
        }
    }

    public void Insert(Person person)
    {
        throw new NotImplementedException();
    }

    public IEnumerable<Person> SelectAll()
    {
        throw new NotImplementedException();
    }

    public void Update(Person person)
    {
        throw new NotImplementedException();
    }
}