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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:24:56+00:00 2026-05-29T06:24:56+00:00

I will give very simple example. class Implementation: IMyInterface { string mArg; public Implementation(string

  • 0

I will give very simple example.

class Implementation: IMyInterface
{
    string mArg;

    public Implementation(string arg)
    {
        mArg = arg;
    }

    public DoStuff(object param)
    {
        // snip
    }
}

class Decorator: IMyInterface
{
    IMyInterface mWrapped;

    public Decorator(IMyInterface wrapped)
    {
        mWrapped = wrapped;
    }

    public DoStuff(object param)
    {
        var result = mWrapped.DoStuff(param);

        // snip

        return result;
    }
}

Now the argument I need for Implementation constructor I get from user in run-time.

IMyInterface GetMyObject()
{
    string arg = AskUserForString();

    return mContext.Resolve // HOW?
}

So what is the proper way to set this up and resolve to Decorated instance?

The example is simple. But imagine there are more layers (decorators/adapters) and the innermost implementation needs parameter I get in run-time.

  • 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-29T06:24:58+00:00Added an answer on May 29, 2026 at 6:24 am

    I am assuming you are using autofac, as it is in the tags of the question.
    You can try something along these lines:

    public class ImplementationFactory
    {
        private readonly IComponentContext container;
    
        public ImplementationFactory(IComponentContext container)
        {
            this.container = container;
    
        }
        public IMyInterface GetImplementation(string arg)
        {
            return container.Resolve<IMyInterface>(new NamedParameter("arg", arg));            
        }
    }
    

    And the registration/resolving part:

    var builder = new ContainerBuilder();
    builder.RegisterType<ImplementationFactory>();
    builder.RegisterType<Implementation>().Named<IMyInterface>("implementation");
    
    builder.Register((c, args) => new Decorator(c.ResolveNamed<IMyInterface>("implementation", args.First())))
        .As<IMyInterface>();
    
    var container = builder.Build();
    
    var factory = container.Resolve<ImplementationFactory>();
    var impl = factory.GetImplementation("abc"); //returns the decorator
    

    If you have multiple decorators, you can chain them in the order you want within the registration:

    builder.Register((c, args) => new Decorator(c.ResolveNamed<IMyInterface>("implementation", args.First())));
    builder.Register((c, args) => new SecondDecorator(c.Resolve<Decorator>(args.First()))).As<IMyInterface>();
    

    Here is a good article about decorator support in autofac as well.

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

Sidebar

Related Questions

The title is very descriptive. Just in case, I will give an example: START
i will give an example of the problem i have. My XML is like
I need a regex that will give me the following results from each example
I'm authoring a simple userscript that will give the backspace button navigation control like
i have this example: while (...){ echo test:'.$a.',- group:'.$m.' } this will give me
Given a very simple string[] , I am trying to accomplish the following in
Which ORM will give me compile-tested queries? Is linqtosql compile time tested? Edit: Say
Which code snippet will give better performance? The below code segments were written in
AssemblyName.GetAssemblyName().Version.ToString() will give the version number ,,but i need to get version number from
Is there a built in dll that will give me a list of links

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.