What is Dependency Injection?

Dependency Injection is a design pattern in which individual objects do not attempt to discover or create their dependencies themselves, but instead rely on those dependencies being injected by some other object. An example of this would be if a constructor, instead of instantiating another object it requires, provides a parameter for that dependency to be passed in. Another example would be if a class, instead of reading a configuration file, provides properties for those same values to be passed in. In a dependency injection system, no object looks outside of itself for its dependencies, instead, it provides the means for the necessary dependencies to be passed to it.

The benefits of dependency injection lie in two main areas: classes become much simpler, and coupling is significantly reduced. Classes become simpler because they have to do much less. Instead of having to read configuration files and discover their dependencies, they merely have to provide constructors, properties, and methods to receive the necessary values. This significantly reduces the amount of parsing, lookup, and data retrieval code required in each object. This means less design, less coding, less testing, and less debugging. As a consequence, development time is significantly improved. Adapting an existing system to use a dependency injection system usually allows about a 1/3rd reduction in the overall amount of code.

The second major advantage of dependency injection is that coupling is significantly reduced. Since classes are no longer responsible for discovering their dependencies, they have to know significantly less about how the rest of the system is set up. They aren't tied to particular files existing on the file system, database existing out on the network, or network resources being available. Objects aren't responsible for creating and configuring instances of their dependencies, and therefore don't need to know the specific types of those objects. This allows specific implementations of those dependencies to be changed to suit the context in which the object is being used with much greater flexibility, and on an instance-by-instance basis. The net result is significantly more modular systems which exhibit much greater code reuse.

What is a Dependency Injection engine?

If one attempts to implement dependency injection fully, no object will ever be connected to any other object, since each expects some outside object to inject its dependencies. A dependency injection engine is that outside object. Such an engine can take many different forms (e.g., a Main method which creates and associates individual instances, or a sophisticated software library which provides a scripting language for describing an object graphs), but it always serves the function of being responsible for injecting all the dependencies into all the other objects in the system.

What is Anise?

Anise is a sophisticated dependency injection engine for C# .NET. It defines a scripting language for specifying object graphs, and an interpreter for reading the scripts and building the objects they describe. Anise provides both an executable to run scripts from the command line and a class library for embedding the interpreter in another program.

Where can I use Anise?

Anise can be used in any environment in which C# code can be executed. This includes: command-line programs, GUI programs, Windows services, and web services. In every case, you simply need to include the Anise library with your dependencies, and create an instance of an AniseEngine to get started.

Do I have to use Anise classes in my code?

No. You simply need to write your classes in the general pattern of dependency injected code (i.e., don't read configuration files; don't instantiate dependencies directly; do provide constructors, properties, and methods to allow those dependencies to be injected). Once your code is complete, you need only to write an Anise script to configure the object graph, and then use the AniseEngine class to create the instances you need.

Last edited Jun 27, 2010 at 11:49 PM by aminer, version 1

Comments

No comments yet.