Introducing endjin composition framework 2.0 : Part 3 - Using the content factory
As I explained in this post, the framework provides a set of installers for registering components against interfaces in the container.
Another powerful tool in the composition framework is the ContentFactory
. This provides methods for registering and getting content based on a contentType
string. When getting content, if the contentType
is not found in the container, it will try to fallback using substrings of contentType
by progressively removing sections after a .
As an example, we might have some serializers that inherit from ISerializer
.
We can create a new SerializerFactory
that inherits from ContentFactory
.
Then we use a custom installer to register the factory as a singleton, and also to register our default content (i.e. the serializers).
We are now able to use the SerializerFactory.GetContentFor(string contentType)
method to retrieve our serializers by the name we registered them with:
SerializerFactory.GetContentFor("Foo")
will returnFooSerializer
SerializerFactory.GetContentFor("Foo.Bar")
will returnFooBarSerializer
SerializerFactory.GetContentFor("Foo.Bar.Baz")
will fallback to contentType of "Foo.Bar" and returnFooBarSerializer
In the next post I'll show you how to get set up with endjin composition framework in an MVC4 / WebAPI application.