关于我的 DAL 层的建议,有什么明显的问题吗?

发布于 2024-08-13 22:30:36 字数 804 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吻泪 2024-08-20 22:30:36

您可能会考虑使用 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 method Load is a bit confusing - why not call it CreateDAO instead?

半透明的墙 2024-08-20 22:30:36

您能够交换 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文