NHibernate Session DI from StructureMap in components
Posted
by Corey Coogan
on Stack Overflow
See other posts from Stack Overflow
or by Corey Coogan
Published on 2010-01-22T18:09:22Z
Indexed on
2010/04/01
17:03 UTC
Read the original article
Hit count: 341
I know this is somewhat of a dead horse, but I'm not finding a satisfactory answer. First let me say, I am NOT dealing with a web app, otherwise managing NH Session is quite simple.
I have a bunch of enterprise components. Those components have their own service layer that will act on multiple repositories. For example:
- Claim Component
- Claim Processing Service
- Claim Repository
- Billing Component
- Billing Service
- Billing REpository
- Policy Component
- PolicyLockService
- Policy Repository
Now I may have a console, or windows application that needs to coordinate an operation that involves each of the services. I want to write the services to be injected with (DI) their required repositories. The Repositories should have an ISession, or similar, injected into them so that I can have this operation performed under one ISession/ITransaction.
I'm aware of the Unit Of Work pattern and the many samples out there, but none of them showed DI. I'm also leery of [ThreadStatic] because this stuff can also be used from WCF and I have found enough posts describing how to do that. I've read about Business Conversations, but need something simple that each windows/console app can easily bootstrap since we have alot of these apps and some pretty inexperienced developers.
So how can I configure StructureMap to inject the same ISession into each of the dependent repositories from an application? Here's a totally contrived and totally made up example without using SM (for clarification only - please don't spend energy critisizing):
ConsoleApplication
Main
{
using(ISession session = GetSession())
using(ITransaction trans = session.BeginTransaction())
{
var policyRepo = new PolicyRepo(session);
var policyService = new PolicyService(policyRepo);
var billingRepo = new BillingRepo(session)
var billingService = new BillingService(billingRepo);
var claimRepo = new ClaimsRepo(session);
var claimService = new ClaimService(claimRepo, policyService, billingService);
claimService.FileCLaim();
trans.Commit();
}
}
© Stack Overflow or respective owner