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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T06:22:51+00:00 2026-05-15T06:22:51+00:00

Is there a way using IoC, MEF [Imports], or another DI solution to compose

  • 0

Is there a way using IoC, MEF [Imports], or another DI solution to compose dependencies on the fly at object creation time instead of during composition time?

Here’s my current thought. If you have an instance of an object that raises events, but you are not creating the object once and saving it in memory, you have to register the event handlers every time the object is created. As far as I can tell, most IoC containers require you to register all of the classes used in composition and call Compose() to make it hook up all the dependencies.

I think this may be horrible design (I’m dealing with a legacy system here) to do this due to the overhead of object creation, dependency injection, etc… but I was wondering if it was possible using one of the emergent IoC technologies.

Maybe I have some terminology mixed up, but my goal is to avoid writing a framework to “hook up all the events” on an instance of an object, and use something like MEF to [Export] handlers (dependencies) that adhere to a very specific interface and [ImportMany] them into an object instance so my exports get called if the assemblies are there when the application starts. So maybe all of the objects could still be composed when the application starts, but I want the system to find and call all of them as the object is created and destroyed.

  • 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-15T06:22:52+00:00Added an answer on May 15, 2026 at 6:22 am

    Typically the way you handle on-the-fly instantiation in a DI/IoC environment is to use abstract factories. The factory is the only type of class allowed to interact directly with the container, in order to resolve dependencies when instantiating a new object.

    public interface IWidgetFactory
    {
        IWidget CreateWidget(...);
    }
    
    public class MyIocWidgetFactory : IWidgetFactory
    {
        private IoCContainer container;
    
        public MyIocWidgetFactory(IoCContainer container)
        {
            if (container == null)
                throw new ArgumentNullException("container");
            this.container = container;
        }
    
        public IWidget CreateWidget(...)
        {
            // Assumes that the container is configured to create transient objects
            // for IWidget, not a singleton.
            return container.Resolve<IWidget>();
        }
    }
    

    Please don’t confuse this with the service locator anti-pattern; nothing else is allowed to touch the container, just the factory, and it only creates one type of object.

    At this point, you just pass around the IWidgetFactory to whatever needs to create a widget at runtime. That’s it – you’ve maintained DI.

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

Sidebar

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.