Using NHibernate in the class library for applications both on the Internet and on Windows

I just modified my NHibernate application to use the Unit of Work template. Then I continued this tutorial to the part where it starts using HttpContext to determine if the code is working in a web application. Using Visual Studio 2010, I get the error "The name" HttpContext "does not exist in the current context." So I added a link to System.Web and imported the namespace. The sample code then checks to see if HttpContext.Current is null to determine if it is a web or windowed application. Is this the best way to do this?

+4
source share
2 answers

I would go differently. Create a UnitOfWork class that has Begin and End methods. Then your applications can use them depending on what you want. Therefore, if you are writing a web application, you may need to start work on BeginRequest and finish work on EndRequest. If you are writing a Windows application, you can start working on the action and complete it when done.

The bottom line is that your data level and unit of work implementation should be independent of the context in which it is used.

+4
source

You can check out HttpRuntime.AppDomainId . This should never be null if the thread is hosted in a web server process. HttpContext may be null depending on when you check it in the event life cycle. Asynchronous methods will also show HttpContext zero, as they do not work in the original web stream.

+2
source

Source: https://habr.com/ru/post/1342836/


All Articles