为什么NHibernate中的SessionFactory是一个工厂?

发布于 2024-09-24 21:18:13 字数 16 浏览 0 评论 0原文

工厂后缀代表什么?

What does the Factory postfix stand for?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

你穿错了嫁妆 2024-10-01 21:18:13

工厂对象的唯一目的是创建(实例化)其他对象。 NHibernate 使用 SessionFactory 来管理 ISession 的创建。

通过同样的措施 - ProxyFactoryFactory 创建一个可以生成代理的工厂。它解决的更大问题是它允许在代码中保持一定程度的抽象,以便两个不同的模块不需要了解彼此的实现细节(即具体实例)即可协同工作。

如果我们在想要打开会话时必须创建一个新的 NHibernate SessionImpl,那么我们需要能够在本地范围内提供其所有依赖项。下面是 NHibernate 中 SessionFactoryImpl 中实际创建会话的代码:

        SessionImpl session = new SessionImpl(connection, this, autoClose, timestamp, sessionLocalInterceptor ?? interceptor,
                                              settings.DefaultEntityMode, settings.IsFlushBeforeCompletionEnabled,
                                              settings.IsAutoCloseSessionEnabled, settings.ConnectionReleaseMode);

我宁愿只使用

sessionFactory.OpenSession();

而不是必须管理所有这些依赖项;)

A factory object is one whose sole purpose is creation (instantiation) of other objects. NHibernate uses the SessionFactory to manage the creation of ISessions.

By the same measure - the ProxyFactoryFactory creates a factory that can produce proxies. The larger problem it solves is it allows to keep a level of abstraction in code so that two distinct modules don't need to understand each others' implementation details (i.e. concrete instances) in order to work together.

If we had to create a new NHibernate SessionImpl when we wanted to open a session, we'd need to be able to supply all of its dependencies in a local scope. Here's the code out of the SessionFactoryImpl in NHibernate that actually creates a session:

        SessionImpl session = new SessionImpl(connection, this, autoClose, timestamp, sessionLocalInterceptor ?? interceptor,
                                              settings.DefaultEntityMode, settings.IsFlushBeforeCompletionEnabled,
                                              settings.IsAutoCloseSessionEnabled, settings.ConnectionReleaseMode);

I'd much rather just use

sessionFactory.OpenSession();

than have to manage all of those dependencies ;)

葬シ愛 2024-10-01 21:18:13

因为它的唯一责任是创建一个会话:一个生产会话的工厂。

Since it's sole responsibility is to create a Session: a factory that produces Sessions.

空‖城人不在 2024-10-01 21:18:13

工厂是一种设计模式。它是一个创建对象的类。会话工厂创建会话。

A factory is a design pattern. It is a class that creates objects. The session factory creates sessions.

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