Skip to content
This repository has been archived by the owner on Feb 14, 2022. It is now read-only.

Features

Jacopo edited this page Dec 5, 2020 · 8 revisions

Features

Table of Contents

Injection

Versioning

You can manage multiple versions of the same service implementation using a double (major.minor version).

The following example shows how to declare the version of a service:

[Service(Version = 1.2)]
class MyService : IMyServiceInterface
{
  // implementation here
}

[Service(Version = 2.1)]
class MyNewService : MyService
{
  // override of some methods
}

This way, if we request to inject IMyServiceInterface and no versioning is specified, MyService will always be served because it's declared before MyNewService.

Now, consider MyNewService to have been implemented in a second moment, and in your application you always want to always use the latest implementation available. In this case, versioning comes handy:

[Service]
class MyApplication
{
  [Served(TargetingMethod = ServedVersionTargetingMethod.LatestMajor)]
  IMyServiceInterface MyService { get; set; }
}

In this example, all implementations of IMyServiceInterface are searched through to find the one with the latest absolute version, so an instance of MyNewService will be injected.

There are 3 version targeting methods:

Major

Always look for the latest implementation; meaning the one with the highest version number

[Served(TargetingMethod = ServedVersionTargetingMethod.LatestMajor)]

Minor

Minor versioning should also define a TargetVersion like in the following example:

[Served(TargetingMethod = ServedVersionTargetingMethod.Minor, TargetVersion = 2.0)]

This will look for the latest minor version implementation of said service; In the following example services with a major version different from 2 will be ignored.

If no 2.x service is found, an ImplementationNotFoundException is thrown

Exact

Searches for service with the same version number

[Served(TargetingMethod = ServedVersionTargetingMethod.Exact, TargetVersion = 6.9)]

If no service with the exact version is found, an ImplementationNotFoundException is thrown

IEnumerable<Service>

It might happen that you want to search for every implementation of a certain service and iterate through all of them; you can do this by declaring an IEnumerable of the service you want injected. You can have them sorted and filtered by versioning.

[Service]
class RequestAllFoo
{
    [Served]
    IEnumerable<IFooService> foos;

    // the services in this enumerable will be sorted in descending order.
    // so for example { foo(2.4), foo(2.2), foo(2.0) }
    [Served(TargetingMethod = ServedVersionTargetingMethod.Minor, TargetVersion = 2.0)]
    IEnumerable<IFooService> latest_foos;


    void DoWork ()
    {
        // the foo fighters
        foreach ( var foo in this.foos )
            foo.Fight();
    }
}

What the code above does, is to request every service implementing IFooService and every class extending/inheriting from those services.

You can always restrict the injected services by restricting the hierarchy, for example by requesting an IEnumerable<SpecificFooService>.

Instances diff

WIP