Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3932690
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:31:04+00:00 2026-05-19T23:31:04+00:00

Problem I currently have a factory that depends on a few parameters to properly

  • 0

Problem

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.

Here is what I currently have. I’ll warn you it’s not pretty.

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<IRole>().ToProvider<RoleProvider>();
    }
}

public class RoleProvider : Provider<IRole>
{
    protected override IRole CreateInstance(IContext context)
    {
        var isNewParameter = context.Parameters
            .Where(x => x.Name == "isNew")
            .Select(x => x.GetValue(context))
            .Cast<bool>()
            .FirstOrDefault();

        if (isNewParameter) return new FooRole();
        return new BarRole();
    }
}

var role = kernel.Get<IRole>(new ConstructorArgument("isNew", true));
Console.WriteLine(role.Name);

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.

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.

Question

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.

Thank you.

Edit

public class RoleFactory : IRoleFactory
{
    public IRole Create(bool isNew)
    {
        if (isNew) return new BarRole();
        return new FooRole();
    }
}

Bind it with Bind<IRoleFactory>().To<RoleFactory>(); and use it like so.

var role = kernel.Get<IRoleFactory>();
Console.WriteLine(role.Create(true).Name);

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 isNew parameter would be true if the factory was called from for example a “Create New Event” button.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:31:05+00:00Added an answer on May 19, 2026 at 11:31 pm

    Ninject provides several machanism to handle such problems. Which one that suits best for you depends on the exact problem. Here are the most likely ones:

    Use named bindings:

    kernel.Bind<IRole>().To<FooRole>().Named("old");
    kernel.Bind<IRole>().To<BarRole>().Named("new");
    kernel.Get<IRole>("new");
    

    Use conditional Bindings

    kernel.Bind<IRole>().To<AdminRole>().When(ctx => UserHasAdminPermission())
    kernel.Bind<IRole>().To<UserRole>();
    kernel.Get<IRole>();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.