在存储库层中使用 Fluent NHibernate 进行 Ninject
由于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我建议您查看 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/
通常我有一个 NHibernateSessionFactory,它是一个具有 OpenSession 方法的单例,并且我通常像这样绑定 ISession。
此方法仅调用
ISessionFactory.OpenSession
您可以将其放入存储库层的
NinjectModule
中,您的应用程序可以在创建内核时加载该模块。Typically I have an NHibernateSessionFactory which is a singleton that has an OpenSession method and I bind ISession typically like this.
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.我在应用程序层(即顶层)进行配置,因为应用程序之间的配置不同。但将一些配置分解为存储在存储库层中的类可能很有用。
我使用 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.