Skip to content

Contravariant registry to map types to whatever. Lookups honour interface / base classes.

License

MPL-2.0, MPL-2.0 licenses found

Licenses found

MPL-2.0
LICENSE
MPL-2.0
LICENSE.txt
Notifications You must be signed in to change notification settings

heartysoft/contra

Repository files navigation

Contra

Contra is a type registry to map .NET types to anything. Types can be registered against values, and the values can be looked up by type. Type resolution honours contravariance, so values registered against interfaces, base classes, etc. resolve correctly. Even structures like Envelope work as expected.

Read the Getting started tutorial to learn more.

Documentation: http://heartysoft.github.io/contra

Example

Assuming PersonRegistered implements Started, the following code will trigger both handlers.

var registry = new TypeRegistry<Action<object>>()
    .Register<PersonRegistered>(x => Console.WriteLine("Person registered."))
    .Register<Started>(x => Console.WriteLine("Something started."));

var msg = new PersonRegistered(...)
var handlers = registry.GetValuesFor(msg);

foreach(var h in handlers) h(msg);

Assuming PersonRegistered implements Started, the following code will trigger both handlers. Here, Envelope is an interface implemented by MessageEnvelope.

var registry = new TypeRegistry<Action<object>>()
    .Register<Envelope<PersonRegistered>>(x => Console.WriteLine("Person registered."))
    .Register<Envelope<Started>>(x => Console.WriteLine("Something started."));

var msg = new MessageEnvelope<PersonRegistered>(new PersonRegistered(...))
var handlers = registry.GetValuesFor(msg);

foreach(var h in handlers) h(msg);

Inverse loopuks can be used for type mappings to keys:

[TestFixture]
public class InverseLookupTests
{
    readonly TypeRegistry<string> _registry = 
        new TypeRegistry<string>()
            .Register<PersonRegisteredEvent>("person-registered")
            .Register<CriminalRegisteredEvent>("criminal-registered");

    [Test]
    public void should_get_type_from_value()
    {
        Assert.AreEqual(typeof(PersonRegisteredEvent), _registry.GetKeysFor("person-registered").Single().Key);
        Assert.AreEqual(typeof(CriminalRegisteredEvent), _registry.GetKeysFor("criminal-registered").Single().Key);
    }
}

Maintainer(s)

About

Contravariant registry to map types to whatever. Lookups honour interface / base classes.

Resources

License

MPL-2.0, MPL-2.0 licenses found

Licenses found

MPL-2.0
LICENSE
MPL-2.0
LICENSE.txt

Stars

Watchers

Forks

Packages

No packages published