1

How can I configure NServiceBus to inject decorated classes correctly?

Imagine this scenario:

public class A
{
    public IDependency Dependency {get;set;}
}

public class B : IDependency
{
    public IDependency DecoratedDependency {get; set;}
}

public class C : IDependency{}

I want to set up my config so that B is created with C injected, and then A is created with B injected. I don't want A created with C injected.

How do I set the config up so that it does this? It feels like I should use something along the lines of:

Configure.Instance.Configurer
    .ConfigureComponent<B>(...)
    .ConfigureProperty(x=>x.DecoratedDependency, instanceOfCFromTheContainer);
Configure.Instance.Configurer
     .ConfigureComponent<A>(...)
     .ConfigureProperty(x=>x.Dependency, instanceOfBFromTheContainer);

but I'm not sure how I refer to those instances? And how do I ensure that C is only used for B's property and that B is used everywhere else when the container does its resolution?

Or do I need to do something different?

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
  • Are you passing your container into NSB for it to use as well? It may be better to use the native container configuration and then pass it in. – Adam Fyles Aug 06 '12 at 17:12
  • We are just using a series of calls to `Configure.Instance.Configurer.ConfigureComponent`. I'm not sure what you mean by 'It may be better to use the native container configuration and then pass it in'. – Sam Holder Aug 06 '12 at 18:31
  • 1
    Adam wants to know if you use a DIcontainer such as windsor, ninject... Or do you ise the DefaultBuilder? If you have Configure.With().NinjectBuilder(kernel) you can provide a kernal instance. It is much more powerful to do this kind of configuration on the native container as your are not restricted to a common container abstractions as NSB uses it internally. – Daniel Marbach Aug 07 '12 at 04:37
  • Thanks Daniel. We are just using the default builder. Can I not do this kind of configuration using the common container abstractions? – Sam Holder Aug 07 '12 at 07:31
  • 1
    Not that I know. I suggest you use one of your favorite containers for that purpose – Daniel Marbach Aug 08 '12 at 05:21

0 Answers0