How to get service type without resolving the service?

33 views
Skip to first unread message

mynkow

unread,
Aug 23, 2012, 7:54:20 AM8/23/12
to castle-pro...@googlegroups.com
Hi,

I have an interface ISomeService and two concrete implementations SSA and SSB. Depending on a configuration container.Resolve<ISomeService>() will return SSA or SSB. Is it possible to get the type which will be resolved without resolving and creating an instance. For example something like this: var theServiceType = container.TheServiceResolvedWillBeOfType<ISomeService>()

Best regards
mynkow

mynkow

unread,
Aug 23, 2012, 7:55:12 AM8/23/12
to castle-pro...@googlegroups.com
Type theServiceType = container.TheServiceResolvedWillBeOfType<ISomeService>()

Patrick Steele

unread,
Aug 23, 2012, 8:37:48 AM8/23/12
to castle-pro...@googlegroups.com
I don't see anything in the API for that.

And this begs the question, "Why do you want to know that?" A
loosely-coupled design wouldn't care of the type -- only the
interface. What are you trying to accomplish?

---
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/-/7o7HyxfDInQJ.
> 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.

mynkow

unread,
Aug 23, 2012, 10:27:23 AM8/23/12
to castle-pro...@googlegroups.com
I have 3 assemblies. Each one has EventHandlers. Only one assembly will work at the same time.
I have IEventBus and OnCreate method within component registration I register only the event handlers from the active assembly;

container.Register(Component.For<IEventBus>().ImplementedBy<InProcessEventBus>().OnCreate((kernel, eventBus) =>
            {
                (eventBus as InProcessEventBus).RegisterAllHandlersInAssembly(Assembly.GetAssembly(typeof(IHaveEventHandlers)), kernel.Resolve, kernel.ReleaseComponent);
                var loginProvider = container.Resolve<ILoginProvider>();
                if (loginProvider.LoginProviderType == LoginProvider.DB_LoginProvider)                     (eventBus as InProcessEventBus).RegisterAllHandlersInAssembly(Assembly.GetAssembly(typeof(IHaveDbLoginProviderEventHandlers)), kernel.Resolve, kernel.ReleaseComponent);                 else if (loginProvider.LoginProviderType == LoginProvider.LDAP_LoginProvider)                     (eventBus as InProcessEventBus).RegisterAllHandlersInAssembly(Assembly.GetAssembly(typeof(IHaveLdapLoginProviderEventHandlers)), kernel.Resolve, kernel.ReleaseComponent);             }));

Patrick Steele

unread,
Aug 28, 2012, 9:31:19 PM8/28/12
to castle-pro...@googlegroups.com
Sorry for dropping off. Got super busy...

What part of the code below is causing you the problem? I'm really
tired, but trying to figure out a more elegant way to do this without
knowing the type that is going to be resolved.

---
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/-/KJ2m6R77yHcJ.

mynkow

unread,
Aug 30, 2012, 5:08:07 PM8/30/12
to castle-pro...@googlegroups.com
Hi Patrick

Each login provider has hardcoded property to identify itself and also each login provider brings its own event handlers. As I said only 1 login provider will be available at the same time.
When the EventBus is started for first time I scan assemblies for EventHandlers. So depending the instance from var loginProvider = container.Resolve<ILoginProvider>(); 
I register the proper EventHandlers. The problem is that I have to resolve ILoginProvider but actually the only thing I need is metadata.

cheers
Reply all
Reply to author
Forward
0 new messages