Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

IoCSharp Example Project

0.00/5 (No votes)
22 Nov 2014 2  
IoC in few steps!

Introduction

This library will allow you to implement the technique dell'inversion of control in a few easy steps.

Background

In software engineering, inversion of control (IoC) describes a design in which custom-written portions of a computer program receive the flow of control from a generic, reusable library. A software architecture with this design inverts control as compared to traditional procedural programming: in traditional programming, the custom code that expresses the purpose of the program calls into reusable libraries to take care of generic tasks, but with inversion of control, it is the reusable code that calls into the custom, or task-specific, code.

Inversion of control is used to increase modularity of the program and make it extensible,[1] and has applications in object-oriented programming and other programming paradigms. The term was popularized by Robert C. Martin and Martin Fowler. The term is related to but different from the dependency inversion principle, which concerns itself with decoupling dependencies between high-level and low-level layers through shared abstractions.

Reference: http://en.wikipedia.org/wiki/Inversion_of_control

Using the Code

Steps:

  1. Install IoCSharp by NuGet.
    PM> Install-Package IoCSharp
  2. Declare IoCSharp section into your web.config or app.config, depends on your application context.
    <configSections>
        <section name="iocSharp" 
        type="IoCSharp.Configurations.IoCConfiguration, IoCSharp" />
    </configSections>
  3. Add IoCSharp section.
    <iocSharp description="Inversion of control">
      <iocContainers>
        <iocContainer interface="IRepository">
          <contexts>
            <context name="ContextOne" inUse="True" 
             namespace="IoCSharp.Console.Repository.ContextOne.Repository" 
             assembly="IoCSharp.Console"/>
    
            <context name="ContextTwo" inUse="False" 
             namespace="IoCSharp.Console.Repository.ContextTwo.Repository" 
             assembly="IoCSharp.Console"/>
    
            <context name="ContextThree" inUse="False" 
             namespace="IoCSharp.Console.Repository.ContextThree.Repository" 
             assembly="IoCSharp.Console"/>
          </contexts>
        </iocContainer>   
      </iocContainers>
    </iocSharp> 

    In detail:

    <!-- Configuration section -->
    <iocSharp description="Inversion of control">
    
    <!-- Declares the interface on which you want to base your IoC -->
    <iocContainer interface="IRepository">
    
    <!-- Declares a context that is instantiated at runtime -->
    <!-- name = key
         inUse = Which must be used by default
         namespace = namespace of class
         assembly = assembly that contain the namespace
    -->
    <context name="ContextOne" inUse="True" 
    namespace="IoCSharp.Console.Repository.ContextOne.Repository" 
    assembly="IoCSharp.Console"/>
  4. Using the IoC:
    IRepository repository = IoCFactory.Create<IRepository>();
    
    //and go!
    repository.FetchItems();
  5. Using the dependencies injection:
  6. IRepository repository = IoCFactory.Create<IRepository>(param1, param2, param3);
    
    //and go!
    repository.FetchItems();
  7. Setting the context at runtime:
    IoCHelpers.SetContextByName<IRepository>("ContextOne");

    or:

    IoCHelpers.SetContextByNamespace<IRepository>
    ("IoCSharp.Console.Repository.ContextOne.Repository");

Points of Interest

In addition, if you want, you can declare many iocContainers independent of each other:

<iocSharp description="Inversion of control">
   <iocContainers> 
      <iocContainer interface="Interface1">
         <contexts>
           <context name="" inUse="" 
           namespace="" assembly=""/>
           <context name="" inUse="" 
           namespace="" assembly=""/>
           <context name="" inUse="" 
           namespace="" assembly=""/>
         </contexts>
       </iocContainer>    
      <iocContainer interface="Interface2">
         <contexts>
            <context name="" inUse="" 
            namespace="" assembly=""/>
            <context name="" inUse="" 
            namespace="" assembly=""/>
            <context name="" inUse="" 
            namespace="" assembly=""/>
         </contexts>
      </iocContainer>
  </iocContainers>
</iocSharp>

Download the Example Project

For completeness, it is available to download a sample project. Good IoC!

For questions, you can write to me at broccio.marco@gmail.com.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here