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

3
  • It's not entirely clear what you're asking. Presumably LoggingService would be an interface, not a concrete type and When you bootstrap the container, you would provide it with a mapping from your LoggingService interface to the concrete type. When you ask the container to reoslve CustomerService, it would see that the constructor takes a LoggingService interface and would try to resolve that and then pass the instance of the LoggingService into the constructor. Commented Jan 21, 2013 at 19:41
  • What you really need is ICustomerService and ILoggingService interfaces and those are what you would resolve. When you bootstrap the container, you would create the mappings from ICustomerService to CustomerService and ILoggingService to LoggingService Commented Jan 21, 2013 at 19:43
  • can u plzz come with small complete code as a result i can visualize better because i am weak in DI. Commented Jan 21, 2013 at 19:59

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

can u plzz provide another sample code for dependency injection with setter instead of constructor. just copy ur first code and do some change for implementing it with setter. thanks
I added an example of property injection to the example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.