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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:11:25+00:00 2026-06-17T17:11:25+00:00

I’m using the Simple Injector as my IoC container. I have developed a number

  • 0

I’m using the Simple Injector as my IoC container. I have developed a number of (excuse me probably using the wrong term) partially closed implementations, for a single generic interface.

I would like to be in a position to request the generic interface, and based on the types supplied, have Simple Injector return the correct class implementation. (I can understand this may be a no-no as implementations could overlap if done wrong etc. but I’d still like to know if it can be done.)

Based on the code snippets below, how can I configure Simple Injector to return me an instance of ITrim<Ford, Green>?

Common base class layer:

public interface IColour { }
public interface IVehicle { }
public interface ITrim<TVehicle, TColour>
    where TVehicle : IVehicle
    where TColour : IColour
{
    void Trim(TVehicle vehicle, TColour colour);
}

public abstract class TrimVehicle<TVehicle, TColour> : ITrim<TVehicle, TColour>
    where TVehicle : IVehicle
    where TColour : IColour
{
    public virtual void Trim(TVehicle vehicle, TColour colour) { }
}

Middle layer, providing common code for a type of vehicle:

public abstract class Green : IColour { }
public abstract class Blue : IColour { }
public abstract class Car : IVehicle { }
public abstract class Bike : IVehicle { }

public abstract class TrimCar<TCar, TColour> : TrimVehicle<TCar, TColour>
    where TCar : Car
    where TColour : IColour
{
    public override void Trim(TVehicle vehicle, TColour colour)
    {
        base.Trim(vehicle, colour);
    }
}

public abstract class TrimBike<TBike, TColour> : TrimVehicle<TBike, TColour>
    where TBike : Bike
    where TColour : IColour
{
    public override void Trim(TVehicle vehicle, TColour colour)
    {
        base.Trim(vehicle, colour);
    }
}

Final layer, providing more specific implementations:

public class Ford : Car { }
public class TrimFord<TFord, TColour> : TrimCar<TFord, TColour>
    where TFord : Ford
    where TColour : IColour
{
    public override void Trim(TVehicle vehicle, TColour colour)
    {
        base.Trim(vehicle, colour);
    }
}

public class Yamaha : Bike { }
public class TrimYamaha<TYamaha, TColour> : TrimBike<TYamaha, TColour>
    where TYamaha : Yamaha
    where TColour : IColour
{
    public override void Trim(TVehicle vehicle, TColour colour)
    {
        base.Trim(vehicle, colour);
    }
}
  • 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-06-17T17:11:27+00:00Added an answer on June 17, 2026 at 5:11 pm

    TLDR;

    container.Register(typeof(ITrim<,>), typeof(ITrim<,>).Assembly);
    

    Long, but outdated answer:

    A very complicated question, with a very simple answer:

    container.RegisterOpenGeneric(typeof(ITrim<,>), typeof(TrimCar<,>));
    container.RegisterOpenGeneric(typeof(ITrim<,>), typeof(TrimFord<,>));
    container.RegisterOpenGeneric(typeof(ITrim<,>), typeof(TrimYamaha<,>));
    

    This works, because Simple Injector respects any generic type constraint of a given type (or at least, any it handles any nasty bizarre type constraint that I could think of). So as long as you make sure the registered open generic types (TrimCar, TrimFord, and TrimYamaha) do not overlap, it will work as expected.

    If they do overlap, the container will throw an exception telling you that multiple observers of the ResolveUnregisteredType event tried to register {some type}.

    Although you should be careful that the use of these overlapping types does not complicate your application, in general I find the use of generic type constraints very convenient and I use it all the time (especially when registering decorators).

    UPDATE

    If you have a set of non-generic decorators, the current implementation of RegisterManyForOpenGeneric can not distinguish them from ‘normal’ types, and it tries to register them. If you don’t want that, you can register your types as follows:

    var types = OpenGenericBatchRegistrationExtensions.GetTypesToRegister(
       typeof(ITrim<,>), typeof(ITrim<,>).Assembly)
       .Where(type => !type.Name.EndsWith("Decorator");
    
    container.RegisterManyForOpenGeneric(typeof(ITrim<,>), types);
    

    The RegisterManyForOpenGeneric extension methods use the GetTypesToRegister internally as well.

    UPDATE 2

    The RegisterManyForOpenGeneric method of Simple Injector 2 now recognizes non-generic decorators, so with v2 you are able to simply do this:

    container.RegisterManyForOpenGeneric(
        typeof(ITrim<,>), 
        typeof(ITrim<,>).Assembly);
    

    Much easier, don’t you think?

    UPDATE 3

    Simple Injector v3 obsoleted RegisterManyForOpenGeneric and removed them completely from v4. For v3 and up, Register can be used instead:

    container.Register(typeof(ITrim<,>), typeof(ITrim<,>).Assembly);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am using JSon response to parse title,date content and thumbnail images and place
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.