Have Castle Windsor select component based on requestor type namespace - castle-windsor

I have an interface ISession whose instance is produced by a different Session Factory depending on which namespace the class belongs to.
Example of my component registration:
IWindsorContainer container = new WindsorContainer();
container.Register(Component.For<NHibernate.ISession>()
.UsingFactoryMethod((kernel, creationContext) =>
{
NHibernate.ISession session =
new SessionFactoryForNamespace1()
.Instance.GetSession();
return session;
})
.LifestylePerWebRequest());
container.Register(Component.For<NHibernate.ISession>()
.UsingFactoryMethod((kernel, creationContext) =>
{
NHibernate.ISession session =
new SessionFactoryForNamespace2()
.Instance.GetSession();
return session;
})
.LifestylePerWebRequest());
container.Register(Component.For<Namespace1.IRepository1()
.ImplementedBy<Namespace1.Repository1>());
container.Register(Component.For<Namespace2.IRepository2>()
.ImplementedBy<Namespace2.Repository2>());
Example of the resolution graph:
public class MyController
{
public MyController(Namespace1.IRepository1 repo1,
Namespace2.IRepository2 repo2) { }
}
namespace Namespace1
{
public interface IRepository1 { }
public class Repository1 : IRepository1
{
public Repository1(NHibernate.ISession session) { }
}
}
namespace Namespace2
{
public interface IRepository2 { }
public class Repository2 : IRepository2
{
public Repository2(NHibernate.ISession session) { }
}
}
When Castle Windsor is asked to resolve MyController, it then tries to resolve IRepository1 and IRepository2, and subsequently the ISession for each. I want to have Castle Windsor select the component handlers based on the requestor type's namespace which in my example is either Namespace1 or Namespace2.
I am new to Castle Windsor and not sure where in the resolution pipeline I'm supposed to be plugging into.
What is the best approach to accomplish what I have outlined above?

I think a service override would work for this.
UPDATE:
I also did an article on some of Windsor's advanced features (including a section on Service Overrides) that should augment the documentation linked above.

Here is how I implemented service override solution:
Repository interfaces now inherit from a common repository interface:
public class MyController
{
public MyController(Namespace1.IRepository1 repo1,
Namespace2.IRepository2 repo2) { }
}
public interface IRepository { }
namespace Namespace1
{
public interface IRepository1 : IRepository { }
public class Repository1 : IRepository1
{
public Repository1(NHibernate.ISession session) { }
}
}
namespace Namespace2
{
public interface IRepository2 : IRepository { }
public class Repository2 : IRepository2
{
public Repository2(NHibernate.ISession session) { }
}
}
Repository component registeration based on its namespace:
IWindsorContainer container = new WindsorContainer();
...
Action<Type> RegisterRepository = t =>
{
container.Register(
AllTypes.FromAssemblyContaining(t)
.BasedOn(typeof(IRepository))
.WithServiceAllInterfaces()
.Configure(c =>
{
c.DependsOn(
ServiceOverride
.ForKey<NHibernate.ISession>()
.Eq(c.Implementation.Namespace));
c.LifeStyle.Is(LifestyleType.Transient);
})
);
};
RegisterRepository(typeof(Namespace1.IRepository1));
RegisterRepository(typeof(Namespace2.IRepository2));
Seems to work :)

Related

How to load to container MassTransit EndPoint in AbstractFacility

