The Siege Project
- Siege.ServiceLocation Part 1 – Introduction and General Use
- Siege.ServiceLocation Part 2 – Contextual Registration and Resolution
- Siege.ServiceLocation Part 3 – Extending the container with custom use cases
- Siege.ServiceLocation Part 4 – Integrating Siege.ServiceLocation with ASP.NET MVC
- Siege.ServiceLocation Part 5 – A guide to getting started with Siege.ServiceLocation
In my last post, I introduced the Siege project. While that post dealt with the goals, values and philosophies of the project, this post serves as a hands-on look at the first component to be released under the Siege umbrella. This component serves to a lesser extent as a general abstraction for service locators and dependency injection containers, providing a simple syntax for component registration. To a greater extent, this library introduces a broader type of contextual registration and allows consumers to extend the registration syntax and resolution mechanisms to account for their own custom use cases.
What is it not?
I think it’s important to start off with this, because I want to set expectations clearly. This is not another IoC framework. When building this my goal was not to build yet-another-IoC from the ground up. There are plenty of projects out there that do this very well already: Castle Windsor, StructureMap, and Ninject, just to name a few. These are all good frameworks that perform their function well, and the community honestly is not in need of another one of these. My intent with this project is to be able to leverage these existing frameworks and gain the benefit of their functionality — to stand on the shoulders of giants.
So what is it?
The Siege.ServiceLocation component is an abstraction mechanism coupled with a way to extend the existing frameworks with additional functionality. First, this component abstracts a developer away from the need to learn another subset of frameworks, with their own styles and approaches to registration and resolution of types. Secondly, Siege.ServiceLocation introduces contextual resolution to selectively resolve types based on run-time state of an application or user session. Finally, it allows developers to extend its behavior with their own custom registration and resolution behaviors.
We use this at our company to write loosely coupled frameworks that can be integrated with our various projects. Where I work, we use more than one IoC, so rather than having to rewrite a lot of registration and resolution code, we simply use a different adapter when the container is constructed.
Let’s look at some code.
At the heart of Siege.ServiceLocation is the SiegeContainer class. This serves as a wrapper class for the various adapters for the different underlying IoC providers. Setting it up to use one of the various providers is easy. This is the constructor for the SiegeContainer:
public SiegeContainer(IServiceLocatorAdapter serviceLocator)
The IServiceLocatorAdapter interface provides the mechanism by which we integrate with existing IoC frameworks. For example, to use Ninject as a provider for SiegeContainer, simply instantiate like this:
var locator = new SiegeContainer(new NinjectAdapter());
If you need to use a preconfigured kernel, this is also easy to do:
var kernel = new StandardKernel();
var locator = new SiegeContainer(new NinjectAdapter(kernel));
From here there are two methods that you can use to get started:
IServiceLocator Register<TService>(IUseCase<TService> useCase);
Included in this assembly is a syntax to help build instances of IUseCase for the consumer. This syntax aligns with my goal of simplicity and covering the 80% of use cases that I mentioned in my previous post. I personally do not like verbose registration syntaxes, xml-based registration, or overly-fluent registrations. While I understand the goals and reasons for them, and I can see their use, I do not personally feel like the value added overcomes the inherent cost of learning and understanding the API. So, for my syntax, I followed the simple BDD formula. Given, When and Then.
Siege.ServiceLocation includes a helper class that is used for the purpose of building instances of IUseCase for the registration of types. Out of the box, this syntax supports both basic registration and contextual registration. While I will devote another post to the contextual resolution of items, I will cover briefly the method by which you contextually register items and what the purpose of contextual resolution is.
Basic (Default) Registration
Basic registration links types to implentations. This can happen between interfaces and classes, classes and subclasses, and a class and itself. Currently Siege has no mechanism to auto-register types, and a type must be either registered through Siege or registered through the underlying provider in order to be resolved. It looks like this:
and registration itself looks like this:
Under the covers, the container coordinates with the adapter to fulfill registration of the type. It also tracks the type internally for its own orchestration of the resolution of deep object graphs.
Contextual registration links types to implementations which are later resolved only if the specified criteria are met. The criteria for these conditional resolutions is completely arbitrary and defined by the consumer. As a developer, you can specify certain types to be returned under as simple or complex criteria as you want. Simply add context as you go to the locator and it will use that context to determine which rules have been met during resolution. SiegeContainer analyzes resolution of every dependency in an object graph no matter how deep and resolves types depending on whether or a condition has been successfully met or a default case needs to be triggered.
The syntax for the helper, Given<T>, looks like this:
.When<ArbitraryContextItem>(contextItem == someValue);
To give a purely contrived, but less abstract example, imagine you have an ordering system that integrates with a third party to fulfill requests online in real time. Sometimes this vendor is down, and you want to save the orders for later processing when this occurs.
.When<VendorStatus>(status == VendorStatus.Offline);
In this case, the application would contextually know to process orders in real-time or to defer them to a later time based on the status of a 3rd party integration component. This is all handled by the container, so you don’t have to build additional logic into your system for determining how to handle different states of the system; This scenario assumes that there is some component performing a health check and if it fails, it notifies the locator that the state of the application is changed so it can respond accordingly. There are many different use cases for such contextually-based resolution; because the conditions are completely arbitrary, it is up to the imagination of the developer on how to use (or abuse) this feature. I will go over, in greater depth, contextual registration and resolution in the following blog post.
Extending the container
Siege.ServiceLocation is built with extensibility in mind. To that end, it has the capacity to incorporate and use custom implementations of IUseCase. While this will be covered in a seperate post, it allows a consumer to extend the container to understand how to register custom scenarios and how to resolve them. This is meant to allow myself and other members of the community to easily provide extensions to this framework without having to constantly update the source code to deliver new functionality to users.
In upcoming posts, I will delve deeper into how contextual registration/resolution works, how to extend the container to incorporate custom use cases and how to integrate the framework into ASP.NET MVC. My goal is to explain how these aspects of the system work in a clear and concise manner that adheres to the main pillars of the Siege design philosophy: Keep it easy to understand and consume. Please check this framework out and leave any feedback you have in the comments section.
Post Footer automatically generated by Add Post Footer Plugin for wordpress.