Note that there are some explanatory texts on larger screens.

plurals
  1. PODynamically determining dependency based on user parameters
    primarykey
    data
    text
    <h3>Problem</h3> <p>I currently have a factory that depends on a few parameters to properly decide which object to return. This factory is not yet bound to DI. As I understand it NInject uses providers as a factory.</p> <p>Here is what I currently have. I'll warn you it's not pretty.</p> <pre><code>public interface IRole { string Name { get; } } public class FooRole : IRole { public string Name { get { return "Foo Role"; } } } public class BarRole : IRole { public string Name { get { return "Bar Role"; } } } public class FooBarRoleModule : NinjectModule { public override void Load() { Bind&lt;IRole&gt;().ToProvider&lt;RoleProvider&gt;(); } } public class RoleProvider : Provider&lt;IRole&gt; { protected override IRole CreateInstance(IContext context) { var isNewParameter = context.Parameters .Where(x =&gt; x.Name == "isNew") .Select(x =&gt; x.GetValue(context)) .Cast&lt;bool&gt;() .FirstOrDefault(); if (isNewParameter) return new FooRole(); return new BarRole(); } } var role = kernel.Get&lt;IRole&gt;(new ConstructorArgument("isNew", true)); Console.WriteLine(role.Name); </code></pre> <p>I'm almost certain this is not the proper way of dynamically injecting dependencies, however it does work. And frankly, looking at what I've hacked together doesn't seem to make much sense.</p> <p>The reason I need to dynamically resolve the dependency is that during the life cycle of the application the end-user can assume several roles. The sample posted above is pretty simply but the parameters (currently only isNew) would determine would object to inject.</p> <h3>Question</h3> <p>What is the proper way to use a provider with parameters not known at run time? These parameters would be passed in whenever the user triggers the code that could give them a different role.</p> <p>Thank you.</p> <h3>Edit</h3> <pre><code>public class RoleFactory : IRoleFactory { public IRole Create(bool isNew) { if (isNew) return new BarRole(); return new FooRole(); } } </code></pre> <p>Bind it with <code>Bind&lt;IRoleFactory&gt;().To&lt;RoleFactory&gt;();</code> and use it like so.</p> <pre><code>var role = kernel.Get&lt;IRoleFactory&gt;(); Console.WriteLine(role.Create(true).Name); </code></pre> <p>This revised version does look better and makes more sense, however I believe I missed some key details in your post. Wanted to add that the <code>isNew</code> parameter would be true if the factory was called from for example a "Create New Event" button.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload