1

I am trying to use the Rebus transaction context within my message handler. I have read the documentation here and have seen the sample here, however I do not know how Windsor works.

Could somebody make an example of using ITransactionContext with EF without any IOC container just to understand the way it works?

Thanks

Lorenzo
  • 29,081
  • 49
  • 125
  • 222

1 Answers1

1

I can recommend you take a look at the Rebus.UnitOfWork package because it provides a slightly higher level API for implementing a custom unit of work – either with or without an IoC container.

With Rebus.UnitOfWork you can do this:

Configure.With(...)
    .(...)
    .Options(o => {
        o.EnableUnitOfWork(Create, Commit, RollBack, Cleanup);
    })
    .Start();

//....

static MyCustomUnitOfWork Create() => new MyCustomUnitOfWork();

static void Commit(MyCustomUnitOfWork uow) => uow.Commit();

static void RollBack(MyCustomUnitOfWork uow) => uow.RollBack();

static void Cleanup(MyCustomUnitOfWork uow) => uow.Dispose();

where MyCustomUnitOfWork can then be whatever you want, e.g. a class that creates an EF database context and calls SaveChanges and whatnot on it.

You can read more on the wiki page about Unit Of Work, or go directly to the sample that demonstrates Rebus.UnitOfWork with running code.

mookid8000
  • 18,258
  • 2
  • 39
  • 63
  • Very clear thank you. The only problem I have right now is that my EF database context is already registered differently (for instance not with a factory method as the suggested sample does) and I dont know how to use a different registration for the message handler in particular. Any suggestion? – Lorenzo Apr 24 '17 at 12:16
  • 1
    My suggestion is always to use a separate IoC container for each "logical application" you have in your process. E.g. if you are hosting a web application and a Rebus-based backend, you would have two separate "logical applications", and therefore you should have two IoC containers. This way, you can use lifestyles that target each particular usage scenario (e.g. bound to web requests in the web app, use factory method in Rebus backend) instead of bending it out of shape to try to make it work in both scenarios. – mookid8000 Apr 24 '17 at 12:39