关于我的 DAL 层的建议,有什么明显的问题吗?
我正在使用 nHibernate。 我的类是 POCO,它与我的数据库表 1:1 映射。 我创建了一个 IGenericDAO -> GenericDAO 可以完成我所有的基本 CRUD。 (存储库) 每个表都有一个 DAO 类,因此:
public class UserDAO : GenericDAO
任何特定于表的查询都将进入 tableDAO 类。
然后我就有了一个工厂,IDAOFactory。
public class NHibernateDAOFactory : IDAOFactory
{
public static UserDAO GetUserDAO()
{
return new UserDAO();
}
}
可能的改进: 1. 为了让我的 Web 应用程序不乱七八糟:
IDAOFactory dbFactory = new NHibernateDAOFactory();
我正在考虑创建:
public class DAOFactoryFactory
{
public static IDAOFactory Load()
{
return new NHibernateDAOFactory();
}
}
这样我就可以有一个单点更改,以防我需要交换数据库层。 (即在 linq2sql 和 nhibernate 之间切换,但不确定这实际上是否可能)
有关如何改进这一点的任何建议/指针,或者这是否非常可靠?
I am using nHibernate.
My classes are POCO, that map 1:1 with my database tables.
I created a IGenericDAO -> GenericDAO that does all my basic CRUD. (repository)
Each table has a DAO class, so:
public class UserDAO : GenericDAO
Any table specific queries will go in the tableDAO class.
I then have a factory, IDAOFactory.
public class NHibernateDAOFactory : IDAOFactory
{
public static UserDAO GetUserDAO()
{
return new UserDAO();
}
}
Possible improvement:
1. Just so my web application isn't littered with:
IDAOFactory dbFactory = new NHibernateDAOFactory();
I was thinking of creating:
public class DAOFactoryFactory
{
public static IDAOFactory Load()
{
return new NHibernateDAOFactory();
}
}
This way I have a single point of change in case I need to swap database layers. (i.e. switch between linq2sql and nhibernate, but not sure that is possible in reality)
Any suggestions/pointers on how to improve this, or is this pretty much solid?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能会考虑使用 DI 容器,而不是希望只在一个地方
新建
工厂来保护您免受进一步的更改。另外,调用工厂方法Load
有点令人困惑 - 为什么不调用它CreateDAO
呢?You might consider using a DI container instead of hoping that having only one place where you
new
the factory protects you from further changes. Also, calling a factory methodLoad
is a bit confusing - why not call itCreateDAO
instead?您能够交换 ORM 实现的机会几乎为零。我会使用 IoC 容器(StructureMap)。为您的 Dao 定义接口,然后通过 ioc 容器将它们注入到您的服务中。
Chances that you are going to be able to swap ORM implementations are slim to none. I would use IoC container (StructureMap). Define interfaces for your Dao's and then inject them in your services via ioc container.