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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T16:48:52+00:00 2026-05-28T16:48:52+00:00

This is a fairly straight forward decorator pattern scenario, with the complication that the

  • 0

This is a fairly straight forward decorator pattern scenario, with the complication that the decorated type has a constructor parameter that is dependent on the type into which it is being injected.

I have an interface like this:

interface IThing
{
    void Do();
}

And an implementation like this:

class RealThing : IThing
{
    public RealThing(string configuration)
    {
        ... implementation ...
    }

    public void Do()
    {
        ... implementation ...
    }
}

And a decorator like this:

class DecoratingThing : IThing
{
    IThing _innerThing;

    public DecoratingThing(IThing thing)
    {
        _innerThing = thing;    
    }

    public void Do()
    {
        _innerThing.Do();
    }
}

Finally, I have some types that require an IThing, called Depender1, Depender2 etc..

class DependerX()
{
    public DependerX(IThing thing)
    {
        ... implementation ...
    }
}

I want to configure an IOC container to resolve instances of DependerX such that they are injected with RealThing decorated with a DecoratingThing. Important: Each DependerX type requires a different value of configuration to be passed to the constructor of its RealThing, say “ConfigX” in each case. e.g. The work done by the IoC container might be:

new Depender1(new DecoratingThing(new RealThing("Config1")));
new Depender2(new DecoratingThing(new RealThing("Config2")));

… and so on.

In Unity, this seems quite clunky to configure as I have to mix in the decorator with the decorated:

container.RegisterType<IThing, DecoratingThing>("ConfigX",
    new InjectionFactory(container => new DecoratingThing(new RealThing("ConfigX"));

container.RegisterType<DependerX>(
    new InjectionConstructor(new ResolvedParameter<IThing>("ConfigX");

And repeat, violating DRY nicely, for each DependerX.

What I’d like to do is remove the need to embed the construction of RealThing in the construction of DecoratingThing in each named registration of IThing – and declare the decoration just once. This is so, for example, that if the decoration needs to change in future, it’s easier to reconfigure. The best I came up with is this helper method for registration:

void RegisterDepender<TDepender>(IUnityContainer container, string config)
{
    container.RegisterType<TDepender>(new InjectionConstructor(
        new ResolvedParameter<IThing>(config)));
    container.RegisterType<IThing, DecoratingThing>(config,
        new InjectionFactory(c => new DecoratingThing(new RealThing(config))));
}

This removes repetition at least, but I still have to embed the construction of the RealThing inside the DecoratingThing – this means I can’t vary their lifetimes independently for example. I can’t register IThing again to do this because I’ve used up my registration of that interface for the name. If I want to do that I have to introduce another set of named instances like so:

void RegisterDepender<TDepender>(IUnityContainer container, string config)
{
    string realConfig = "Real" + config;

    container.RegisterType<TDepender>(new InjectionConstructor(
        new ResolvedParameter<IThing>(config)));
    container.RegisterType<IThing, DecoratingThing>(config,
        new InjectionFactory(c => new DecoratingThing(
            container.Resolve<IThing>(realConfig))));
    container.RegisterType<IThing, RealThing>(realConfig,
        new ContainerControlledLifetimeManager(),
        new InjectionConstructor(config));
}

Is this really the best option? It feels complex and potentially hard for those that will come after to grok. Do other IoC containers have a compelling way to cover this scenario? Since the pattern for how injection works is repeated for each DependerX, is there a way to only use a named instance at the top (DependerX) level?

Any other comments?

  • 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-28T16:48:53+00:00Added an answer on May 28, 2026 at 4:48 pm

    The class design itself seems reasonable. Here’s a convention-based container configuration that basically does this:

    public class MyConventions : UnityContainerExtension
    {
        protected override void Initialize()
        {
            var dependers = from t in typeof(IThing).Assembly.GetExportedTypes()
                            where t.Name.StartsWith("Depender")
                            select t;
    
            foreach (var t in dependers)
            {
                var number = t.Name.TrimStart("Depender".ToArray());
                var realName = "Real" + number;
                var decoName = "Deco" + number;
                var config = "Config" + number;
                this.Container.RegisterType<IThing, RealThing>(realName, 
                    new InjectionConstructor(config));
                this.Container.RegisterType<IThing, DecoratingThing>(decoName,
                    new InjectionConstructor(
                        new ResolvedParameter<IThing>(realName)));
                this.Container.RegisterType(t,
                    new InjectionConstructor(
                        new ResolvedParameter<IThing>(decoName)));
            }
        }
    }
    

    This configuration will automatically add all classes that match the above predicate, so once you’ve set it up, you can just add more classes (like Depender4 or Depender5) without revisiting the container configuration at all.

    The above configuration satisfies these unit tests:

    [Fact]
    public void ContainerCorrectlyResolvesDepender1()
    {
        var container = new UnityContainer().AddNewExtension<MyConventions>();
        var actual = container.Resolve<Depender1>();
    
        var deco = Assert.IsAssignableFrom<DecoratingThing>(actual.Thing);
        var thing = Assert.IsAssignableFrom<RealThing>(deco.Thing);
        Assert.Equal("Config1", thing.Configuration);
    }
    
    [Fact]
    public void ContainerCorrectlyResolvesDepender2()
    {
        var container = new UnityContainer().AddNewExtension<MyConventions>();
        var actual = container.Resolve<Depender2>();
    
        var deco = Assert.IsAssignableFrom<DecoratingThing>(actual.Thing);
        var thing = Assert.IsAssignableFrom<RealThing>(deco.Thing);
        Assert.Equal("Config2", thing.Configuration);
    }
    
    [Fact]
    public void ContainerCorrectlyResolvesDepender3()
    {
        var container = new UnityContainer().AddNewExtension<MyConventions>();
        var actual = container.Resolve<Depender3>();
    
        var deco = Assert.IsAssignableFrom<DecoratingThing>(actual.Thing);
        var thing = Assert.IsAssignableFrom<RealThing>(deco.Thing);
        Assert.Equal("Config3", thing.Configuration);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My web app, up until this point, has been fairly straight forward. I have
I have a fairly straight-forward sequential approval workflow that has an EnableModificationActivity that is
I have a fairly straight-forward scenario that I am trying to solve but I'm
This seems like it should be fairly straight-forward, but I can't see anything obvious.
I'm converting a C++ application into C# which has generally been fairly straight forward,
This should be fairly straight forward but is proving not to be. I want
this question is fairly straight forward: is it recommended to place a semicolon when
This is a fairly straight forward question but I'm having a problem finding a
This should be a fairly straight forward python question, but I'm getting stuck getting
This is a fairly common problem, it probably has a name, I just don't

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.