turtleM (turtle mediator) is a micro library that bootstrap's the mediator pattern in your .Net projects.
If you are unfamiliary with the mediator pattern and why you might want to see this blog post. It provides a good example on how you can de-clutter your controller, seperate your concerns and create cleaner, more maintanable code.
Here are three major advantages to using mediators:
- Streamline your dependencies (and thereby decouple them)
- Promote single responsibility
- Enable application composition
turtleM was created to reduce boilerplate, maximize flexibility and support the following out of the box:
- Async\Await
- Any object can be used for request and response objects (no inheritance or interface tagging required)
- Handler chaining (more than one response type can be returned for any request)
- Named mediators (useful if you have multiple handlers with the same signature)
The core library is available on nuget
install-package turtleM
Currently Supported IoC Packages include:
Setup (Unity)
using Microsoft.Practices.Unity;
using turtleM;
using turtleM.Unity;
//IoC
var container = new UnityContainer();
//Register componenets required by turtleM
container.RegisterType<IDependencyInjector, UnityDependencyInjector>();
container.RegisterType<IMediator, Mediator>();
//Register one or more handlers
container.RegisterType<IRequestHandler<Ping, Pong>, PingPongHandler>();
Runtime
using turtleM;
public class Ping { }
public class Pong
{
public string Message { get; set; }
}
public class PingPongHandler : IRequestHandler<Ping, Pong>
{
public Pong Handle(Ping request)
{
return new Pong
{
Message = "Pong Object"
};
}
}
//xUnit test
public class PingPongTest
{
[Fact]
public void ExecuteTest()
{
//Resolve mediator
var mediator = container.Resolve<IMediator>();
var result = mediator.Get<Ping, Pong>(new Ping());
Assert.Equal("Pong Object", result.Message);
}
}
For more examples or to check out a different IoC framework see the unit test projects
0.1.0
MIT