I am trying to add something (I don't know what that something is) to Castle Windsor that will detect when I'm trying to create an instance of a class that derives from System.Activities.Activity, Windsor would inspect the properties of type InArgument<T> and resolve values for these arguments before returning the created instance to the callee.
What are the steps for doing something like this?
Basically, I would have something like this:
var someActivity = container.Resolve<SomeActivity>();
Then Windsor would detect that SomeActivity derives from Activity, it would inspect the properties of type InArgument<T> on SomeActivity and would resolve values for those properties from the container itself.
But SomeActivity could be any Activity, not necessarily specifically SomeActivity.
Windsor by default sets all the properties it can resolve.
Related
I am trying to register all of my typed factories in Castle Windsor using a single registration. All of my factories implement IModelFactory so I would like to be able to write something like:
container.Register(Types.FromThisAssembly()
.BasedOn<IModelFactory>()
.AsFactory());
The BasedOn method returns a type BasedOnDescriptor which does not allow me to call the AsFactory() extension method.
Do I really need to register all typed factories one by one?
.Configure(x => x.AsFactory());
I wonder if it's possible for Castle container to resolve dependency on instance when it is registered as follows:
var inst = Globals.Ribbons.TestRibbon;
this.container.Register(
Component
.For<TestRibbon>()
.Instance(inst)
);
long story short, i work on Excel addIn, using Castle.Windsor as IoC Container, where in some point interaction happens betwen custom Ribbon and CustomTaskPane, as Ribbon is instantiated and loaded by Excel, i have no way to register type and then resolve it with dependencies (or there is a way?)
I have two options to workaround, do some manual Property Injection or reference Container from inside of Ribbon, either way it feels awkward.
Is there a way to register instance with Castle container, so that it would automatically resolve dependencies on that instance?
I have some components implementing the same interface and I would like to chose which one gets injected to my Repository.
Component.For<IRepository>().ImplementedBy<Repository>().<whatShouldGoHere>()
I thought I had this working with DependsOn but now I saw that DependsOn are for static dependecies such as strings. Is the IHandlerSelector the only way forward? I would rather have the declaration inline with the component registration. Maybe a factory method? Are there any recommendations?
Edit
Example Constructor
public PersitentRepository(Func<ISession,string> sessionFactory)
Digging around I realize that the delegate is an artifact from the TypedFactoryFacility. There seems to have been some change so it now resolves by type only. In older Castle versions the string argument was used to select component by name.
A factory would to the trick.
You need to add the FactorySupportFacility to your container for this to work.
For much more detail, see the Castle Windsor documentation at http://docs.castleproject.org/Default.aspx?Page=Factory-Support-Facility&NS=Windsor&AspxAutoDetectCookieSupport=1.
See also http://www.mail-archive.com/castle-project-users#googlegroups.com/msg04463.html.
DependsOn does work for other things than statics, the problem is that the injected delegate does not resolve the way it used to. I ended up registering my own component for handling this specific delegate
container.Register(Component.for<Func<ISession,string>>().ImplementedBy(sessionName => container.resolve<ISession>(sessionName));
(I typed the above from memory so please excuse any typos)
I generally use StructureMap, but on a current project I'm using Castle Windsor (2.1). I've found some code to auto-register types and interfaces:
_container.Register(AllTypes.Pick().FromAssembly(GetType().Assembly).WithService.FirstInterface());
However I would like to add a string dependency to one of the types. I can do this without the convention like this:
var registration = Component.For() .ImplementedBy().DependsOn(new[] { Property.ForKey("someString").Eq("blahblah") });
_container.Register(registration);
Is there any way I can do this with the auto-registered types?
you use ConfigureFor<> method which lets you finetune your registration on a granular level.
I'm trying to implement automatic registration of my listeners to a singleton event aggregator when listeners are created by the IoC container - basically what Jeremy D. Miller is doing, but with Castle instead of StructureMap.
So I want to be able to "intercept" Windsor's object creation mechanism and, if the object supports the marker interface (let's say IListener), call the Subscribe method to an EventAggregator (which is also registered in the container) to make the newly created object a subscriber to events. Also, before the object instance has been released by the container, I want to be able to unsubscribe it.
I'm a little bit confused about what mechanism in Windsor Castle I should use to achieve something like this? I started looking at IInterceptor interface, but it seems to intercept all calls to the object, which is not what I really need (and want to avoid for performance reasons).
IKernel exposes various events like ComponentCreated and ComponentDestroyed which you can use to build that. There are many samples on the web.
Otherwise you could just use the event wiring facility, but it's not convention based.
You could also use OnCreate like this:
container.Register(
Component.For(typeof (Foo)).OnCreate(
(k, c) => {
// ...
eventAggregator.Subscribe(c);
// ...
}));