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

  • SEARCH
  • Home
  • 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 4082508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:11:47+00:00 2026-05-20T18:11:47+00:00

I think this falls under the concept of contextual binding , but the Ninject

  • 0

I think this falls under the concept of contextual binding, but the Ninject documentation, while very thorough, does not have any examples close enough to my current situation for me to really be certain. I’m still pretty confused.

I basically have classes that represent parameter structures for queries. For instance..

class CurrentUser {
  string Email { get; set; }
}

And then an interface that represents its database retrieval (in the data layer)

class CurrentUserQuery : IQueryFor<CurrentUser> {
   public CurrentUserQuery(ISession session) {
     this.session = session;
   }

   public Member ExecuteQuery(CurrentUser parameters) {
      var member = session.Query<Member>().Where(n => n.Email == CurrentUser.Email);
      // validation logic
      return member;
   }
}

Now then, what I want to do is to establish a simple class that can take a given object and from it get the IQueryFor<T> class, construct it from my Ninject.IKernel (constructor parameter), and perform the ExecuteQuery method on it, passing through the given object.

The only way I have been able to do this was to basically do the following…

Bind<IQueryFor<CurrentUser>>().To<CurrentUserQuery>();

This solves the problem for that one query. But I anticipate there will be a great number of queries… so this method will become not only tedious, but also very prone to redundancy.

I was wondering if there is an inherit way in Ninject to incorporate this kind of behavior.
:-

In the end, my (ideal) way of using this would be …

class HomeController : Controller {
  public HomeController(ITransit transit) {
    // injection of the transit service
  }

  public ActionResult CurrentMember() {
    var member = transit.Send(new CurrentUser{ Email = User.Identity.Name });
  }
}

Obviously that’s not going to work right, since the Send method has no way of knowing the return type.

I’ve been dissecting Rhino Service Bus extensively and project Alexandria to try and make my light, light, lightweight implementation.

Update

I have been able to get a fairly desired result using .NET 4.0 dynamic objects, such as the following…

dynamic Send<T>(object message);

And then declaring my interface…

public interface IQueryFor<T,K>
{
    K Execute(T message);
}

And then its use …

public class TestCurrentMember
{
    public string Email { get; set; }
}

public class TestCurrentMemberQuery : IConsumerFor<TestCurrentMember, Member>
{
    private readonly ISession session;

    public TestCurrentMemberQuery(ISession session) {
        this.session = session;
    }

    public Member Execute(TestCurrentMember user)
    {
        // query the session for the current member
        var member = session.Query<Member>()
            .Where(n => n.Email == user.Email).SingleOrDefault();

        return member;
    }
}

And then in my Controller…

        var member = Transit.Send<TestCurrentMemberQuery>(
            new TestCurrentMember { 
                Email = User.Identity.Name 
            }
        );

effectively using the <T> as my ‘Hey, This is what implements the query parameters!’. It does work, but I feel pretty uncomfortable with it. Is this an inappropriate use of the dynamic function of .NET 4.0? Or is this more the reason why it exists in the first place?

Update (2)

For the sake of consistency and keeping this post relative to just the initial question, I’m opening up a different question for the dynamic issue.

  • 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-20T18:11:48+00:00Added an answer on May 20, 2026 at 6:11 pm

    Yes, you should be able to handle this with Ninject Conventions. I am just learning the Conventions part of Ninject, and the documentation is sparse; however, the source code for the Conventions extension is quite light and easy to read/navigate, also Remo Gloor is very helpful both here and on the mailing list.

    The first thing I would try is a GenericBindingGenerator (changing the filters and scope as needed for your application):

    internal class YourModule : NinjectModule
    {
        public override void Load()
        {
            Kernel.Scan(a => {
                        a.From(System.Reflection.Assembly.GetExecutingAssembly());
                        a.InTransientScope();
                        a.BindWith(new GenericBindingGenerator(typeof(IQueryFor<>)));
                    });
        }
    }
    

    The heart of any BindingGenerator is this interface:

    public interface IBindingGenerator 
    {
        void Process(Type type, Func<IContext, object> scopeCallback, IKernel kernel);
    }
    

    The Default Binding Generator simply checks if the name of the class matches the name of the interface:

    public void Process(Type type, Func<IContext, object> scopeCallback, IKernel kernel)
    {
        if (!type.IsInterface && !type.IsAbstract)
        {
            Type service = type.GetInterface("I" + type.Name, false);
            if (service != null)
            {
                kernel.Bind(service).To(type).InScope(scopeCallback);
            }
        }
    }
    

    The GenericBindingGenerator takes a type as a constructor argument, and checks interfaces on classes scanned to see if the Generic definitions of those interfaces match the type passed into the constructor:

    public GenericBindingGenerator(Type contractType)
    {
        if (!contractType.IsGenericType && !contractType.ContainsGenericParameters)
        {
            throw new ArgumentException("The contract must be an open generic type.", "contractType");
        }
        this._contractType = contractType;
    }
    
    
    public void Process(Type type, Func<IContext, object> scopeCallback, IKernel kernel)
    {
        Type service = this.ResolveClosingInterface(type);
        if (service != null)
        {
            kernel.Bind(service).To(type).InScope(scopeCallback);
        }
    }
    
    public Type ResolveClosingInterface(Type targetType)
    {
        if (!targetType.IsInterface && !targetType.IsAbstract)
        {
            do
            {
                foreach (Type type in targetType.GetInterfaces())
                {
                    if (type.IsGenericType && (type.GetGenericTypeDefinition() == this._contractType))
                    {
                        return type;
                    }
                }
                targetType = targetType.BaseType;
            }
            while (targetType != TypeOfObject);
        }
        return null;
    }
    

    So, when the Conventions extension scans the class CurrentUserQuery it will see the interface IQueryFor<CurrentUser>. The generic definition of that interface is IQueryFor<>, so it will match and that type should get registered for that interface.

    Lastly, there is a RegexBindingGenerator. It tries to match interfaces of the classes scanned to a Regex given as a constructor argument. If you want to see the details of how that operates, you should be able to peruse the source code for it now.

    Also, you should be able to write any implementation of IBindingGenerator that you may need, as the contract is quite simple.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I think this could be a very easy question for you. But I have
(I think this is a pretty basic question on OOP, but unfortunately I wasn't
I think this is nearly impossible or very tricky. I'm using CriteriaBuilder, JPA 2.0,
I think this is a pretty straightforward problem but... var outerHeight = $('.profile').outerHeight(); $(#total-height).text(outerHeight
I think this is mostly because I'm new to PHP OOP, but I have
I think this question has been asked many a times but I've been searching
I think this is an overly easy question but my brain is failing to
As near as I can tell, this falls under a you can't do that.
I think this is easily explained by looking at code, so I'm posting a
I think this is a simple and a silly question. I have included a

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.