Giter Site home page Giter Site logo

lucaslab-dev / tyrion Goto Github PK

View Code? Open in Web Editor NEW
10.0 2.0 2.0 36 KB

⚔️ Tyrion is a mediator pattern implementation package for dotnet

Home Page: https://www.nuget.org/packages/Tyrion/

C# 100.00%
cqrs csharp dependency-injection mediator-pattern test-driven-development nuget package

tyrion's Introduction

⚔️ 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

tyrion's People

Contributors

lucasluizss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tyrion's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.