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 6002135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:56:02+00:00 2026-05-23T00:56:02+00:00

I have an abstract class: public abstract class Validator<T> : IValidator and a couple

  • 0

I have an abstract class:

 public abstract class Validator<T> : IValidator

and a couple of classes that implement this class for specific purposes, e.g.

public sealed class NewsValidator : Validator<News>

Now using Ninject i want to do Dependency Injection like the following (this particular code is NOT working):

Bind<Validator<News>>().To<NewsValidator>();
Bind(typeof(Validator<>)).To(typeof(NullValidator<>));

So what I want to achieve is that

Validator<News>

Should be bound to the Class “NewsValidator”, but if any other not-bound version of this class is requested, say

Validator<Article>
Validator<SomethingElse>

that should be bound to a default Class (NullValidator). Using the code used above throws an Exception, though, because it binds the Validator < News > both to the NewsValidator as well as to the NullValidator.

How could I implement this? Particular types of the generic class should be bound to individual classes. All other types of the generic class that were not explicitly bound should be bound to a default class.

Would be really glad about a couple of suggestions! Thanks!

  • 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-23T00:56:03+00:00Added an answer on May 23, 2026 at 12:56 am

    You could create a custom implementation of IMissingBindingResolver.

    Whenever the kernel fails to resolve a binding for a requested service it delegates to the HandleMissingBinding method (this is true for any kernel derived from KernelBase). The HandleMissingBinding method will ask every missing binding resolver if it can create a binding for the requested service. The bindings returned by the resolvers, if any, will be added to the kernel.

    Note that any binding created by a missing binding resolver will be added to the kernel as an implicit binding. This could have implication on your application. For example, if you have a mixture of explicit and implicit bindings for a service, resolving these bindings, i.e. kernel.GetAll<TService>(), only resolves explicit bindings. However, if all bindings are implicit they will all be resolved.

    Ninject has two standard implementations of IMissingBindingResolver:

    • DefaultValueBindingResolver
    • SelfBindingResolver

    Let’s implement a custom resolver for the null validators.

    public class MissingValidatorResolver : NinjectComponent, IMissingBindingResolver
    {
        public IEnumerable<IBinding> Resolve(
            Multimap<Type, IBinding> bindings, IRequest request)
        {
            var service = request.Service;
            if (!typeof(IValidator).IsAssignableFrom(service))
            {
                return Enumerable.Empty<IBinding>();
            }
    
            var type = service.GetGenericArguments()[0];
            var validatorType = typeof(NullValidator<>).MakeGenericType(type);
    
            var binding = new Binding(service)
            {
                ProviderCallback = StandardProvider.GetCreationCallback(validatorType)
            };
    
            return new[] { binding };
        }
    }
    

    Now the following test (using xUnit.net) passes.

    [Fact]
    public void ShouldResolveNonBoundValidatorDerivedFromValidatorAsNullValidator()
    {
        var kernel = new StandardKernel();
        kernel.Components.Add<IMissingBindingResolver, MissingValidatorResolver>();
    
        var validator = kernel.Get<Validator<Article>>();
        Assert.IsType<NullValidator<Article>>(validator);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have classes declared like this: public abstract class IdentifiableEntity { public boolean
If I have a base class such that public abstract class XMLSubscription <T extends
Consider this piece of code: public abstract class Validator { protected Validator() { }
I have an abstract class: type TInterfaceMethod = class abstract public destructor Destroy; virtual;
I have an abstract base class and derived class: type TInterfaceMethod = class public
I have an abstract base class with a TcpClient field: public abstract class ControllerBase
I have the following class: public abstract class AbstractParent { static String method() {
In my domain model I have an abstract class CommunicationChannelSpecification, which has child classes
I have an abstract generic class public abstract class Foo<TType> with an abstract method
i have an abstract class public abstract class Document { public int DocumentID {get;

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.