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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:02:09+00:00 2026-05-26T02:02:09+00:00

Let’s say I have the following classes and interfaces in my MVC 2 project:

  • 0

Let’s say I have the following classes and interfaces in my MVC 2 project:

Repositories:

IRepository1, IRepository2, IRepository3

public class ConcreteRepository1 : IRepository1
{
     public ConcreteRepository1()
     {
         ...
     }
     ...
}

public class ConcreteRepository2 : IRepository2
{
     public ConcreteRepository2()
     {
         ...
     }
     ...
}

public class ConcreteRepository3 : IRepository3
{
     public ConcreteRepository3()
     {
         ...
     }
     ...
}

Service classes:

public class Service1
{
    private IRepository1 repo1;
    private IRepository2 repo2;

    public Service1(IRepository1 repo1, IRepository2 repo2)
    {
        this.repo1 = repo1;
        this.repo2 = repo2;
    }
    ...
}

Controllers:

public class Controller1 : Controller
{
    private Service1 srv1;
    private Service2 srv2;

    public Controller1(Service1 srv1, Service2 srv2)
    {
        this.srv1 = srv1;
        this.srv2 = srv2;
    }
    ...
}

I have custom ControllerFactory and I know how to tie concrete repositories to interfaces:

IUnityContainer container = new UnityContainer();
container.RegisterType<IRepository1, ConcreteRepository1>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository2, ConcreteRepository2>(new TransientLifetimeManager(), new InjectionConstructor());
container.RegisterType<IRepository3, ConcreteRepository3>(new TransientLifetimeManager(), new InjectionConstructor());

The question is how should I register services’ instances and controllers’ types inside my custom ControllerFactory to make unity container resolve the whole hierarchy Controller->Service->Repository and avoid calling Resolve inside controllers or services?

Thanks.

  • 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-26T02:02:10+00:00Added an answer on May 26, 2026 at 2:02 am

    If you have already registered IRepository1-3, so you can get Service1 instance simply calling

    container.Resolve<Service1>()
    

    Calling container.Resolve<Controller1>() will automatically resolve dependencies and will create instance of type Controller1.

    Sample:

    public interface IRepository1 { }
    public interface IRepository2 { }
    public interface IRepository3 { }
    
    public class ConcreteRepository1 : IRepository1 { }
    public class ConcreteRepository2 : IRepository2 { }
    public class ConcreteRepository3 : IRepository3 { }
    
    public class Service1
    {
        private IRepository1 repo1;
        private IRepository2 repo2;
    
        public Service1(IRepository1 repo1, IRepository2 repo2)
        {
            this.repo1 = repo1;
            this.repo2 = repo2;
        }
    }
    
    public class Service2
    {
        private IRepository1 repo1;
        private IRepository2 repo2;
    
        public Service2(IRepository1 repo1, IRepository3 repo3)
        {
            this.repo1 = repo1;
            this.repo2 = repo2;
        }
    }
    
    public class Controller1
    {
        private Service1 srv1;
        private Service2 srv2;
    
        public Controller1(Service1 srv1, Service2 srv2)
        {
            this.srv1 = srv1;
            this.srv2 = srv2;
        }
    }
    

    Resolving:

    var container = new UnityContainer();
    container.RegisterType<IRepository1, ConcreteRepository1>();
    container.RegisterType<IRepository2, ConcreteRepository2>();
    container.RegisterType<IRepository3, ConcreteRepository3>();
    
    var controller = container.Resolve<Controller1>();
    

    EDIT:

    public interface IRepository { }
    public class Repository : IRepository { }
    
    public class Service
    {
        //[InjectionConstructor]
        public Service()
        {
            Console.WriteLine("Parameterless constructor called");
        }
    
        public Service(IRepository repository)
        {
            Console.WriteLine("Contructor with IRepository called");
        }
    }
    
    private static void Main()
    {
        var container = new UnityContainer();
        container.RegisterType<IRepository, Repository>();
        var service = container.Resolve<Service>();
        container.RegisterType<Service>(new InjectionConstructor());
        var service2 = container.Resolve<Service>();
    }
    

    Output:

    Contructor with IRepository called
    Parameterless constructor called
    

    So, when we haven’t registered Service (in other words, it has default resolving behavior), Unity constructs Service using greediest constructor by default. When you specify new InjectionConstructor(), this tells Unity to use parameterless constructor. The same behavior can be received if you mark constructor (in this case public Service()) with InjectionConstructorAttribute.

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

Sidebar

Related Questions

Let's say that I have classes like this: public class Parent { public int
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following models class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model):
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have window.open (without name parameter), scattered in my project and I
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's imagine I have a Java class of the type: public class MyClass {
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress

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.