在存储库层中使用 Fluent NHibernate 进行 Ninject

发布于 2024-10-15 15:00:08 字数 398 浏览 4 评论 0原文

由于 LinqToSql 不适合多对多关系,我正在决定迁移到 NHibernate(Fluent NHibernate),除非另有说服力......

项目结构:UI(带有 Ninject 的 Mvc2 应用程序将所有服务连接到控制器和存储库)服务)、DomainServiceLayer(所有 util、帮助程序、服务、域模型等)和我的用于持久性的存储库层。我有一个名为 Model 的另一个项目,它基本上公开了所有项目引用的实体。

基本上,我在存储库层中创建映射,并引用 NHIbernate 和 Fluent NHIibernate,我希望将接口公开给域服务以查询和保存数据。如何连接 iSession?在哪里连接?有示例代码吗,我应该把它放在什么项目中?理想情况下,我想将其保留在存储库层中...值得学习 NHibernate 并经历这一切吗?

Due do LinqToSql not being appropriate for Many To Many relationships I am in the process of deciding to move to NHibernate (Fluent NHibernate) unless convinced otherwise...

Project Structure: UI (Mvc2 app with Ninject wiring up all services to controllers, and repositories to services), DomainServiceLayer (all util, helpers, services, domain model etc) and my Repository Layer for persistence. I have a another project call Model which basically exposes the entities, which all projects reference.

Basically I am creating my mappings within the Repository Layer with references to NHIbernate and Fluent NHIibernate, I hope to expose the interfaces to the Domain Service for querying and persisting data. How do I wire up the iSession, where do I wire it up? Any example code, what project should I put it in? Ideally I want to keep this within the Repository Layer... Is it worth learning NHibernate and going through all this?

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

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

发布评论

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

评论(3

梦魇绽荼蘼 2024-10-22 15:00:08

我建议您查看 Bob 的博客文章。他详细描述了如何使用 NHibernate 在 Ninject 中使用存储库模式。我计划在不久的将来向带有 MVC 扩展的示例应用程序添加一个示例,因为这个问题一次又一次地出现。

http:// blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to- Fluent-nhibernate-and-mysql/

http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/

http://blog.bobcravens.com/2010/ 09/the-repository-pattern-part-2/

I recommend looking at the blog posts of Bob. He describes in detail how to use the repository pattern in Ninject using NHibernate. I planned adding an example in the near future to the sample application comming with the MVC exptension as this question comes up again and again.

http://blog.bobcravens.com/2010/06/the-repository-pattern-with-linq-to-fluent-nhibernate-and-mysql/

http://blog.bobcravens.com/2010/07/using-nhibernate-in-asp-net-mvc/

http://blog.bobcravens.com/2010/09/the-repository-pattern-part-2/

动听の歌 2024-10-22 15:00:08

通常我有一个 NHibernateSessionFactory,它是一个具有 OpenSession 方法的单例,并且我通常像这样绑定 ISession。

Bind<ISession>().ToMethod(context => 
NHibernateSessionFactory.Instance.OpenSession()).InRequestScope();

此方法仅调用 ISessionFactory.OpenSession
您可以将其放入存储库层的 NinjectModule 中,您的应用程序可以在创建内核时加载该模块。

Typically I have an NHibernateSessionFactory which is a singleton that has an OpenSession method and I bind ISession typically like this.

Bind<ISession>().ToMethod(context => 
NHibernateSessionFactory.Instance.OpenSession()).InRequestScope();

This method just calls through to ISessionFactory.OpenSession
You can put this into a NinjectModule in your repository layer, which your app can load when it creates the Kernel.

我还不会笑 2024-10-22 15:00:08

我在应用程序层(即顶层)进行配置,因为应用程序之间的配置不同。但将一些配置分解为存储在存储库层中的类可能很有用。

我使用 HttpModule 打开和关闭会话。

I do the configuration in the Application Layer (i.e. the top layer) as the configuration differs between applications. But it can be useful to break out some of the configuration into classes stored in the Repository Layer.

I open and close the session with an HttpModule.

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