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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:38:43+00:00 2026-06-17T14:38:43+00:00

i am new in Dependency injection. i just got familiar how to implement dependency

  • 0

i am new in Dependency injection. i just got familiar how to implement dependency injection with Interface injection but we know that Dependency injection can be implemented with three way or may be more and those are :-

  1. Interface injection: The service provides an interface which consumers must implement. The interface exposes specific behaviors at run time.
  2. Setter injection: The dependent object exposes a “setter” method to inject the dependency.
  3. Constructor injection: Dependencies are injected through the class constructor

so i am looking for few sample code which can help me to understand how to implement Dependency injection using either Setter injection or Constructor injection using unity. any help with small small code for different way of implementing dependency injection will be appreciated.

i know only Interface injection using unity. here is my code which works fine using Interface injection with unity.

public interface ILogger
{
     void Write(string message);
}

We have define three classes as follows.

public class FileLogger : ILogger
{
     public void Write(string message)
     {
          //Do somthing
     }
}

public class SQLLogger : ILogger
{
     public void Write(string message)
     {
          //Do somthing
     }
}

public class WindowsEventLogger : ILogger
{
     public void Write(string message)
     {
          //Do somthing
     }
}

Need to register and map these classes with interface in configuration file (i.e. app.config).

<configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <alias type="UnityTest.ILogger, UnityTest" alias="ILogger" />
    <namespace name="UnityTest"/>

    <container>
      <register mapTo="UnityTest.FileLogger, UnityTest" name="MyFileLogger" type="ILogger"/>
      <register mapTo="UnityTest.SQLLogger, UnityTest" name="MySQLLogger" type="ILogger"/>
      <register mapTo="UnityTest.WindowsEventLogger, UnityTest" name="MyWindowsEventLogger" type="ILogger"/>
    </container>
</unity>

Note: name attribute is important in register tag.

Finally we have to use this map into our code. So, we have to know that for which one is preferable for specific country.

A dictionary object can be use to keep this mapping as follows.

IDictionary<string, string> loggers = new Dictionary<string, string>();
loggers.Add("USA", "MyFileLogger");
loggers.Add("GBR", "MySQLLogger");
loggers.Add("IN", "MyWindowsEventLogger");

You can populate it from database, xml or another source, and now it’s time to call the Write method.

IUnityContainer container = new UnityContainer();
container.LoadConfiguration();

ILogger logger = container.Resolve<ILogger>(loggers[objUser.countryCode]);
logger.Write("Hello World");

New question

I found a sample code for construction injection with unity but still one thing is not clear. Here is the code.

public class CustomerService
{
  public CustomerService(LoggingService myServiceInstance)
  { 
    // work with the dependent instance
    myServiceInstance.WriteToLog("SomeValue");
  }
} 

IUnityContainer uContainer = new UnityContainer();
CustomerService myInstance = uContainer.Resolve<CustomerService>();

When we write uContainer.Resolve<CustomerService>(); then we are not sending any instance of LoggingService class then how we can create instance of CustomerService class because it’s constructor require instance of LoggingService.

This area is not clear. Please explain to me how it works.

Another question is [Dependency] attribute: what it does, and when a method needs to be decorated with the [Dependency] attribute.

  • 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-17T14:38:44+00:00Added an answer on June 17, 2026 at 2:38 pm

    With your code as a base.

    if you have some class that depends on ILogger, and you have a default registration for ILogger (a registration without name)

    IUnityContainer container = new UnityContainer();
    
    //fixed
    //by default RegisterType will create new instance of the type every time 
    //the container resolves the dependancy 
    container.RegisterType<ILogger,SomeLoggerImplementation>();
    container.RegisterType<Foo>();
    container.RegisterType<Bar>();
    
    //will create a new instance of SomeLoggerImplementation using a default ctor, pass it to the constructor of Foo and return the instance of Foo
    //if SomeLoggerImplementation has some other dependancies they can be registered in the container too! 
    var foo = container.Resolve<Foo>();
    
    //will create a new instance of SomeLoggerImplementation using a default ctor, create a new instance of Bar, 
    //Set the value of the Property Logger (but not the Logger2), and return the instance of Bar
    var bar = container.Resolve<Bar>();
    
    //Constructor injection
    public class Foo
    {
        private ILogger _logger;
        public Foo(ILogger logger)
        {
            //if Foo is resolved from the container , the value for the logger parameter will be provided from the container
            _logger = logger;
        }
    }
    
    
    //property injection
    public class Bar
    {
        //If Bar is resolvced from the container the value for the Logger property will also  be provided from the container
        [Dependency]
        public ILogger Logger { get; set; }
    
        //this will not be injected
        public ILogger Logger2 { get; set; }
    
        public Bar()
        {
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently just starting to implement Dependency injection so I can start testing my
I've been reading about Dependency Injection but the examples I've found just look like
I (think I) understand the purpose of dependency injection, but I'm just not getting
I've currently got four XML files that are used to build my Dependency Injection
i am new in DI pattern...now just learning.i got a code for Constructor Injection
I'm thinking of implementing Objectify DAO with dependency injection, such that I can maintain
I am new to using dependency injection, and am trying on Ninject. As I
I am very new to using dependency injection, and I am trying it out
I am new to mocking and dependency injection and need some guidance. My application
I am new to MVC, repository concept and dependency injection. My repository and DAL

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.