Resolving injected instances when using Activator.CreateInstance?

AFAIK you can register in castle windsor you can register the so called "named instances" so you can create the object you need by resolving them througth the container without dealing with Activator. CreateInstance that for sure can't perform IoC. Basically you have to register your component with a key.

AFAIK you can register in castle windsor you can register the so called "named instances" so you can create the object you need by resolving them througth the container without dealing with Activator. CreateInstance that for sure can't perform IoC. Basically you have to register your component with a key: AddComponent(String key, Type classType) and then call Resolve(string Key) to have back your component properly created with all dependencies resoved.

Thanks Felice, your answer has been really helpful! I'll post my final solution below as another answer and set yours as the accepted answer. Cheers for the help and the nudge in the right direction.

– Jamie Dixon Feb 6 at 22:11 you're welcome! – Felice Pollano Feb 6 at 22:16.

To expand on the answer given by Felice I thought it'd be useful to post the solution I came to based on the accepted answer. Currently my commands are mapped via an IDictionary but will be moved to another medium soon (XML, JSON, etc). Here's how I'm registering the compnents for user input commands: public void InstallUserCommands(IWindsorContainer container) { var commandToClassMappings = new Dictionary { {"move", "MoveCommand"}, {"locate","LocateSelfCommand"}, {"lookaround","LookAroundCommand"}, {"bag","LookInBagCommand"} }; foreach (var command in commandToClassMappings) { var commandType = Type.

GetType("TheGrid.Commands. UserInputCommands. " + command.

Value); container. Register(Component. For(commandType).

Named(command. Key)); } } and to resolve the instance: public UserCommandInputMapperResponse Invoke(UserCommandInputMapperRequest request) { var container = new WindsorContainer(); container. Install(FromAssembly.This()); IUserInputCommand instance; try { instance = container.

Resolve(request.CommandName.ToLower().Trim()); } catch (Exception) { instance = null; } return new UserCommandInputMapperResponse { CommandInstance = instance }; }.

A better implementation, Windsor-wise, would be to utilize a typed factory. With a typed factory, your code would not have to reference the container, because a factory implementation would be automatically created for you. With a typed factory, your factory could look like this: public interface IUserInputCommandFactory { IUserInputCommand GetMove(); IUserInputCommand GetLocate(); IUserInputCommand GetLookAround(); IUserInputCommand GetBag(); } and Windsor would forward each factory method to a corresponding resolve - e.g. GetMove would become container.

Resolve("Move"). See typed factory docs ("'get' methods lookup by name) for more information. I think this is one of the places where Windsor really shines :).

Oooh thanks Mogens. That looks awesome. I'll have a further look at this tomorrow.

Cheers for the help! – Jamie Dixon Feb 7 at 9:25.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions