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:
- Install
IoCSharp
by NuGet.
PM> Install-Package IoCSharp
- 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>
- 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:
<!---->
<iocSharp description="Inversion of control">
<!---->
<iocContainer interface="IRepository">
<!---->
<!---->
<context name="ContextOne" inUse="True"
namespace="IoCSharp.Console.Repository.ContextOne.Repository"
assembly="IoCSharp.Console"/>
- Using the IoC:
IRepository repository = IoCFactory.Create<IRepository>();
repository.FetchItems();
- Using the dependencies injection:
-
IRepository repository = IoCFactory.Create<IRepository>(param1, param2, param3);
repository.FetchItems();
- 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 iocContainer
s 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.