如何使用存储库模式和工厂方法模式组织我的类/接口?
我正在创建一个示例应用程序来一起理解存储库和工厂方法模式,因为将在更大的项目中使用。
我想要实现的是能够使网站与不同的 ORM 工具一起工作。
例如,网站将实现 LINQ to SQL 和 Ado 实体框架工作类,然后使用工厂方法将使用这些 ORM 之一“使用配置值”来加载存储库对象中的数据。
到目前为止我得到的内容如下
interface IRepository : IDisposable
{
IQueryable GetAll();
}
interface ICustomer : IRepository
{
}
public class CustomerLINQRepository : ICustomer
{
public IQueryable GetAll()
{
// get all implementation using linqToSql
}
public void Dispose()
{
throw;
}
public IRepository GetObject()
{
return this;
}
}
public class CustomerADORepository : ICustomer
{
public IQueryable GetAll()
{
// get all implementation using ADO
}
public void Dispose()
{
throw new NotImplementedException();
}
public IRepository GetObject()
{
return this;
}
}
// Filling a grid with data in a page
IRepository customers = GetCustomerObject();
this.GridView1.DataSource = customers.GetAll();
this.GridView1.DataBind();
////
public IRepository GetCustomerObject()
{
return new CustomerLINQRepository(); // this will return object based on a config value later
}
但我可以感觉到有很多设计错误希望你能帮助我找出来以获得更好的设计。
I am creating a sample, application to understand repository and factory method patterns together, because will use in a bigger project.
What i want to achieve is be able to make the website work with different ORM tools.
For example the website will have LINQ to SQL and Ado entity frame work classes implemented then using the factory method will use one of these ORMs "using config value" to load the data in the repository objects.
What i got till now is like the following
interface IRepository : IDisposable
{
IQueryable GetAll();
}
interface ICustomer : IRepository
{
}
public class CustomerLINQRepository : ICustomer
{
public IQueryable GetAll()
{
// get all implementation using linqToSql
}
public void Dispose()
{
throw;
}
public IRepository GetObject()
{
return this;
}
}
public class CustomerADORepository : ICustomer
{
public IQueryable GetAll()
{
// get all implementation using ADO
}
public void Dispose()
{
throw new NotImplementedException();
}
public IRepository GetObject()
{
return this;
}
}
// Filling a grid with data in a page
IRepository customers = GetCustomerObject();
this.GridView1.DataSource = customers.GetAll();
this.GridView1.DataBind();
////
public IRepository GetCustomerObject()
{
return new CustomerLINQRepository(); // this will return object based on a config value later
}
But i can feel that there are a lot of design mistakes hope you can help me figure it out to get better design.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我的两点意见:
A.我会添加通用基础存储库类。无论类型是什么,许多存储库操作都是相同的。它可以节省您大量的打字时间。
B. 我不明白为什么您的存储库要实现 ICustomer 接口。
数据对象的接口是一个很好的实践,但我认为您的存储库不应该实现它。
C. 如果您的数据对象有一个通用的实现,我将为它们创建一个基类,并限制存储库仅适用于该类型的派生类。
我会做类似的事情:
用法:
这就是我使用 NHibernate 实现 DAL 的方式。您可以在“NHibernate in Action”中找到该用法的一些变化。
我还建议使用某种 IoC 控制器,正如 Matt 建议的那样。
My two cents:
A. I would add generic base repository class. a lot of the repository actions will be the same no matter what the type is. it can save you lots of typing.
B. I don't understand why your repositories are implementing the ICustomer interface.
an interface for your Data Objects is a good practice but I don't think that your repository should implement it.
C. If there is a common implementation to your Data Objects, I would create a base class for them and restrict the repositories to work only with derived classes of the type.
I would do something like that:
Usage:
That is the way that I implemented my DAL Using NHibernate. you can find some twists of that usage in "NHibernate in Action".
I would also recommend using some kind of IoC controller as Matt suggested.
我不确定
GetWanted
的命名。你到底想要得到什么?名称应该更具描述性,也许是GetCustomerRepository
?GetCustomerObject
与GetWanted
相同吗?抛出
有什么用?I'm not sure on the naming of
GetWanted
. What exactly is wanted that you are getting? The name should be more descriptive, maybeGetCustomerRepository
?Is
GetCustomerObject
the same asGetWanted
?What is the
throw
for?大部分看起来都不错。我有两点意见:
Looks good for the most part. I have two comments: