Skip to content

lucaslab-dev/Tyrion

Repository files navigation

⚔️ Tyrion

Build Status GitHub license NuGet NuGet Codacy Badge

Tyrion is an implementation of mediator pattern for dotnet

👨🏽‍💻 Installing

  • Package Manager
Install-Package Tyrion
  • .Net CLI
dotnet add package Tyrion
  • Pakat CLI
paket add Tyrion

🧾 Usage

In your Startup.cs, add Tyrion on your service with some typeof class, for identify the currently assembly project. (Could be any class in your project) like this:

services.AddTyrion(typeof(Category));

When we talk about mediator everything is about requests... Tyrion is abstract for just one interface, where you will handle with multiple concepts types implementing just the IRequest interface for Commands and Queries and INotification interface when you want send multiple notifications events.

You must keep it simple, so when you are creating a CommandHandle you have to combine your Command request. That way, Tyrion can find quickly the required implementation.

Samples of requests bellow:

public class ProductCommand : IRequest { }

public class CategoryQuery : IRequest { }

public class CategoryAddedEvent : INotification { }

Samples of validation requests bellow (this is an optional implementation):

public sealed class CategoryCommandValidator : Validator<CategoryCommand>
{
	public CategoryCommandValidator()
	{
		RuleFor(x => x.Name).NotEmpty();
	}
}

Samples of Handlers bellow:

public sealed class CategoryCommandHandler : IRequestHandler<CategoryCommand, Category>
{
	public async Task<IResult<Category>> Execute(CategoryCommand command)
	{
		return await Result<Category>.SuccessedAsync(new Category());
	}
}

public sealed class CategoryQueryHandler : IRequestHandler<CategoryQuery, Category>
{
	public async Task<IResult<Category>> Execute(CategoryQuery request)
	{
		return await Result<Category>.SuccessedAsync(new Category());
	}
}

public sealed class CategoryNotificationHandler : INotificationHandler<CategoryAddedEvent>
{
	public async Task Publish(CategoryAddedEvent notification)
	{
		await Task.CompletedTask;
	}
}

IRequestHandler an receive one or two generics arguments, the first one is your request (Command, Query, etc) all implementing IRequest and the secound one is you return type if you need.

Case you implement your request validation (using Validator<> class), Tyrion will handle with the validation and return your errors automaticaly.

That way we can assunrency that we will receive what we are requiring.

Tyrion allows you to implement multiple handlers in your application service. Just in case you need keep the same context together.

Something like this:

public sealed class ProductCommandHandler : IRequestHandler<SaveProductCommand, Product>,
					    IRequestHandler<UpdateProductCommand, Product>,
					    IRequestHandler<RemoveProductCommand>,
					    IRequestHandler<InativeProductCommand>
{
	public async Task<IResult<Product>> Execute(SaveProductCommand request)
	{
		return await Result<Product>.SuccessedAsync(new Product());
	}

	public async Task<IResult<Product>> Execute(UpdateProductCommand command)
	{
		return await Result<Product>.SuccessedAsync(new Product());
	}

	public async Task<IResult> Execute(RemoveProductCommand request)
	{
		return await Result.SuccessedAsync(new Product());
	}

	public async Task<IResult> Execute(InativeProductCommand request)
	{
		return await Result.SuccessedAsync();
	}
}

I hope you enjoy this. I will work to include little improvements!

🙋🏽‍♂️ Author

X: @lucasluizss Platform: @lucaslab.dev

📝 Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

⚖️ License

MIT