为IHttpModule的和控制台应用程序功能NHibernate模式 [英] Fluent NHibernate pattern for both IHttpModule and console apps

查看:73
本文介绍了为IHttpModule的和控制台应用程序功能NHibernate模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有一个存储库模式使用功能NHibernate(+ LINQ)一个C#MVC 2的Web应用程序和正在使用Ninject来处理,它在存储库中通过MVC控制器的构造要求。

I currently have a C# MVC 2 web app using Fluent NHibernate (+ LINQ) in a Repository Pattern and am using Ninject to handle the MVC controller's constructor requirement that it be passed in the repository.

我的功能NHibernate code目前勾搭成一个IHttpModule的,所以会议可以打开,并与Web请求关闭。

My Fluent NHibernate code is currently hooked into an IHttpModule, so the session can be opened and closed with the web request.

直到我试图勾我的域模型到一个控制台应用程序这一伟大工程。

This works great until I try to hook my domain model into a console app.

首先,我决定把我的数据库逻辑移到我的域模型。我猜这是不好的行为,但我在这里要求在code设计的帮助,所以请随时提出建议。我想这样做,因为我想写一个控制台应用程序+ Web应用程序在这个领域模型,而不关心在哪里以及如何存储数据。也许我应该有一个使用域模型做了具体的实现一个单独的基础设施项目?

First, I decided to move my database logic into my domain model. I'm guessing this is bad behavior but I am here asking for help on code design, so please feel free to make suggestions. I wanted to do this because I want to write a console app + web app on this domain model without caring where or how the data is stored. Perhaps I should have a separate "Infrastructure" project that uses the domain model to do a particular implementation?

再回到问题...我碰到一个问题,即它似乎流利NHibernate的的getCurrentSession()假设你正在使用的网络 - 它崩溃,我得到一个回溯到一些NH code提一个WebSession。

Getting back to the question ... I run into a problem where it seems Fluent NHibernate's GetCurrentSession() assumes you're using the web - it crashes and I get a traceback to some NH code mentioning a WebSession.

有关参考起见,code是在这里: https://github.com/cthielen/RightsManagement

For reference sake, the code is here: https://github.com/cthielen/RightsManagement .

我对不是太具体的道歉;我在寻找code设计建议最佳处理FNH +中的LINQ与Web和控制台应用程序运行良好既是一个资源库模式 - 并单独询问我的数据库连接(即NH逻辑)应该在域的项目或没有。

My apologies for not being too specific; I'm looking for code design advice to best handle FNH + Linq in a repository pattern that works well both with the Web and a console app -- and separately asking if my database connection (i.e. NH logic) should be in the domain project or not.

推荐答案

这是一个很好的问题。有很多的,这可能会回答不同的方式,但每个答案将取决于你有多少经验,有不同的方法在那里。例如,你熟悉TDD?依赖注入? IoC容器中,?等等。

This is a good question. There are a lot of different ways that this could be answered, but each answer is going to depend on how much experience you have with the different approaches out there. For example, are you familiar with TDD? Dependency injection? IoC containers? And so on.

我可以在此时给出的最好的建议是清理所有现有的类,使它们不依赖上下文。这方面的一个例子是修改你的资料库类,以便他们采取一个ISession对象作为构造函数的参数,而不是根据什么似乎是一个单例,仅当在HTTP上下文是存在的。这将允许你在会话创建逻辑重构你的域逻辑之外的更高的水平。

The best advice I can give at this point is to clean up all of your existing classes so that they are not context dependent. One example of this would be modifying your repository classes so that they take an ISession object as a constructor argument instead of depending on what appears to be a singleton instance that only exists if you're in a HTTP context. This would allow you to refactor the session creation logic to a higher level outside of your domain logic.

public class PeopleRepository {
    public PeopleRepository(ISession session) {
        // store session
    }
}

在我的情况,我会用一个IoC容器注入会话依赖。我可以使用此容器从任何类型的应用程序,无论是使用session-per-request模式或控制台应用基于web的应用。

In my case, I would use a IoC container to inject the session dependency. I can use this container from any type of application, whether it's a web based application using the session-per-request pattern or a console application.

有一些你需要做的就是NHibernate的两个应用程序中发挥不错的工作。面临的挑战是尽可能最大程度地减少工作

There's some work you'll need to do to get NHibernate to play nice in both applications. The challenge is to minimize that work as much as possible.

我很乐意为您提供更多的指导。让我知道了。

I'm happy to provide more guidance. Let me know.

这篇关于为IHttpModule的和控制台应用程序功能NHibernate模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