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

  • Home
  • SEARCH
  • 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 8899163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:47:36+00:00 2026-06-15T00:47:36+00:00

Is it possible to register with the unity container the following recursive structure: public

  • 0

Is it possible to register with the unity container the following recursive structure:

public interface IFoo
{
    IBar[] Bars { get; set; }
}

public interface IBar
{
    IFoo[] Foos { get; set; }
}

Assuming multiple named instances exist for each interface:

public class Foo1 : IFoo 
{
    public IBar[] Bars { get; set; }
}

public class Foo2 : IFoo
{
    public IBar[] Bars { get; set; }
}

public class Bar1 : IBar
{
    public IFoo[] Foos { get; set; }
}

public class Bar2 : IBar
{
    public IFoo[] Foos { get; set; }
}

And the registration:

var container = new UnityContainer();

container.RegisterType<IFoo, Foo1>("foo1");
container.RegisterType<IFoo, Foo2>("foo2");
container.RegisterType<IBar, Bar1>("bar1");
container.RegisterType<IBar, Bar2>("bar2");

var instanceOfBar = container.Resolve<IBar>("bar1");

How to configure Unity container so that the collection properties are automatically injected?

  • 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-15T00:47:38+00:00Added an answer on June 15, 2026 at 12:47 am

    There is a way to annotate a property to be a dependency. But in your case this will not work because of the infiniteness of resolution process. Simply you will get stackoverflow exception. To implement such structures I use lazy pattern so I resolve such collection only if needed:

     Func<IFoo[]> resolver;
     IFoo[] value;
     public IFoo[] Foos { get{
          if(value == null) value = resolver();
             return value;
         } 
     }
    

    to make Func<IFoo[]> resolvable from container add this:

     container.RegisterInstance<Func<IFoo[]>>(e => e.Resolve<IFoo[]>());
    

    resolution of array or elements is done out of the box by unity it simply maps to ResolveAll.

    Then simply inject Func<IFoo[]> through your constructor.

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

Sidebar

Related Questions

I have an interface: public interface IFoo{ string Bar{ get; set; } } and
I'd like to auto-register the common/simple services in my unity container. I think the
Given the following interface: public interface IMyProcessor { void Process(); } I'd like to
Is it possible to register ONE type for TWO interface in StructureMap ? Look:
I have already done the following: Register an instance of my Logger in unity
Is it possible to register an Interface with the RegisterExpectedMemoryLeak procedure? I have a
I'm trying to automatically register all reports in a unity container. All reports implement
In my WCF web application I have configured the Unity container for Interception. Following
In the Windsor IOC container is it possible to register a type that I've
Possible Duplicate: Register the Android App with C2DM so, I've been looking all over

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.