Click here to Skip to main content
15,885,757 members
Articles / Programming Languages / C# 5.0
Tip/Trick

IoCSharp Example Project

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
22 Nov 2014CPOL1 min read 11.1K   53   6  
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.
    XML
    <configSections>
        <section name="iocSharp" 
        type="IoCSharp.Configurations.IoCConfiguration, IoCSharp" />
    </configSections>
  3. Add IoCSharp section.
    XML
    <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:

    XML
    <!-- 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:
    C#
    IRepository repository = IoCFactory.Create<IRepository>();
    
    //and go!
    repository.FetchItems();
  5. Using the dependencies injection:
  6. C#
    IRepository repository = IoCFactory.Create<IRepository>(param1, param2, param3);
    
    //and go!
    repository.FetchItems();
  7. Setting the context at runtime:
    C#
    IoCHelpers.SetContextByName<IRepository>("ContextOne");

    or:

    C#
    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:

XML
<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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --