Hi Sean, 

It's not really the list to answer these questions, and you've asked a 
mouthful, but maybe I can point in in the right direction for you to get the 
answers you need as the information is out there -- I found it for my project 
:).

Everything you want to do is doable, and for the most part you are heading in 
the right direction. Here are some notes and pointers:

Should I Use A Singleton For Session Factory/Repository Creation
No. You don't need a singleton for this. I would encourage you not to, at 
least. Look at dependency injection. The container you use doesn't matter 
(Unity, NInject, Autofac, Castle etc), as MVC4's architecture abstracts this 
away. Here's an example using Unity (I personally am using NInject): 
http://netmvc.blogspot.com.au/2012/04/dependency-injection-in-aspnet-mvc-4.html

Using injection, you can then wire up your controller's constructors to expect 
a constructed class to be passed in, MVC4 will resolve the constructor 
dependencies through the DI container automatically for you. Your controllers 
should never have to instantiate a repository, and they should have no idea 
about NHibernate.

So How Do I Handle Sessions/Transactions?
Use filters. Spin up your session factory in Application_Start and stuff it 
into your DI container. Build filters to deal with the rest. I personally use a 
Matroska doll-like series of filters. First filter builds an NHibernate 
session, next filter loads a bunch of static preference and default data (out 
of transaction), next filter starts a transaction for the request.

The other way, which is an extension of what I suggest, is to use filter 
attributes. This way you can just attribute each controller with session and 
transaction attributes when needed. That lets you also have controllers that 
have no database access at all. All of my business logic is encapsulated in a 
Web API with each request hitting the database, so I went with filters. They're 
typically setup in your global.asax.cs file, but I use web activator and a 
bootstrapper class.

The accepted answer to this is the first search result I came across and will 
give you an idea: 
http://stackoverflow.com/questions/12118196/how-to-handle-nhibernate-sessions-in-an-mvc4-project

A Word On Repositories.
Don't.

More The A Word On Repositories.
Really don't. I started out with repositories, and have wound up wishing I 
hadn't. Use data access objects instead.

Why?
The original idea of a repository is list-like access to your data. Your 
application should think and work like it is simply using a large typed 
collection, and forget about the database side of things. Add something to the 
list, remove something from the list, then filter the list for specific 
somethings. Imagine filtering a list of ten million transactions for a specific 
transaction by number, or a group by type: 

var candidateTransactions = new List<Transaction>();
foreach(var tx in transactionRepository) {
    if (tx.Type == candidateType) {
        candateTransactions.Add(tx);
    }
}
return candidateTransactions.

Ridiculous.

So, the next level is to add a method to your repository interface that accepts 
a criteria object of some sort.

http://martinfowler.com/eaaCatalog/repository.html

The smart move at this stage is to wire in NHibernate's criteria queries to do 
the dirty work for you. Whoops! You've leaked NHibernate all over your business 
logic. So now you start building specialised repositories for each aggregate, 
then add methods to them to encapsulate the NHibernate stuff (like 
FindTransactionWithNumber and FindTransactionsWithType etc).

After all this, what you have wound up with is a bunch of data access objects 
that you call repositories. Don't kid yourself. Just because the class name 
ends in repository, doesn't mean your following the repository pattern. Begin 
with the end in mind - use data access objects from the beginning. It's not a 
religious thing, it's just a better fit for bigger applications.

I hope all that helps somewhat, and I don't get into trouble :). This really 
isn't the list for it. You'd be better off asking on the nh mailing list or the 
mvc mailing list. 

Thanks,
Geoff.


On Saturday, 2 February 2013 at 6:31 AM, Sean Killeen wrote:

> Hi all,
> 
> New to NHibernate but not necessarily the concepts of ORM. 
> 
> I've read a great deal of information and some of it seems to blur the lines 
> for my specific situation so I'm wondering if someone can clarify a few of 
> the concepts and maybe refer me to a blog post to solve a specific.
> 
> I have the following Solution Structure (including some test suites not shown 
> here):
> Databases: Oracle 11g (only need to query; strictly read-only) & SQL Server 
> (not using NHib for this; using EF CodeFirst)
> Project.Model -- class library with all my POCOs.
> Project.Data -- class library with Repositories, Session Factories, etc. This 
> is where I would like all of my data access code to be 
> Project.Web -- MVC4 webapp. I would like this webapp to only communicate via 
> POCOs, Repositories, and units of work.
> 
> I would like accomplish the following:
> 
> Store my connection string in Project.Web's Web.config (http://Web.config) 
> and pass it into my Data project (my web.config (http://web.config) is 
> transformed automatically in my build process so it would be nice to keep it 
> here).
> Use FluentNHibernate to configure NHibernate and fluently map to entities
> Use ODP.NET + FluentNHibernate for an Oracle Database (I have another SQL 
> Server DB which I'll be using EF Code-First for instead of NHibernate as well)
> Use a session factory the proper way
> Use transactions the proper way
> Use linq to query the Oracle DB (again, read only -- no writes/updates)
> Verify via NUnit that these things are working as they should be.
> I'd like to make use of Transact() => to set up transactions, but am 
> unfamiliar with the syntax around it.
> I'd like to keep anything about the data within the context of the Data 
> project as much as possible.
> 
> My general thinking so far (please tell me if/where I'm going wrong here as 
> I'm almost certain I am):
> 
> install ODP.net (http://ODP.net) via nuget to Project.Data
> Create a SessionFactory as some type of singleton in Project.Data that takes 
> a string parameter for the connection string and uses that to create an 
> OracleDataClientConfiguration with that connection string.
> From my Project.Web file Application_Start, call the 
> Project.Data.MyDBSessionFactory(ConfigurationManager.ConnectionStrings["TheDBConnStringName"].ConnectionString).CreateSessionFactory()
>  method to create the session factory.
> I'm unsure about the best way to handle sessions and transactions. It seems 
> that maybe I should manage them at the repository level (creating a new 
> session if one isn't provided) and then using a unit of work to provide a 
> session to multiple repositories when working in the context of a larger 
> transaction?
> Any thought on this and how to architect it would be greatly appreciated. 
> Again, I've been searching around but blog links would be appreciated.
> 
> 
> Thanks,
> Sean
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "Fluent NHibernate" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to fluent-nhibernate+unsubscr...@googlegroups.com 
> (mailto:fluent-nhibernate+unsubscr...@googlegroups.com).
> To post to this group, send email to fluent-nhibernate@googlegroups.com 
> (mailto:fluent-nhibernate@googlegroups.com).
> Visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>  
>  

-- 
You received this message because you are subscribed to the Google Groups 
"Fluent NHibernate" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to fluent-nhibernate+unsubscr...@googlegroups.com.
To post to this group, send email to fluent-nhibernate@googlegroups.com.
Visit this group at http://groups.google.com/group/fluent-nhibernate?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to