Unity Application Block, How pass a parameter to Injection Factory?

While most DI frameworks have advanced features to do these types of registrations, I personally rather change the design of my application to solve such a problem. This keeps my DI configuration simple and makes the code easier to understand. Especially for the creation of objects that depend on some context (thread, request, whatever) or have a lifetime that must be managed explicitly, I like to define factories.

Factories make these things much more explicit In your situation, you want to fetch a profile for a certain user. This is typically something you would like to have a factory for. Here's an example of this: Definition public interface IProfileFactory { IProfile CreateProfileForUser(string username); } // Usage var profile = Container.Resolve() .

CreateProfileForUser("John"); // Registration Container.RegisterType(); // Mock implementation public class ProfileFactory : IProfileFactory { public IProfile CreateProfileForUser(string username) { IUser user = Container.Resolve() . GetUser(username); return new UserProfile(user); } } I hope this helps.

While most DI frameworks have advanced features to do these types of registrations, I personally rather change the design of my application to solve such a problem. This keeps my DI configuration simple and makes the code easier to understand. Especially for the creation of objects that depend on some context (thread, request, whatever) or have a lifetime that must be managed explicitly, I like to define factories.

Factories make these things much more explicit. In your situation, you want to fetch a profile for a certain user. This is typically something you would like to have a factory for.

Here's an example of this: // Definition public interface IProfileFactory { IProfile CreateProfileForUser(string username); } // Usage var profile = Container.Resolve() . CreateProfileForUser("John"); // Registration Container.RegisterType(); // Mock implementation public class ProfileFactory : IProfileFactory { public IProfile CreateProfileForUser(string username) { IUser user = Container.Resolve() . GetUser(username); return new UserProfile(user); } } I hope this helps.

Resolve method allow passing parameters of ResolverOverride. Subtype of ResolverOverride is ParameterOverride which can be used to pass parameter to resolved constructor. You can do it this way (parameter is Name and passed value is John): Container.

Resolve(new ParameterOverride("Name", "John")).

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