views:

279

answers:

3

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


  }

}
A: 

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
A: 

One thing to keep in mind, is that Jeremy's post is for Web apps. They are using some StructureMap magic to influence the way ASP.Net is managing sessions and the NH Session. So essentially every request starts a new transaction.

I have my webapp, setup like Jeremy does. But was wanting to setup a WinGui of some of the stuff to use the same code in my core project. Will post back if I get a solution.

Ryan Kelley
Thanks for the comment Ryan. Actually though, the stuff JDM had is perfect for any situation. Relying on nested containers to control transaction scope works like a champ in any environment. You can execute a command from his TransactionProcessor per WinForm, console app, etc.http://codebetter.com/blogs/jeremy.miller/archive/2010/01/06/how-dovetail-uses-structuremap-with-nhibernate.aspxhttp://codebetter.com/blogs/jeremy.miller/archive/2010/02/10/nested-containers-in-structuremap-2-6-1.aspx
Corey Coogan
A: 

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