Support for Guice's annotatedWith registration

39 views
Skip to first unread message

Kim Birkelund

unread,
May 2, 2012, 7:11:00 AM5/2/12
to castle-pro...@googlegroups.com
Hi

I've been looking for and trying to build a facility that provides the same support as Guice's annotatedWith (http://code.google.com/p/google-guice/wiki/BindingAnnotations) but for Windsor.

Basically what I want is to be able to make a registrations like:

container.Register(Component.For<IFoo>().Instance(foo1));

container.Register(Component.For<IFoo>().Instance(foo2).AttributedWith(typeof(SomeAttribute)));

And then declare a type depending on IFoo:

public class Bar {
   public Bar([Some]IFoo foo) {...}
}

Which would then get the foo2 instance instead of foo1 which it normally would.

I have been unable to find something that already provides this and so far haven't had luck enough to implement it myself, though I'll keep trying :)

I've been able to created the extension method allowing me to do the registration and added the attribute info (using .AddAttributeDescription) to the registration - currently it just a key/value pair containing a key and the fully qualified name of the attribute type.

I've tried using the DependencyResolving event on the kernel but can't figure out 1) how to get all the registered components so I can figure out which has the attribute I'm looking for, and 2) how to replace the dependency that has already been resolved.

Any suggestions of how to do this i welcomed, also if anyone knows how I could support syntax like .AttributedWith<SomeAttribute>() instead of passing the type using typeof I'd like that too.


Regards

Kim

Patrick Steele

unread,
May 2, 2012, 10:00:48 AM5/2/12
to castle-pro...@googlegroups.com
Wouldn't service overrides work in this case?

http://docs.castleproject.org/Windsor.Registering-components-one-by-one.ashx#Supplying_the_component_for_a_dependency_to_use_Service_override_9

---
Patrick Steele
http://weblogs.asp.net/psteele
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/castle-project-users/-/Aeq4f5aEligJ.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to
> castle-project-u...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/castle-project-users?hl=en.

Kim Birkelund

unread,
May 2, 2012, 2:46:07 PM5/2/12
to castle-pro...@googlegroups.com
Hi

Yeah, I think I could do it with ServiceOverrides - or actually with DependsOn - but from where?

If I understand it correctly I would need to place an override on the registration of Bar, which is exactly want I don't want to do. If I simply wanted to configure which one should be used that would be easy, but that's not what I want.

What I need is someplace where I can hook a facility in that can modify the ComponentRegistration object or can do something else that has the same effect.

So that when I call container.Register(ComponentRegistration.For<IBar>().ImplementedBy<Bar>()); something happens that connects the second IFoo (from example in first post) to the IFoo dependency in Bar, without it being part of the configuration of Bar.


/kim


On Wednesday, 2 May 2012 16:00:48 UTC+2, Patrick Steele wrote:
> To post to this group, send email to castle-project-users@googlegroups.com.
> To unsubscribe from this group, send email to

Kim Birkelund

unread,
May 2, 2012, 4:01:41 PM5/2/12
to castle-pro...@googlegroups.com
I found a way to do it.

Pasted code for facility here:  http://pastebin.com/raw.php?i=DBeRmESi 


With the WithAttributedFacility the program prints Bar2Bar2 and without it, it prints BarBar.

Any feedback is appreciated.


/kim
Reply all
Reply to author
Forward
0 new messages