├── src ├── Comandante.Tests │ ├── Queries │ │ ├── User.cs │ │ ├── MissingQuery.cs │ │ ├── GetUserQuery.cs │ │ ├── GetUserQueryHandler.cs │ │ ├── ExceptionQueryHandler.cs │ │ └── QueryTests.cs │ ├── Commands │ │ ├── MissingCommand.cs │ │ ├── CreateUserCommand.cs │ │ ├── CreateUserCommandHandler.cs │ │ ├── ExceptionCommandHandler.cs │ │ ├── CommandDecorator.cs │ │ └── CommandTests.cs │ └── Comandante.Tests.csproj ├── Comandante.Benchmarks │ ├── PingQuery.cs │ ├── Program.cs │ ├── PingQueryHandler.cs │ ├── Comandante.Benchmarks.csproj │ └── ComandanteBenchmark.cs ├── Comandante │ ├── IQuery.cs │ ├── ICommand.cs │ ├── IServiceFactory.cs │ ├── ComandanteException.cs │ ├── IQueryHandler.cs │ ├── ICommandHandler.cs │ ├── Comandante.csproj │ ├── IQueryDispatcher.cs │ ├── ICommandDispatcher.cs │ ├── QueryDispatcher.cs │ └── CommandDispatcher.cs └── Comandante.Extensions.Microsoft.DependencyInjection │ ├── ServiceFactory.cs │ ├── ServiceCollectionExtensions.cs │ └── Comandante.Extensions.Microsoft.DependencyInjection.csproj ├── .github └── workflows │ ├── dotnet.yml │ └── publish.yml ├── Comandante.sln ├── README.md ├── .gitignore └── LICENSE /src/Comandante.Tests/Queries/User.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante.Tests.Queries 2 | { 3 | public record User(long UserId, string UserName); 4 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Queries/MissingQuery.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante.Tests.Queries 2 | { 3 | public class MissingQuery : IQuery 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Commands/MissingCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante.Tests.Commands 2 | { 3 | public class MissingCommand : ICommand 4 | { 5 | 6 | } 7 | } -------------------------------------------------------------------------------- /src/Comandante.Benchmarks/PingQuery.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante.Benchmarks 2 | { 3 | public class PingQuery : IQuery 4 | { 5 | 6 | } 7 | 8 | public record Pong 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /src/Comandante.Benchmarks/Program.cs: -------------------------------------------------------------------------------- 1 | using BenchmarkDotNet.Running; 2 | 3 | namespace Comandante.Benchmarks 4 | { 5 | class Program 6 | { 7 | static void Main(string[] args) => 8 | BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args); 9 | } 10 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Queries/GetUserQuery.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante.Tests.Queries 2 | { 3 | public class GetUserQuery : IQuery 4 | { 5 | public GetUserQuery(long userId) 6 | { 7 | UserId = userId; 8 | } 9 | 10 | public long UserId { get; } 11 | } 12 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Commands/CreateUserCommand.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante.Tests.Commands 2 | { 3 | public class CreateUserCommand : ICommand 4 | { 5 | public CreateUserCommand(string userName) 6 | { 7 | UserName = userName; 8 | } 9 | 10 | public string UserName { get; } 11 | } 12 | } -------------------------------------------------------------------------------- /src/Comandante/IQuery.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante 2 | { 3 | /// 4 | /// Represents a query 5 | /// 6 | /// A query payload type 7 | /// A query result 8 | public interface IQuery where TQuery : IQuery 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /src/Comandante.Benchmarks/PingQueryHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante.Benchmarks 5 | { 6 | public class PingQueryHandler : IQueryHandler 7 | { 8 | public Task Handle(PingQuery query, CancellationToken cancellationToken) 9 | { 10 | return Task.FromResult(new Pong()); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Comandante/ICommand.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante 2 | { 3 | /// 4 | /// Represents a command 5 | /// 6 | /// A command payload type 7 | /// A command result 8 | public interface ICommand where TCommand : ICommand 9 | { 10 | } 11 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Queries/GetUserQueryHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante.Tests.Queries 5 | { 6 | public class GetUserQueryHandler : IQueryHandler 7 | { 8 | public Task Handle(GetUserQuery query, CancellationToken cancellationToken) 9 | { 10 | return Task.FromResult(new User(42, "The one")); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Commands/CreateUserCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante.Tests.Commands 5 | { 6 | public class CreateUserCommandHandler : ICommandHandler 7 | { 8 | public Task Handle(CreateUserCommand command, CancellationToken cancellationToken) 9 | { 10 | return Task.FromResult(42L); 11 | } 12 | } 13 | } -------------------------------------------------------------------------------- /src/Comandante/IServiceFactory.cs: -------------------------------------------------------------------------------- 1 | namespace Comandante 2 | { 3 | /// 4 | /// Creates an object that requested by dispatchers 5 | /// 6 | public interface IServiceFactory 7 | { 8 | /// 9 | /// Creates a new service by requested type 10 | /// 11 | /// A service type 12 | /// 13 | /// Returns the requested service 14 | /// 15 | T GetService(); 16 | } 17 | } -------------------------------------------------------------------------------- /src/Comandante/ComandanteException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Comandante 4 | { 5 | /// 6 | /// Inner comandante exception 7 | /// 8 | public class ComandanteException : Exception 9 | { 10 | /// 11 | /// Creates a new exception 12 | /// 13 | /// An exception message 14 | public ComandanteException(string message) 15 | : base(message) 16 | { 17 | 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /.github/workflows/dotnet.yml: -------------------------------------------------------------------------------- 1 | name: .NET 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Setup .NET 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: 6.0.x 20 | - name: Restore dependencies 21 | run: dotnet restore 22 | - name: Build 23 | run: dotnet build --no-restore 24 | - name: Test 25 | run: dotnet test --no-build --verbosity normal 26 | -------------------------------------------------------------------------------- /src/Comandante.Extensions.Microsoft.DependencyInjection/ServiceFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace Comandante 5 | { 6 | public class ServiceFactory : IServiceFactory 7 | { 8 | private readonly IServiceProvider _serviceProvider; 9 | 10 | public ServiceFactory(IServiceProvider serviceProvider) 11 | { 12 | _serviceProvider = serviceProvider; 13 | } 14 | 15 | public T GetService() 16 | { 17 | return _serviceProvider.GetService(); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Queries/ExceptionQueryHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Comandante.Tests.Queries 6 | { 7 | public class ExceptionQueryHandler : IQueryHandler 8 | { 9 | public Task Handle(ExceptionQuery query, CancellationToken cancellationToken) 10 | { 11 | throw new QueryException(); 12 | } 13 | } 14 | 15 | public class ExceptionQuery : IQuery 16 | { 17 | 18 | } 19 | 20 | public class QueryException : Exception 21 | { 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Commands/ExceptionCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Comandante.Tests.Commands 6 | { 7 | public class ExceptionCommandHandler : ICommandHandler 8 | { 9 | public Task Handle(ExceptionCommand command, CancellationToken cancellationToken) 10 | { 11 | throw new CommandException(); 12 | } 13 | } 14 | 15 | public class ExceptionCommand : ICommand 16 | { 17 | 18 | } 19 | 20 | public class CommandException : Exception 21 | { 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /src/Comandante.Benchmarks/Comandante.Benchmarks.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | false 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /src/Comandante.Tests/Commands/CommandDecorator.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante.Tests.Commands 5 | { 6 | public class CommandDecorator : ICommandHandler 7 | where TCommand : ICommand 8 | { 9 | private readonly ICommandHandler _decoratee; 10 | 11 | public CommandDecorator(ICommandHandler decoratee) 12 | { 13 | _decoratee = decoratee; 14 | } 15 | 16 | public Task Handle(TCommand command, CancellationToken cancellationToken) 17 | { 18 | return _decoratee.Handle(command, cancellationToken); 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish 2 | 3 | on: 4 | push: 5 | tags: [ '*' ] 6 | 7 | jobs: 8 | publish: 9 | runs-on: ubuntu-latest 10 | env: 11 | DOTNET_NOLOGO: 1 12 | DOTNET_CLI_TELEMETRY_OPTOUT: 1 13 | DOTNET_SKIP_FIRST_TIME_EXPERIENCE: 1 14 | steps: 15 | - uses: actions/checkout@v2 16 | - name: Setup .NET Core 17 | uses: actions/setup-dotnet@v1 18 | with: 19 | dotnet-version: 6.0.x 20 | - name: Get the version 21 | id: get_version 22 | run: echo ::set-output name=VERSION::${GITHUB_REF/refs\/tags\//} 23 | - name: Create the package 24 | run: dotnet pack -p:PackageVersion=${{ steps.get_version.outputs.VERSION }} -c Release -o Packages 25 | - name: Publish the package 26 | run: dotnet nuget push ./Packages/*.nupkg -s https://api.nuget.org/v3/index.json -k $NUGET_AUTH_TOKEN 27 | env: 28 | NUGET_AUTH_TOKEN: ${{secrets.NUGET_TOKEN}} 29 | -------------------------------------------------------------------------------- /src/Comandante/IQueryHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante 5 | { 6 | /// 7 | /// A query handler to process queries 8 | /// 9 | /// A query type 10 | /// A query result 11 | public interface IQueryHandler 12 | where TQuery: IQuery 13 | { 14 | /// 15 | /// Asynchronously handles a single query 16 | /// 17 | /// A query 18 | /// A cancellation token 19 | /// 20 | /// Returns a task that represents a query operation. The task result contains the query result 21 | /// 22 | Task Handle(TQuery query, CancellationToken cancellationToken); 23 | } 24 | } -------------------------------------------------------------------------------- /src/Comandante/ICommandHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante 5 | { 6 | /// 7 | /// A command handler to process commands 8 | /// 9 | /// A command type 10 | /// A command type result 11 | public interface ICommandHandler 12 | where TCommand : ICommand 13 | { 14 | /// 15 | /// Asynchronously handles a single command 16 | /// 17 | /// A command 18 | /// A cancellation token 19 | /// 20 | /// Returns a task that represents a command operation. The task result contains the command result 21 | /// 22 | Task Handle(TCommand command, CancellationToken cancellationToken); 23 | } 24 | } -------------------------------------------------------------------------------- /src/Comandante.Benchmarks/ComandanteBenchmark.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | using BenchmarkDotNet.Attributes; 4 | using BenchmarkDotNet.Jobs; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Comandante.Benchmarks 8 | { 9 | [MemoryDiagnoser] 10 | [SimpleJob(RuntimeMoniker.Net50)] 11 | public class ComandanteBenchmark 12 | { 13 | private IQueryDispatcher _queryDispatcher; 14 | 15 | [GlobalSetup] 16 | public void Setup() 17 | { 18 | var conf = new ServiceCollection(); 19 | conf.AddComandate(typeof(PingQuery).Assembly); 20 | var sp = conf.BuildServiceProvider(); 21 | 22 | _queryDispatcher = sp.GetRequiredService(); 23 | } 24 | 25 | [Benchmark] 26 | public async Task HandleQuery() 27 | { 28 | var result = await _queryDispatcher.Dispatch(new PingQuery(), CancellationToken.None); 29 | return result; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /src/Comandante/Comandante.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0;net6.0;netstandard2.0 5 | Sergey Vlasov 6 | A small and simple library that was created to make development of CQRS applications easier. 7 | Commandante 8 | mediator;cqrs;commands;queries 9 | https://github.com/vlasovsv/Comandante 10 | Apache-2.0 11 | 8 12 | true 13 | true 14 | snupkg 15 | mediator;cqrs;commands;queries 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/Comandante/IQueryDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante 5 | { 6 | /// 7 | /// Send a single query to be handled by a single query handler. 8 | /// 9 | public interface IQueryDispatcher 10 | { 11 | /// 12 | /// Asynchronously dispatches a query to a single query handler 13 | /// 14 | /// A query 15 | /// A cancellation token 16 | /// A query payload type 17 | /// A query result type 18 | /// 19 | /// Returns a task that represents a query operation. The task result contains a query handler response. 20 | /// 21 | Task Dispatch( 22 | IQuery query, 23 | CancellationToken cancellationToken 24 | ) where TQuery : IQuery; 25 | } 26 | } -------------------------------------------------------------------------------- /src/Comandante/ICommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Comandante 5 | { 6 | /// 7 | /// Send a single command to be handled by a single command handler. 8 | /// 9 | public interface ICommandDispatcher 10 | { 11 | /// 12 | /// Asynchronously dispatches a command to a single command handler 13 | /// 14 | /// A command 15 | /// A cancellation token 16 | /// A command payload type 17 | /// A command result 18 | /// 19 | /// Returns a task that represents a command operation. The task result contains a command handler response. 20 | /// 21 | Task Dispatch( 22 | ICommand command, 23 | CancellationToken cancellationToken 24 | ) where TCommand : ICommand; 25 | } 26 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Comandante.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | all 15 | runtime; build; native; contentfiles; analyzers; buildtransitive 16 | 17 | 18 | 19 | 20 | 21 | 22 | all 23 | runtime; build; native; contentfiles; analyzers; buildtransitive 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /src/Comandante.Extensions.Microsoft.DependencyInjection/ServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Microsoft.Extensions.DependencyInjection.Extensions; 5 | 6 | namespace Comandante 7 | { 8 | public static class ServiceCollectionExtensions 9 | { 10 | public static IServiceCollection AddComandate(this IServiceCollection services, params Assembly[] assemblies) 11 | { 12 | services.TryAddTransient(); 13 | services.TryAddTransient(); 14 | services.TryAddTransient(); 15 | 16 | var commandHandlerType = typeof(ICommandHandler<,>); 17 | var queryHandlerType = typeof(IQueryHandler<,>); 18 | 19 | services.Scan 20 | ( 21 | x => x.FromAssemblies(assemblies) 22 | .AddClasses(c => c.AssignableTo(typeof(ICommandHandler<,>)) 23 | .Where(_ => !_.IsGenericType)) 24 | .AsImplementedInterfaces() 25 | .WithTransientLifetime() 26 | 27 | .AddClasses(c => c.AssignableTo(typeof(IQueryHandler<,>)) 28 | .Where(_ => !_.IsGenericType)) 29 | .AsImplementedInterfaces() 30 | .WithTransientLifetime() 31 | ); 32 | 33 | return services; 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /src/Comandante/QueryDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Comandante 6 | { 7 | /// 8 | /// Default query dispatcher implementation 9 | /// 10 | public class QueryDispatcher : IQueryDispatcher 11 | { 12 | private readonly IServiceFactory _serviceFactory; 13 | 14 | /// 15 | /// Creates a new query dispatcher 16 | /// 17 | /// A service factory 18 | public QueryDispatcher(IServiceFactory serviceFactory) 19 | { 20 | _serviceFactory = serviceFactory; 21 | } 22 | 23 | /// 24 | public Task Dispatch( 25 | IQuery query, 26 | CancellationToken cancellationToken 27 | ) where TQuery : IQuery 28 | { 29 | if (!(query is TQuery concreteQuery)) 30 | { 31 | throw new ArgumentException($"Query must be an instance of {typeof(TQuery)}"); 32 | } 33 | 34 | var handler = _serviceFactory.GetService>(); 35 | 36 | if (handler is null) 37 | throw new ComandanteException( 38 | $"Handler was not found for query of type {typeof(TQuery)}. Register your handlers with the container."); 39 | 40 | return handler.Handle(concreteQuery, cancellationToken); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /src/Comandante/CommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Comandante 6 | { 7 | /// 8 | /// Default command dispatcher implementation 9 | /// 10 | public class CommandDispatcher : ICommandDispatcher 11 | { 12 | private readonly IServiceFactory _serviceFactory; 13 | 14 | /// 15 | /// Creates a new command dispatcher 16 | /// 17 | /// A service factory 18 | public CommandDispatcher(IServiceFactory serviceFactory) 19 | { 20 | _serviceFactory = serviceFactory; 21 | } 22 | 23 | /// 24 | public Task Dispatch( 25 | ICommand command, 26 | CancellationToken cancellationToken 27 | ) where TCommand : ICommand 28 | { 29 | if (!(command is TCommand concreteCommand)) 30 | { 31 | throw new ArgumentException($"Command must be an instance of {typeof(TCommand)}"); 32 | } 33 | 34 | var handler = _serviceFactory.GetService>(); 35 | 36 | if (handler is null) 37 | throw new ComandanteException( 38 | $"Handler was not found for command of type {typeof(TCommand)}. Register your handlers with the container."); 39 | 40 | return handler.Handle(concreteCommand, cancellationToken); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /src/Comandante.Extensions.Microsoft.DependencyInjection/Comandante.Extensions.Microsoft.DependencyInjection.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net5.0;net6.0 5 | Comandante 6 | Sergey Vlasov 7 | Comandante extensions for ASP.NET Core 8 | Commandante.Extensions.Microsoft.DependencyInjection 9 | mediator;cqrs;commands;queries 10 | https://github.com/vlasovsv/Comandante 11 | Apache-2.0 12 | 8 13 | true 14 | true 15 | snupkg 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /src/Comandante.Tests/Queries/QueryTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using FluentAssertions; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Xunit; 6 | 7 | namespace Comandante.Tests.Queries 8 | { 9 | public class QueryTests 10 | { 11 | private IServiceProvider _provider; 12 | 13 | public QueryTests() 14 | { 15 | var conf = new ServiceCollection(); 16 | conf.AddComandate(this.GetType().Assembly); 17 | _provider = conf.BuildServiceProvider(); 18 | } 19 | 20 | [Fact] 21 | public async Task Query_Is_Null_Throws_ArgumentException() 22 | { 23 | // ARRANGE 24 | GetUserQuery query = null; 25 | var sut = _provider.GetRequiredService(); 26 | 27 | // ACT + ASSERT 28 | await Assert.ThrowsAsync(() => sut.Dispatch(query, default)); 29 | } 30 | 31 | [Fact] 32 | public async Task Query_Handle_As_Expected() 33 | { 34 | // ARRANGE 35 | var query = new GetUserQuery(42); 36 | var sut = _provider.GetRequiredService(); 37 | 38 | // ACT 39 | var user = await sut.Dispatch(query, default); 40 | 41 | // ASSERT 42 | user.UserId.Should().Be(42); 43 | user.UserName.Should().Be("The one"); 44 | } 45 | 46 | [Fact] 47 | public async Task Query_Without_Handler_Throws_Exception() 48 | { 49 | // ARRANGE 50 | var query = new MissingQuery(); 51 | var sut = _provider.GetRequiredService(); 52 | 53 | // ACT + ASSERT 54 | await Assert.ThrowsAsync(() => sut.Dispatch(query, default)); 55 | } 56 | 57 | [Fact] 58 | public async Task Query_Throws_Exception_QueryDispatcher_Rethrows_It() 59 | { 60 | // ARRANGE 61 | var query = new ExceptionQuery(); 62 | var sut = _provider.GetRequiredService(); 63 | 64 | // ACT + ASSERT 65 | await Assert.ThrowsAsync(() => sut.Dispatch(query, default)); 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /src/Comandante.Tests/Commands/CommandTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using FluentAssertions; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Xunit; 6 | 7 | namespace Comandante.Tests.Commands 8 | { 9 | public class CommandTests 10 | { 11 | private IServiceProvider _provider; 12 | 13 | public CommandTests() 14 | { 15 | var conf = new ServiceCollection(); 16 | conf.AddComandate(this.GetType().Assembly); 17 | conf.Decorate(typeof(ICommandHandler<,>), typeof(CommandDecorator<,>)); 18 | _provider = conf.BuildServiceProvider(); 19 | } 20 | 21 | [Fact] 22 | public async Task Command_Is_Null_Throws_ArgumentException() 23 | { 24 | // ARRANGE 25 | CreateUserCommand cmd = null; 26 | var sut = _provider.GetRequiredService(); 27 | 28 | // ACT + ASSERT 29 | await Assert.ThrowsAsync(() => sut.Dispatch(cmd, default)); 30 | } 31 | 32 | [Fact] 33 | public async Task Command_Handle_As_Expected() 34 | { 35 | // ARRANGE 36 | var cmd = new CreateUserCommand("The one"); 37 | var sut = _provider.GetRequiredService(); 38 | 39 | // ACT 40 | var userId = await sut.Dispatch(cmd, default); 41 | 42 | // ASSERT 43 | userId.Should().Be(42); 44 | } 45 | 46 | [Fact] 47 | public async Task Command_Without_Handler_Throws_Exception() 48 | { 49 | // ARRANGE 50 | var cmd = new MissingCommand(); 51 | var sut = _provider.GetRequiredService(); 52 | 53 | // ACT + ASSERT 54 | await Assert.ThrowsAsync(() => sut.Dispatch(cmd, default)); 55 | } 56 | 57 | [Fact] 58 | public async Task Command_Throws_Exception_CommandDispatcher_Rethrows_It() 59 | { 60 | // ARRANGE 61 | var query = new ExceptionCommand(); 62 | var sut = _provider.GetRequiredService(); 63 | 64 | // ACT + ASSERT 65 | await Assert.ThrowsAsync(() => sut.Dispatch(query, default)); 66 | } 67 | } 68 | } -------------------------------------------------------------------------------- /Comandante.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Comandante", "src\Comandante\Comandante.csproj", "{29BE9568-3C1E-4AC4-8214-294803863215}" 4 | EndProject 5 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Comandante.Tests", "src\Comandante.Tests\Comandante.Tests.csproj", "{F953EC42-1408-40EE-9502-641CF6F9E911}" 6 | EndProject 7 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Comandante.Extensions.Microsoft.DependencyInjection", "src\Comandante.Extensions.Microsoft.DependencyInjection\Comandante.Extensions.Microsoft.DependencyInjection.csproj", "{50B71E9D-A177-46D9-BBF5-5D8027803A67}" 8 | EndProject 9 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Comandante.Benchmarks", "src\Comandante.Benchmarks\Comandante.Benchmarks.csproj", "{97CAC64C-38C0-4C2C-982C-E438AAD7A087}" 10 | EndProject 11 | Global 12 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 13 | Debug|Any CPU = Debug|Any CPU 14 | Release|Any CPU = Release|Any CPU 15 | EndGlobalSection 16 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 17 | {29BE9568-3C1E-4AC4-8214-294803863215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 18 | {29BE9568-3C1E-4AC4-8214-294803863215}.Debug|Any CPU.Build.0 = Debug|Any CPU 19 | {29BE9568-3C1E-4AC4-8214-294803863215}.Release|Any CPU.ActiveCfg = Release|Any CPU 20 | {29BE9568-3C1E-4AC4-8214-294803863215}.Release|Any CPU.Build.0 = Release|Any CPU 21 | {F953EC42-1408-40EE-9502-641CF6F9E911}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 22 | {F953EC42-1408-40EE-9502-641CF6F9E911}.Debug|Any CPU.Build.0 = Debug|Any CPU 23 | {F953EC42-1408-40EE-9502-641CF6F9E911}.Release|Any CPU.ActiveCfg = Release|Any CPU 24 | {F953EC42-1408-40EE-9502-641CF6F9E911}.Release|Any CPU.Build.0 = Release|Any CPU 25 | {50B71E9D-A177-46D9-BBF5-5D8027803A67}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 26 | {50B71E9D-A177-46D9-BBF5-5D8027803A67}.Debug|Any CPU.Build.0 = Debug|Any CPU 27 | {50B71E9D-A177-46D9-BBF5-5D8027803A67}.Release|Any CPU.ActiveCfg = Release|Any CPU 28 | {50B71E9D-A177-46D9-BBF5-5D8027803A67}.Release|Any CPU.Build.0 = Release|Any CPU 29 | {97CAC64C-38C0-4C2C-982C-E438AAD7A087}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 30 | {97CAC64C-38C0-4C2C-982C-E438AAD7A087}.Debug|Any CPU.Build.0 = Debug|Any CPU 31 | {97CAC64C-38C0-4C2C-982C-E438AAD7A087}.Release|Any CPU.ActiveCfg = Release|Any CPU 32 | {97CAC64C-38C0-4C2C-982C-E438AAD7A087}.Release|Any CPU.Build.0 = Release|Any CPU 33 | EndGlobalSection 34 | EndGlobal 35 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Comandante 2 | ======= 3 | ![CI](https://github.com/vlasovsv/Comandante/workflows/.NET/badge.svg) 4 | [![NuGet](https://img.shields.io/nuget/v/Commandante.svg)](https://www.nuget.org/packages/Commandante/) 5 | 6 | **Comandante** is a small and simple library that was created to make development of CQRS applications easier. 7 | 8 | ## Setup 9 | Install the package via NuGet first: 10 | `Install-Package Commandante` 11 | 12 | Comandante has separate pipeline for commands and queries. That principle let you optimize processing commands and queries independently from one another. 13 | 14 | Comandante rests on dependency injection to create command and query handlers. 15 | So to work correctly you have to add some of supported DI library such as: 16 | * Comandante.Extensions.Microsoft.DependencyInjection 17 | 18 | Otherwise, you have to implement `IServiceFactory` 19 | 20 | ```csharp 21 | public class ServiceFactory : IServiceFactory 22 | { 23 | public object GetService(Type serviceType) 24 | { 25 | // Creates a new service 26 | } 27 | } 28 | ``` 29 | 30 | ### ASP.NET Core (or .NET Core in general) 31 | To register all necessary Comandante handlers and dispatchers you can use Commandante.Extensions.Microsoft.DependencyInjection method 32 | ```csharp 33 | public void ConfigureServices(IServiceCollection services) 34 | { 35 | services.AddMvc(); 36 | 37 | services.AddComandate(Assembly); 38 | } 39 | ``` 40 | 41 | ## Basics 42 | Comandante provides two types of messages: 43 | * `ICommand` for command operations that create something 44 | * `IQuery` for query operations that retrieve some information 45 | 46 | ### Commands 47 | Command message is just a simple POCO class that implements `ICommand` interface 48 | ```csharp 49 | public class CreateUserCommand : ICommand 50 | { 51 | public CreateUserCommand(string userName) 52 | { 53 | UserName = userName; 54 | } 55 | 56 | public string UserName { get; } 57 | } 58 | ``` 59 | 60 | Next, create a handler: 61 | ```csharp 62 | public class CreateUserCommandHandler : ICommandHandler 63 | { 64 | public Task Handle(CreateUserCommand command, CancellationToken cancellationToken) 65 | { 66 | return Task.FromResult(42l); 67 | } 68 | } 69 | ``` 70 | Finally, send a command through the command dispatcher `ICommandDispatcher`: 71 | ```csharp 72 | var cmd = new CreateUserCommand("test"); 73 | var userId = await dispatcher.Dispatch(cmd, default); 74 | Debug.WriteLine(userId); // 42 75 | ``` 76 | 77 | ### Queries 78 | Query message is also a simple POCO class that implements `IQuery` interface 79 | ```csharp 80 | public class GetUserQuery : IQuery 81 | { 82 | public GetUserQuery(long userId) 83 | { 84 | UserId = userId; 85 | } 86 | 87 | public long UserId { get; } 88 | } 89 | ``` 90 | 91 | Next, create a handler: 92 | ```csharp 93 | public record User(long UserId, string UserName); 94 | 95 | public class GetUserQueryHandler : IQueryHandler 96 | { 97 | public Task Handle(GetUserQuery query, CancellationToken cancellationToken) 98 | { 99 | return Task.FromResult(new User(42, "The one")); 100 | } 101 | } 102 | ``` 103 | Finally, send a query through the query dispatcher `IQueryDispatcher`: 104 | ```csharp 105 | var query = new GetUserQuery(42) 106 | var user = await dispatcher.Dispatch(query, default); 107 | Debug.WriteLine(user.ToString()); // User { UserId = 42, UserName = The one } 108 | ``` 109 | 110 | ### Decorators 111 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.vspscc 94 | *.vssscc 95 | .builds 96 | *.pidb 97 | *.svclog 98 | *.scc 99 | 100 | # Chutzpah Test files 101 | _Chutzpah* 102 | 103 | # Visual C++ cache files 104 | ipch/ 105 | *.aps 106 | *.ncb 107 | *.opendb 108 | *.opensdf 109 | *.sdf 110 | *.cachefile 111 | *.VC.db 112 | *.VC.VC.opendb 113 | 114 | # Visual Studio profiler 115 | *.psess 116 | *.vsp 117 | *.vspx 118 | *.sap 119 | 120 | # Visual Studio Trace Files 121 | *.e2e 122 | 123 | # TFS 2012 Local Workspace 124 | $tf/ 125 | 126 | # Guidance Automation Toolkit 127 | *.gpState 128 | 129 | # ReSharper is a .NET coding add-in 130 | _ReSharper*/ 131 | *.[Rr]e[Ss]harper 132 | *.DotSettings.user 133 | 134 | # TeamCity is a build add-in 135 | _TeamCity* 136 | 137 | # DotCover is a Code Coverage Tool 138 | *.dotCover 139 | 140 | # AxoCover is a Code Coverage Tool 141 | .axoCover/* 142 | !.axoCover/settings.json 143 | 144 | # Coverlet is a free, cross platform Code Coverage Tool 145 | coverage*.json 146 | coverage*.xml 147 | coverage*.info 148 | 149 | # Visual Studio code coverage results 150 | *.coverage 151 | *.coveragexml 152 | 153 | # NCrunch 154 | _NCrunch_* 155 | .*crunch*.local.xml 156 | nCrunchTemp_* 157 | 158 | # MightyMoose 159 | *.mm.* 160 | AutoTest.Net/ 161 | 162 | # Web workbench (sass) 163 | .sass-cache/ 164 | 165 | # Installshield output folder 166 | [Ee]xpress/ 167 | 168 | # DocProject is a documentation generator add-in 169 | DocProject/buildhelp/ 170 | DocProject/Help/*.HxT 171 | DocProject/Help/*.HxC 172 | DocProject/Help/*.hhc 173 | DocProject/Help/*.hhk 174 | DocProject/Help/*.hhp 175 | DocProject/Help/Html2 176 | DocProject/Help/html 177 | 178 | # Click-Once directory 179 | publish/ 180 | 181 | # Publish Web Output 182 | *.[Pp]ublish.xml 183 | *.azurePubxml 184 | # Note: Comment the next line if you want to checkin your web deploy settings, 185 | # but database connection strings (with potential passwords) will be unencrypted 186 | *.pubxml 187 | *.publishproj 188 | 189 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 190 | # checkin your Azure Web App publish settings, but sensitive information contained 191 | # in these scripts will be unencrypted 192 | PublishScripts/ 193 | 194 | # NuGet Packages 195 | *.nupkg 196 | # NuGet Symbol Packages 197 | *.snupkg 198 | # The packages folder can be ignored because of Package Restore 199 | **/[Pp]ackages/* 200 | # except build/, which is used as an MSBuild target. 201 | !**/[Pp]ackages/build/ 202 | # Uncomment if necessary however generally it will be regenerated when needed 203 | #!**/[Pp]ackages/repositories.config 204 | # NuGet v3's project.json files produces more ignorable files 205 | *.nuget.props 206 | *.nuget.targets 207 | 208 | # Microsoft Azure Build Output 209 | csx/ 210 | *.build.csdef 211 | 212 | # Microsoft Azure Emulator 213 | ecf/ 214 | rcf/ 215 | 216 | # Windows Store app package directories and files 217 | AppPackages/ 218 | BundleArtifacts/ 219 | Package.StoreAssociation.xml 220 | _pkginfo.txt 221 | *.appx 222 | *.appxbundle 223 | *.appxupload 224 | 225 | # Visual Studio cache files 226 | # files ending in .cache can be ignored 227 | *.[Cc]ache 228 | # but keep track of directories ending in .cache 229 | !?*.[Cc]ache/ 230 | 231 | # Others 232 | ClientBin/ 233 | ~$* 234 | *~ 235 | *.dbmdl 236 | *.dbproj.schemaview 237 | *.jfm 238 | *.pfx 239 | *.publishsettings 240 | orleans.codegen.cs 241 | 242 | # Including strong name files can present a security risk 243 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 244 | #*.snk 245 | 246 | # Since there are multiple workflows, uncomment next line to ignore bower_components 247 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 248 | #bower_components/ 249 | 250 | # RIA/Silverlight projects 251 | Generated_Code/ 252 | 253 | # Backup & report files from converting an old project file 254 | # to a newer Visual Studio version. Backup files are not needed, 255 | # because we have git ;-) 256 | _UpgradeReport_Files/ 257 | Backup*/ 258 | UpgradeLog*.XML 259 | UpgradeLog*.htm 260 | ServiceFabricBackup/ 261 | *.rptproj.bak 262 | 263 | # SQL Server files 264 | *.mdf 265 | *.ldf 266 | *.ndf 267 | 268 | # Business Intelligence projects 269 | *.rdl.data 270 | *.bim.layout 271 | *.bim_*.settings 272 | *.rptproj.rsuser 273 | *- [Bb]ackup.rdl 274 | *- [Bb]ackup ([0-9]).rdl 275 | *- [Bb]ackup ([0-9][0-9]).rdl 276 | 277 | # Microsoft Fakes 278 | FakesAssemblies/ 279 | 280 | # GhostDoc plugin setting file 281 | *.GhostDoc.xml 282 | 283 | # Node.js Tools for Visual Studio 284 | .ntvs_analysis.dat 285 | node_modules/ 286 | 287 | # Visual Studio 6 build log 288 | *.plg 289 | 290 | # Visual Studio 6 workspace options file 291 | *.opt 292 | 293 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 294 | *.vbw 295 | 296 | # Visual Studio LightSwitch build output 297 | **/*.HTMLClient/GeneratedArtifacts 298 | **/*.DesktopClient/GeneratedArtifacts 299 | **/*.DesktopClient/ModelManifest.xml 300 | **/*.Server/GeneratedArtifacts 301 | **/*.Server/ModelManifest.xml 302 | _Pvt_Extensions 303 | 304 | # Paket dependency manager 305 | .paket/paket.exe 306 | paket-files/ 307 | 308 | # FAKE - F# Make 309 | .fake/ 310 | 311 | # CodeRush personal settings 312 | .cr/personal 313 | 314 | # Python Tools for Visual Studio (PTVS) 315 | __pycache__/ 316 | *.pyc 317 | 318 | # Cake - Uncomment if you are using it 319 | # tools/** 320 | # !tools/packages.config 321 | 322 | # Tabs Studio 323 | *.tss 324 | 325 | # Telerik's JustMock configuration file 326 | *.jmconfig 327 | 328 | # BizTalk build output 329 | *.btp.cs 330 | *.btm.cs 331 | *.odx.cs 332 | *.xsd.cs 333 | 334 | # OpenCover UI analysis results 335 | OpenCover/ 336 | 337 | # Azure Stream Analytics local run output 338 | ASALocalRun/ 339 | 340 | # MSBuild Binary and Structured Log 341 | *.binlog 342 | 343 | # NVidia Nsight GPU debugger configuration file 344 | *.nvuser 345 | 346 | # MFractors (Xamarin productivity tool) working folder 347 | .mfractor/ 348 | 349 | # Local History for Visual Studio 350 | .localhistory/ 351 | 352 | # BeatPulse healthcheck temp database 353 | healthchecksdb 354 | 355 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 356 | MigrationBackup/ 357 | 358 | # Ionide (cross platform F# VS Code tools) working folder 359 | .ionide/ 360 | 361 | # Fody - auto-generated XML schema 362 | FodyWeavers.xsd 363 | 364 | # Rider 365 | .idea/ -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------