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

The Archive Base Latest Questions

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

i am new in DI pattern…now just learning.i got a code for Constructor Injection

  • 0

i am new in DI pattern…now just learning.i got a code for Constructor Injection using unity. 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>();

here we can see CustomerService ctor looking for LoggingService instance but here when we create instance of CustomerService through resolve then we are not passing the instance of LoggingService. so tell me how could will work. any one explain it with small complete sample code. 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-06-17T14:06:52+00:00Added an answer on June 17, 2026 at 2:06 pm

    The code would look something like this:

    public interface ILoggingService
    {
        void WriteToLog(string logMsg);
    }
    
    public class LoggingService : ILoggingService
    {
        public void WriteToLog(string logMsg)
        {
            ... WriteToLog implementation ...
        }
    }
    
    public interface ICustomerService
    {
        ... Methods and properties here ...
    }
    
    public class CustomerService : ICustomerService
    {
    
        // injected property
        public ISomeProperty SomeProperty { get; set; }
    
        public CustomerService(ILoggingService myServiceInstance)
        { 
            // work with the dependent instance
            myServiceInstance.WriteToLog("SomeValue");
        }
    } 
    
    ...
    ...
    
    // Bootstrap the container. This is typically part of your application startup.
    IUnityContainer container = new UnityContainer();
    container.RegisterType<ILoggingService, LoggingService>();
    
    // Register ICustomerService along with injected property
    container.RegisterType<ICustomerService, Customerservice>(
                                new InjectionProperty("SomeProperty", 
                                    new ResolvedParameter<ISomeInterface>()));
    ...
    ...
    
    ICustomerService myInstance = container.Resolve<ICustomerService>();
    

    So when you resolve your ICustomerService interface, unity will return a new instance of CustomerService. When it instantiates the CustomerService object, it will see that it needs an ILoggingService implementation and will determine that LoggingService is the class it wants to instantiate.

    There’s more to it, but that’s the basics.

    Update – Added parameter injection

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

Sidebar

Related Questions

I'm pretty new to WPF and using the MVVM design pattern. To help learn
I have the following code: public void DriveRecursion(string retPath) { string pattern = @[~#&!%\+\{\}]+;
Learnt JUnit yesterday, learning Mockito today I wrote a simple class; public class FileOperations
Introduction I just thought of a new design pattern. I'm wondering if it exists,
I am doing probes with the new TAP pattern, using Task and CTP to
I'm trying to parse a string but I'm new to pattern matching. Here's my
I'm new to pattern matching, having finally figured it out. I am stuck trying
I am new to the MVVM design pattern, and I am working on a
I am new to Regex .I want to match pattern of my file ..
I am new to sencha touch. I am beginning with MVC pattern. I want

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.