1

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();


  }

}
Corey Coogan
  • 1,569
  • 2
  • 17
  • 31
  • You can do this by using a AsSingleton in the structuremap configuration, but you probably do not want to use one session for the whole applications. – Paco Jan 22 '10 at 18:32
  • Correct, I don't want to use one per application. – Corey Coogan Jan 22 '10 at 19:09
  • I've read Jeremy D. Millers post on using NH at Dovetail 100 times and think there is something to nested containers, but I can't find much to help wrap my head around it yet. – Corey Coogan Jan 22 '10 at 20:35

2 Answers2

2

I finally got around to some posts on StructureMap that could help some folks.

First, a primer that is somewhat relevant to the next posts: http://blog.coreycoogan.com/2010/05/24/using-structuremap-to-configure-applications-and-components/

Now how to use SM with WCF and NHIbernate: http://blog.coreycoogan.com/2010/05/26/structuremap-wcf-nhibernate-part-1/

Corey Coogan
  • 1,569
  • 2
  • 17
  • 31
0

I think I now have the missing piece of the puzzle. Jeremy D. Miller was kind enough to post his code for the ITransactionProcessor.

http://codebetter.com/blogs/jeremy.miller/archive/2010/01/06/how-dovetail-uses-structuremap-with-nhibernate.aspx

This will use the new StructureMap nested containers to scope my session to all my components in a single transaction. When I get this working, I'll post the code to my blog and an update to this thread.

http://blog.coreycoogan.com

Corey Coogan
  • 1,569
  • 2
  • 17
  • 31