I have a PersistenceFacility class .
This class inhereted "AbstractFacility" as "Castle.MicroKernel.Facilities".
I wonder load and install into container: IWindsorContainer.
public class PersistenceFacility: AbstractFacility
{
protected override void Init()
{
ServiceIoC.Container.Register(Component.For<IBusControl>().LifeStyle.Singleton.UsingFactoryMethod(k =>
{
var busControl = Bus.Factory.CreateUsingRabbitMq(config =>
{
config.Host(new Uri("blabla"), host =>
{
host.Username("guest");
host.Password("guest");
});
config.ReceiveEndpoint("", endpoint =>
{
endpoint.EnableMessageScope();
// Above method works but it is deprecated, instead below method should be used to get Consumer from container.
//endPoint.Consumer<YourConsumer>(container.Kernel);
});
});
return busControl;
}).LifeStyle.Singleton.Named("XXXMassTransitRMQ"));
}
// installer class
public class PersistenceInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container?.AddFacility<PersistenceFacility>();
}
}
//UOW Class
public class UnitOfWork
{
internal static IBusControl MassTransitRmqControl => ServiceIoC.Container?.Resolve<IBusControl>("XXXMassTransitRMQ");
}
First, I would consider moving to use the new container support for Windsor, as outlined in the documentation.
You can see how MassTransit registers components with Windsor by looking at the configuration.
I'm not sure an abstract facility is the way to go, given the approach taken by the links referenced above.

How to register an interface for interceptions

I want to register an interface like: IInterceptingAware, so that for all classes which implement this interface an interceptor class is used.
public class InterceptorClass : IInterceptor
{
public void Intercept(IInvocation invocation)
{
// Do my work
}
}
public class Foo : IInterceptingAware
{
}
public class Bar : IInterceptingAware
{
}
How do I setup castle windsor for this?
using (var container = new WindsorContainer())
{
container.Register(
Component.For<MyInterceptorClass>(),
Classes.FromThisAssembly()
.BasedOn<IInterceptingAware>()
.WithServiceDefaultInterfaces()
.ConfigureFor<IInterceptingAware>(c => c.Interceptors<MyInterceptorClass>())
);
var foo = container.Resolve<Foo>();
foo.Test();
var bar = container.Resolve<IBar>();
bar.Test();
}
Console.ReadLine();
Keep in mind interceptor requires at least virtual methods on target class, even better your target class should implement an interface in order to have the interceptor working on that contract.
Said so, your Foo class should at least have a method named Test marked as virtual
while Bar should implement IBar:
public interface IBar
{
void Test();
}

ninject factory constructor selection with runtime generics

How can I use a ninject factory, which creates an instance with constructor parameters, without relying on the argument names.
The problem is that the ToConstructor()-Method not works, because I bind it to a generic definition.
The following example works, if I use the factory method with the corresponding constructor argument name, but I don't like it rely on names.
Because the following solution is very fragil and breaks if someone chooses a wrong name or renames the ctor-argument in the derived class.
Any solution?
Here's the example code:
[TestFixture]
public class NinjectFactoryBindingsTest
{
[Test]
public void ConstructorSelectionWithArguments()
{
NinjectSettings ninjectSettings = new NinjectSettings();
ninjectSettings.LoadExtensions = false;
using (var kernel = new StandardKernel(ninjectSettings, new FuncModule()))
{
// IDependencyA will be passed to the factory, therefore it is not bounded
//kernel.Bind<IDependencyA>().To<DependencyA>();
kernel.Bind<IDependencyB>().To<DependencyB>();
kernel.Bind(typeof(IGenericBaseClass<>)).To(typeof(GenericDerivedClass<>));
kernel.Bind<IGenericClassFactory>().ToFactory();
IGenericClassFactory factory = kernel.Get<IGenericClassFactory>();
DependencyA dependencyA = new DependencyA();
IGenericBaseClass<GenericImpl> shouldWorkInstance = factory.Create<GenericImpl>(dependencyA);
Assert.NotNull(shouldWorkInstance);
}
}
}
public interface IGenericClassFactory
{
IGenericBaseClass<TGeneric> Create<TGeneric>(IDependencyA someName) where TGeneric : IGeneric;
// This works, but relies on ctor-param-names!!!
// IGenericBaseClass<TGeneric> Create<TGeneric>(IDependencyA otherNameThanInBaseClass) where TGeneric : IGeneric;
}
public class DependencyA : IDependencyA
{
}
public class DependencyB : IDependencyB
{
}
public class GenericDerivedClass<TGeneric> : GenericBaseClass<TGeneric> where TGeneric : IGeneric
{
public GenericDerivedClass(IDependencyA otherNameThanInBaseClass, IDependencyB dependencyB)
: base(otherNameThanInBaseClass, dependencyB)
{
}
}
public abstract class GenericBaseClass<TGeneric> : IGenericBaseClass<TGeneric> where TGeneric : IGeneric
{
protected GenericBaseClass(IDependencyA dependencyA, IDependencyB dependencyB)
{
}
}
public interface IGenericBaseClass<TGeneric> where TGeneric : IGeneric
{
}
public interface IDependencyB
{
}
public interface IDependencyA
{
}
public class GenericImpl : IGeneric
{
}
public interface IGeneric
{
}
The factory extension has the convention that arguments must have the same name as the constructor argument they will be passed to. There are no easy ways to do it differently. The only way I can think of is about the following:
Create a new IParameter implementation that can hold a reference to an IDependencyA.
Create either a hardcoded factory or a custom IInstanceProvider (see documentation) that creates an instance of your IParameter implementation so that it is passed to the Get<> request
Add a new binding for IDependencyA: Bind<IDependency>().ToMethod(ctx => extract and return your parameter from the context)

