使用c#为存储库模式准备通用数据访问层 [英] Preparing a generic data access layer for Repository Pattern using c#

查看:90
本文介绍了使用c#为存储库模式准备通用数据访问层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好b
$ b我试图在不使用EF的情况下自行设计存储库模式。我知道EF解决了很多这些问题,但是为了学习我自己就是这样做的。我开始为它创建interfaces.classes。



首先我有一个存储库接口,定义需要对db执行哪些操作。它看起来如下。

接口IRepository()
{
IList< T>得到所有();
IList< T> GetAll(string [] include);
IList< T>得到所有();

bool Add(tentity);
bool删除(Tentity);
bool更新(T实体);
bool IsValid(T实体);
}



另一个与open,close,getConnectionString等连接相关的接口。

接口IDbConnectionManagerHelper()
{
public void close();
public void open();

}



返回特定ProviderFactory实例的提供者工厂

 class DataBaseProviderFactory()
{
DataBaseProviderFactory(DbType dbType)
{
switch (dbType)
{
case dbType.Oracle:
返回新的OracleDataBasePRoviderFactory();
case dbType.SqlServer:
返回new SqlServerDataBasePRoviderFactory();
case dbType.MySql:
返回新的OracleDataBasePRoviderFactory();
}
}

}


我应该声明DataProviderManager调用DataBaseProviderFactory并获取特定类型的连接并将其传递到实现IRepository的类以查询实际数据库?


 class DataProviderManager()

{

//步骤1

DataBaseProviderFactory DataBaseProviderFactory = new DataBaseProviderFactory(dbType.SqlServer);

Step2

//这里我应该将databaseproviderfactory传递给IDbConnectionManagerHelper的派生类来获取连接吗?

//步骤3

我应该在这里将连接管理器从步骤2传递给衍生的IRepository类吗?

}


现在我的特定DataBaseProviderFactory应该像SqlServer,Oracle或mySql一样实现IDbConnectionHelper ?如果是这样我是否需要为实现IRepository的特定数据库提供另一个接口来查询实际的
数据库?





任何人都可以让我知道我的设计中应该有哪些接口/方法,以便我可以在我的WPF客户端应用程序中开始实现它们吗?


我们非常感谢任何帮助。


  &NBSP; &NBSP;     

解决方案

首先我有一个存储库接口,用于定义需要执行哪些操作D b。它看起来如下所示。


http://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx


< copied>


存储库位于更高级别。它也处理数据并隐藏查询和所有这些,但是,存储库处理**业务/域对象**。这就是区别。存储库将使用DAO从存储中获取数据,并将该数据用于
还原业务对象。或者它将采用业务对象并提取将持久化的数据。如果您有贫血域,则存储库将只是一个DAO。此外,在处理查询内容时,大多数情况下专门的查询存储库
只是将DTO发送到更高层的DAO。


< end>


https://en.wikipedia.org/wiki/Data_transfer_object


Hi

I am trying to design a repository pattern on my own without using EF. I know EF solves lot of these problems but for learning i am doing this on my own.I started creating interfaces.classes for the same.

First i have a repository interface which defines what operations needs to be done against db. And it looks like below.

Interface IRepository()
{
    IList<T> GetAll();
    IList<T> GetAll(string[] include);
    IList<T> GetAll();

    bool Add(tentity);
    bool Delete(Tentity);
    bool Update(T entity);
    bool IsValid(T entity);
}


Another interface related to connection like open, close , getConnectionString.

Interface IDbConnectionManagerHelper()
{
public void close();
        public void open();

}


A provider factory which returns instance of specific ProviderFactory

class DataBaseProviderFactory()
{
DataBaseProviderFactory(DbType dbType)
{
  switch(dbType)
   {
     case dbType.Oracle:
           return new OracleDataBasePRoviderFactory();
     case dbType.SqlServer:
           return new SqlServerDataBasePRoviderFactory();
    case dbType.MySql:
           return new OracleDataBasePRoviderFactory();
   } 
}

}

Should i declare DataProviderManager which calls DataBaseProviderFactory and get connection for specific type and pass that onto the class which implements IRepository to query the actual database ?

class DataProviderManager()

{

//Step 1 

 DataBaseProviderFactory DataBaseProviderFactory = new DataBaseProviderFactory(dbType.SqlServer);

Step2 

// here should i pass databaseproviderfactory to derived classes of IDbConnectionManagerHelper to get the connection ?

//Step 3

Should i here pass the connectionmanager from Step2 to the dervied classes of IRepository ?

}

Now should my specific DataBaseProviderFactory like SqlServer,Oracle or mySql should implement IDbConnectionHelper  ? If so Do i need another interface for specific Database which implement IRepository which queries actual databases?


Can anyone let me know what interfaces/methods i should have in my design more so that i can start implementing them in my WPF client application ?

Any help would be really appreciated.

          

解决方案

First i have a repository interface which defines what operations needs to be done against db. And it looks like below.

http://blog.sapiensworks.com/post/2012/11/01/Repository-vs-DAO.aspx

<copied>

A repository sits at a higher level. It deals with data too and hides queries and all that but, a repository deals with** business/domain objects**. That’s the difference. A repository will use a DAO to get the data from the storage and uses that data to restore a business object. Or it will take a business object and extract the data that will be persisted. If you have an anemic domain, the repository will be just a DAO. Also when dealing with querying stuff, most of the time a specialized query repository is just a DAO sending DTOs to a higher layer.

<end>

https://en.wikipedia.org/wiki/Data_transfer_object


这篇关于使用c#为存储库模式准备通用数据访问层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