Eml.Mediator 2.2.0.2

Capture Business-Use-Cases and convert it into modular and highly testable chunk of codes. One step closer to migrating or disecting monolithic apps. A combination of Command, Request-Response, Mediator Pattern and Abstract Class Factory pattern. Provide a common ground for projects with large number of developers. Now supports .NetCore2.2.

Install-Package Eml.Mediator -Version 2.2.0.2
dotnet add package Eml.Mediator --version 2.2.0.2
<PackageReference Include="Eml.Mediator" Version="2.2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Eml.Mediator --version 2.2.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Eml.Mediator

A. Usage - Command

    [Test]
    public async Task Command_ShouldBeCalledOnce()
    {
        var command = new TestCommandAsync();			//<-Data

        await mediator.ExecuteAsync(command);           //<-Execute

        command.EngineInvocationCount.ShouldBe(1);
    }

1. Create a command class.

TestCommandAsync contains the needed data for the CommandEngine.

    public class TestCommandAsync : ICommandAsync
    {
        public int EngineInvocationCount { get; set; }

        public TestCommandAsync()
        {
            EngineInvocationCount = 0;
        }
    }

2. Create a command engine.

TestCommandEngine will be auto-discovered and executed by await command.ExecuteAsync();.

    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class TestCommandEngine : ICommandAsyncEngine<TestCommandAsync>
    {
        public async Task ExecuteAsync(TestCommandAsync commandAsync)
        {
            await Task.Run(() => commandAsync.EngineInvocationCount++);
        }
    }

B. Usage - Request-Response

    [Test]
    public async Task Response_ShouldReturnCorrectValue()
    {
        var request = new TestRequestAsync(Guid.NewGuid());     //<-Data

        var response = await mediator.GetAsync(request);        //<-Execute

        response.ResponseToRequestId.ShouldBe(request.Id);      //<-Return Value
    }

1. Create a Request class.

TestRequestAsync contains the needed data for the RequestEngine.

    public class TestRequestAsync : IRequestAsync<TestRequestAsync, TestResponse>
    {
        public Guid Id { get; }                                 //<-Data

        public TestRequestAsync(Guid id)
        {
            Id = id;
        }
    }

2. Create a Response class.

TestResponse is the return value of RequestEngine.

    public class TestResponse : IResponse
    {
        public Guid ResponseToRequestId { get; }                //<-Return Value

        public TestResponse(Guid responseToRequestId)
        {
            ResponseToRequestId = responseToRequestId;
        }
    }

3. Create a Request engine.

TestRequestEngine will be auto-discovered and executed by await mediator.GetAsync(request);.

  • For NetFramework
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class TestRequestEngine : IRequestAsyncEngine<TestRequestAsync, TestResponse>
    {
        public async Task<TestResponse> GetAsync(TestRequestAsync request)  //<-Execute
        {
            return await Task.Run(() => new TestResponse(request.Id));
        }
    }
  • For NetCore2.2 (no need to place CreationPolicy.NonShared attribute)
    public class TestRequestEngine : IRequestAsyncEngine<TestRequestAsync, TestResponse>
    {
        public async Task<TestResponse> GetAsync(TestRequestAsync request)  //<-Execute
        {
            return await Task.Run(() => new TestResponse(request.Id));
        }
    }

That's it.

Check out EmlExtensions.vsix to automate the above process in one go.

Eml.Mediator

A. Usage - Command

    [Test]
    public async Task Command_ShouldBeCalledOnce()
    {
        var command = new TestCommandAsync();			//<-Data

        await mediator.ExecuteAsync(command);           //<-Execute

        command.EngineInvocationCount.ShouldBe(1);
    }

1. Create a command class.

TestCommandAsync contains the needed data for the CommandEngine.

    public class TestCommandAsync : ICommandAsync
    {
        public int EngineInvocationCount { get; set; }

        public TestCommandAsync()
        {
            EngineInvocationCount = 0;
        }
    }

2. Create a command engine.

TestCommandEngine will be auto-discovered and executed by await command.ExecuteAsync();.

    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class TestCommandEngine : ICommandAsyncEngine<TestCommandAsync>
    {
        public async Task ExecuteAsync(TestCommandAsync commandAsync)
        {
            await Task.Run(() => commandAsync.EngineInvocationCount++);
        }
    }

B. Usage - Request-Response

    [Test]
    public async Task Response_ShouldReturnCorrectValue()
    {
        var request = new TestRequestAsync(Guid.NewGuid());     //<-Data

        var response = await mediator.GetAsync(request);        //<-Execute

        response.ResponseToRequestId.ShouldBe(request.Id);      //<-Return Value
    }

1. Create a Request class.

TestRequestAsync contains the needed data for the RequestEngine.

    public class TestRequestAsync : IRequestAsync<TestRequestAsync, TestResponse>
    {
        public Guid Id { get; }                                 //<-Data

        public TestRequestAsync(Guid id)
        {
            Id = id;
        }
    }

2. Create a Response class.

TestResponse is the return value of RequestEngine.

    public class TestResponse : IResponse
    {
        public Guid ResponseToRequestId { get; }                //<-Return Value

        public TestResponse(Guid responseToRequestId)
        {
            ResponseToRequestId = responseToRequestId;
        }
    }

3. Create a Request engine.

TestRequestEngine will be auto-discovered and executed by await mediator.GetAsync(request);.

  • For NetFramework
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class TestRequestEngine : IRequestAsyncEngine<TestRequestAsync, TestResponse>
    {
        public async Task<TestResponse> GetAsync(TestRequestAsync request)  //<-Execute
        {
            return await Task.Run(() => new TestResponse(request.Id));
        }
    }
  • For NetCore2.2 (no need to place CreationPolicy.NonShared attribute)
    public class TestRequestEngine : IRequestAsyncEngine<TestRequestAsync, TestResponse>
    {
        public async Task<TestResponse> GetAsync(TestRequestAsync request)  //<-Execute
        {
            return await Task.Run(() => new TestResponse(request.Id));
        }
    }

That's it.

Check out EmlExtensions.vsix to automate the above process in one go.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.2.0.2 414 4/1/2019