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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:41:28+00:00 2026-05-31T20:41:28+00:00

In autoFac, I can register multiple implementation of an interface. When autofac instantiates my

  • 0

In autoFac, I can register multiple implementation of an interface. When autofac instantiates my object, all instances are passed to the constructor.

From autofac’s documentation: here

For example, when Autofac is injecting a constructor parameter of type
IEnumerable it will not look for a component that supplies
IEnumerable. Instead, the container will find all
implementations of ITask and inject all of them.

Is this functionality available in StructureMap?

For my classes:

public interface IFoo
{

}

public class Foo1 : IFoo
{

}

public class Foo2 : IFoo
{

}

public class UsingFoo
{
    public UsingFoo(IEnumerable<IFoo> allFoos)
    {
        foreach (var foo in allFoos)
        {

        }
    }
}

How do I register my implementations, so that when UsingFoo is instantiated, the constructor will be passed all implementations of IFoo?

  • 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-31T20:41:29+00:00Added an answer on May 31, 2026 at 8:41 pm

    In StructureMap you can do:

    ObjectFactory.Intialize(x => x.Scan(y => y.AddAllTypesOf<IFoo>()));
    

    That will register all types of IFoo

    Then when you resolve UsingFoo, they will be injected.

    Edit:

    I just quickly wrote this up in a console app:

    ObjectFactory.Initialize(x =>
    {
        x.Scan(y =>
        {
            y.AddAllTypesOf<IFoo>();
        });
    });
    
    var usingFoo = ObjectFactory.GetInstance<UsingFoo>();
    

    Edit:

    You made me doubt myself, so I double checked.

    It works fine.

    Here’s a working example I quickly wrote in a console app:

    public interface IFoo
    {
        string Text { get; }
    }
    
    public class Foo1 : IFoo
    {
        public string Text
        {
            get { return "This is from Foo 1"; }
        }
    }
    
    public class Foo2 : IFoo
    {
        public string Text
        {
            get { return "This is from Foo 2"; }
        }
    }
    
    public class Bar
    {
        private readonly IEnumerable<IFoo> _myFoos;
    
        public Bar(IEnumerable<IFoo> myFoos)
        {
            _myFoos = myFoos;
        }
    
        public void Execute()
        {
            foreach (var myFoo in _myFoos)
            {
                Console.WriteLine(myFoo.Text);
            }
        }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            ObjectFactory.Initialize(x =>
            {
                x.UseDefaultStructureMapConfigFile = false;
                x.Scan(y =>
                {
                    y.TheCallingAssembly();
                    y.AddAllTypesOf<IFoo>();
                });
            });
    
            var myBar = ObjectFactory.GetInstance<Bar>();
    
            myBar.Execute();
    
            Console.WriteLine("Done");
            Console.ReadKey();
        }
    }
    

    The output is:

    This is from Foo 1

    This is from Foo 2

    Done

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

Sidebar

Related Questions

I'm trying to autowire up my application using Autofac. All is fine apart from
I need to register my Autofac container with specific interface, for this case I
Should I register ViewModels in Container and resolve from there? Benefits: I can perform
I'm using Autofac and want to resolve the correct implementation of the current assembly
I'm using Autofac. I want to inject a different implementation of a dependency based
I'm using Castle DynamicProxy with Autofac. I have an object for which I've created
I am trying to use Autofac and Autofac.Integrations.Web to register ASP.NET MVC controllers. I
I have registered this with Autofac: builder.Register(c => ViewerFactory.CreateDefault()).SingleInstance(); I'm not sure, but I
Does anyone have any tips or best practices regarding how Autofac can help manage
I can see how I can instantiate objects of classes using autofac, and then

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.