Change ServiceOverride after component registration in Castle

I have two components registered for a service:
container.Register(
Component.For<IDataStorage>().Named("FirstChoice").ImplementedBy...
Component.For<IDataStorage>().Named("SecondChoice").ImplementedBy
Then I have a set of components deriving from this base class, that depends on those components:
public abstract class BaseMessageHandler
{
public IDataStorage FirstStorage {get; set;}
public IDataStorage SecondStorage {get; set;}
}
If I was registering these "handlers" (deriving from BaseMessageHandler) manually, I would specify a service override, indicating which component I want for properties "FirstStorage" and "SecondStorage". Something like:
.Configure(x => x.DependsOn(
ServiceOverride.ForKey("FirstStorage").Eq("FirstChoice"),
ServiceOverride.ForKey("SecondStorage").Eq("SecondChoice"))
Unfortunately, this registration is done automatically by a framework (NServiceBus). I know that if I register the handlers first (before NServiceBus has a chance to do so), these registrations will stick. But rather than try to guess and mimick the registration NServiceBus does, I wonder if I can specify service overrides in a custom IContributeComponentModelConstruction.
It seems a good place to do so, and I'm able to find these properties:
public class DataStorageOverrideContributor : IContributeComponentModelConstruction
{
public void ProcessModel(Castle.MicroKernel.IKernel kernel, Castle.Core.ComponentModel model)
{
var dataStorageDependencies = model.Properties.Where(
x => x.Dependency.TargetItemType == typeof(IDataStorage));
foreach (var propertyDependency in dataStorageDependencies)
{
// now what??
but I'm not sure the proper way to:
check if a service override is already specified (in which case I would do nothing)
add a service override to a property dependency.
Is this something that could be done inspecting and working the ComponentModel in the IContributeComponentModelConstruction.ProcessModel method?
I would use a subdependency resolver in your scenario. You can see the code below on how to use it.
Goodluck,
Marwijn.
public interface IDataStorage
{
}
public class DataStore1 : IDataStorage
{
}
public class DataStore2 : IDataStorage
{
}
public class BaseMessageHandler
{
public IDataStorage FirstStorage { get; set; }
public IDataStorage SecondStorage { get; set; }
}
public class SubDependencyResolver : ISubDependencyResolver
{
private readonly IKernel _kernel;
public SubDependencyResolver(IKernel kernel)
{
_kernel = kernel;
}
public bool CanResolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
{
return model.Implementation == typeof (BaseMessageHandler) && dependency.TargetType == typeof(IDataStorage);
}
public object Resolve(CreationContext context, ISubDependencyResolver contextHandlerResolver, ComponentModel model,
DependencyModel dependency)
{
var handlers = _kernel.GetHandlers(dependency.TargetType);
switch (dependency.DependencyKey)
{
case "FirstStorage":
return handlers.Single(h => h.ComponentModel.Implementation == typeof (DataStore1)).Resolve(context);
case "SecondStorage":
return handlers.Single(h => h.ComponentModel.Implementation == typeof(DataStore2)).Resolve(context);
}
return null;
}
}
[TestFixture]
public class Tests
{
[Test]
public void SomeTest()
{
var container = new WindsorContainer();
container.Kernel.Resolver.AddSubResolver(new SubDependencyResolver(container.Kernel));
container.Register(
Component.For<IDataStorage>().Named("FirstChoice").ImplementedBy<DataStore1>(),
Component.For<IDataStorage>().Named("SecondChoice").ImplementedBy<DataStore2>(),
Component.For<BaseMessageHandler>()
);
var messageHandler = container.Resolve<BaseMessageHandler>();
Assert.AreEqual(typeof(DataStore1), messageHandler.FirstStorage.GetType());
Assert.AreEqual(typeof(DataStore2), messageHandler.SecondStorage.GetType());
}
}
alternative you may use:
case "FirstStorage":
return handlers.Single(h => h.ComponentModel.Name == "FirstChoice").Resolve(context);
case "SecondStorage":
return handlers.Single(h => h.ComponentModel.Name == "SecondChoice").Resolve(context);
to resolve on component name rather then implementation type.

castle windsor register a generic interface and retrieve all registered instances the inherit indirectly from it

I have the following structure -
public interface IBaseInterface<T>
{
}
public interface IChildInterface1<Class1> : IBaseInterface<Class1>
{
}
public interface IChildInterface2<Class2> : IBaseInterface<Class2>
{
}
public class ImplementationClass1 : IChildInterface1<Class1>
{
}
public class ImplementationClass2 : IChildInterface2<Class2>
{
}
I want to register all types in the assembly that implement IBaseInterface directly or indirectly, so I want to be able to register ImplementationClass1 and ImplementationClass2 given IBaseInterface. And then I want to retrieve any concrete type based on the generic type definition of ChildInterface classes.
I am registering types like this -
container.Register(
AllTypes.FromAssemblyContaining<Class1>()
.BasedOn(typeof(IBaseInterface<>))
.WithService.Base()
.Configure(c => c.LifeStyle.Transient)
);
and then resolving the type using
var impl = container.Resolve(typeof(IChildInterface1<>))
I expected this to give me an instance of ImplementationClass1, however I get the error -
No component for supporting the service IChildInterface`1 was found
Can someone figure out what I am doing wrong, is this even a valid scenario that I am attempting?
This is my whole code -
using Castle.MicroKernel.Registration;
namespace complexresolutionioc
{
class Program
{
static void Main(string[] args)
{
var container = new Castle.Windsor.WindsorContainer();
container.Register(
AllTypes.FromAssemblyContaining<Class1>()
.BasedOn(typeof(IBaseInterface<>))
.WithService.AllInterfaces()
.Configure(c => c.LifeStyle.Transient)
);
var thisWorks = container.Resolve<IChildInterface1<Class2>>();
}
}
public interface IBaseInterface<T>
{
}
public interface IChildInterface1<Class1> : IBaseInterface<Class1>
{
}
public interface IChildInterface2<Class2> : IBaseInterface<Class2>
{
}
public class ImplementationClass1 : IChildInterface1<Class1>
{
}
public class ImplementationClass2 : IChildInterface2<Class2>
{
}
public class Class1 { }
public class Class2 { }
}
gives me this error -
No component for supporting the service complexresolutionioc.IChildInterface1`1[[complexresolutionioc.Class2, complexresolutionioc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] was found
at Castle.MicroKernel.DefaultKernel.Resolve(Type service) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\MicroKernel\DefaultKernel_Resolve.cs:line 172
at Castle.Windsor.WindsorContainer.Resolve(Type service) in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 889
at Castle.Windsor.WindsorContainer.ResolveT in c:\TeamCity\buildAgent\work\1ab5e0b25b145b19\src\Castle.Windsor\Windsor\WindsorContainer.cs:line 978
at complexresolutionioc.Program.Main(String[] args) in C:\MyDev\complexresolutionioc\complexresolutionioc\Program.cs:line 21
-ilias
You need to provide closed generic type you want to resolve:
var thisWorks = container.Resolve<IChildInterface1<Class2>>();