Dependency Injection in FwkLight

ALL THE EXAMPLES FROM THIS POST ARE EXTRACTED FROM THE FWKLIGHT DEMO. TO FIND OUT HOW TO GET STARTED WITH THE DEMO, READ THIS POST.

For dependency injection, FwkLight uses an excellent framework called StructureMap.

Why Dependency Injection?

Because it’s the most simple and elegant solution for instantiating services in classes which need them. DI is magic, it takes care of dependencies management without making you lose control over them.

If you don’t use this solution you have to instantiate them manually, which means you either:

  • instantiate them whenever you need them, without following any rules or guidelines: this is the worst solution from a design and productivity perspective, because:
    • the dependencies are not class scoped, they can be method scoped which means you can’t reuse them between methods inside the class
    • dependencies are not clearly highlighted and you can lose control over them
    • dependencies management becomes complicated
  • use a set of rules or guidelines on when and how to instantiate services: this can be ok, but you are still doing more dependencies management than you should

 

How it works and how it should be used in FwkLight

In an application there are 2 types of classes: entities (aggregates are part of this type) and services. Everything that is not a business entity is a service of some sort for a particular layer, and you should always instantiate it using dependency injection.

So, you should never instantiate a service manually:

  • if the class that needs the service is another service, you declare it in the constructor of the client class and StructureMap will take care of instantiating and passing it to the class
  • if the class that needs the service is an entity or an aggregate, it should not have a direct dependency on other services; instead, you should create entity extensions, which can depend on other services

As an example, in the DependencyInjectionController there are 2 variables:

var firstEntityIsOkNoDI = new DependencyInjectionTask(new NHUnitOfWorkProvider(new SessionItemDictionary(), new ThreadItemDictionary()),new DependencyInjectionLoadTask(new BaseRepository<DependencyInjection>(new NHUnitOfWorkProvider(new SessionItemDictionary(), new ThreadItemDictionary())),new NHUnitOfWorkProvider(new SessionItemDictionary(), new ThreadItemDictionary())),new ReceiveSomething(new AmountAvailable())).Execute(1);

var firstEntityIsOk = _dependencyInjectionTask.Execute(1);

The first variable (firstEntityIsOkNoDI) is calculated by a task which is instantiated without using Dependency Injection, and you can easily see how much work is required. In most cases you will not see all these dependencies in the constructor, which makes the code look less scary but at the same time more instable and hard to maintain.

The second variable (firstEntityIsOk) is calculated exactly like the first one, but the task is instantiated by letting StructureMap inject al the necessary dependencies for every service in the hierarchy. It’s extremely easy and elegant.

 

That’s the main idea with Dependency Injection, and how it works in FwkLight.

Advertisement

About this entry