KhatiMediaTr

Published At 2024/Oct/14
No Image Found

The mediator pattern is often used in CQRS architectures to decouple components involved in handling commands and queries.

Using MediatR in a CQRS setup helps by providing a mediator to send requests (commands or queries) and handle them separately. This separation simplifies the code, improves maintainability, and makes it easier to reason about each operation independently.

As for the benefits of unit testing when implementing MediatR, it generally enhances the overall quality of your code. Unit tests allow you to verify the correctness of individual components in isolation, ensuring that each command or query handler behaves as expected. This can lead to more robust and reliable CQRS implementations, helping you catch and fix issues early in the development process.

Introducing My own MediaTr (KhatiMediaTr) Nuget that can be use in CQRS pattern.

Package Manager

  1. NuGet\Install-Package KhatiMediaTr -Version 1.0.0

Add Middleware

  1. builder.Services.AddMediaTr();

Implement Your Service, For Example:

  1. namespace MyProjectTest.Services
  2. {
  3. public class MyService : IMyService
  4. {
  5. public IOtherDependency _otherDependency;
  6. public MyService(IOtherDependency otherDependency)
  7. {
  8. _otherDependency = otherDependency;
  9. }
  10. public string Print()
  11. {
  12. return "Hello World";
  13. }
  14. public (string message, string description) PrintTupple()
  15. {
  16. return ("Hello World","I am Tupple");
  17. }
  18. public List<MyModel> GetModel()
  19. {
  20. MyModel model = new MyModel() { MyObject = "Hello" };
  21. List<MyModel> list = new List<MyModel>();
  22. list.Add(model);
  23. return list;
  24. }
  25. //You can Add parameters as per as your requirements
  26. public AddParms Request(string value, string value1 , List<MyModel> myModel, MyModel model)
  27. {
  28. AddParms parms = new AddParms()
  29. {
  30. value = value,
  31. value1 = value1,
  32. myModel = myModel,
  33. model = model
  34. };
  35. return parms;
  36. }
  37. public void Wait()
  38. {
  39. _otherDependency.Wait();
  40. }
  41. }
  42. }
  43. public class MyModel
  44. {
  45. public string MyObject { set; get; }
  46. }
  47. public class AddParms
  48. {
  49. public string value { set; get; }
  50. public string value1 { set; get; }
  51. public List<MyModel> myModel { set; get; }
  52. public MyModel model { set; get; }
  53. }

CQRS -> Query

Print Query

  1. using KhatiMediaTr;
  2. using MyProjectTest.Services;
  3. namespace MyProjectTest.CQRS.Query
  4. {
  5. public class PrintQuery : IEventHandler
  6. {
  7. private readonly IMyService _myService;
  8. public PrintQuery(IMyService myService)
  9. {
  10. _myService = myService;
  11. }
  12. public string Handler()
  13. {
  14. return _myService.Print();
  15. }
  16. }
  17. }

Print Tuple

  1. using KhatiMediaTr;
  2. using MyProjectTest.Services;
  3. namespace MyProjectTest.CQRS.Query
  4. {
  5. public class PrintTuple : IEventHandler
  6. {
  7. private readonly IMyService _myService;
  8. public PrintTuple(IMyService myService)
  9. {
  10. _myService = myService;
  11. }
  12. public (string message, string description) Handler()
  13. {
  14. return _myService.PrintTupple();
  15. }
  16. }
  17. }

Print Reference

  1. using KhatiMediaTr;
  2. using MyProjectTest.Services;
  3. namespace MyProjectTest.CQRS.Query
  4. {
  5. public class PrintReference : IEventHandler
  6. {
  7. private readonly IMyService _myService;
  8. public PrintReference(IMyService myService)
  9. {
  10. _myService = myService;
  11. }
  12. public List<MyModel> Handler()
  13. {
  14. return _myService.GetModel();
  15. }
  16. }
  17. }

CQRS-> Command

Request

  1. public class Request : IEventHandler
  2. {
  3. private readonly IMyService _myService;
  4. public Request(IMyService myService)
  5. {
  6. _myService = myService;
  7. }
  8. public AddParms Handler(string value, string value1, List<MyModel> myModel, MyModel model)
  9. {
  10. return _myService.Request(value,value1,myModel,model);
  11. }
  12. }

Void Request

  1. using KhatiMediaTr;
  2. using MyProjectTest.Services;
  3. namespace MyProjectTest.CQRS.Command
  4. {
  5. public class DoSomething : IEventHandler
  6. {
  7. private readonly IMyService _myService;
  8. public DoSomething(IMyService myService)
  9. {
  10. _myService = myService;
  11. }
  12. public void Handler()
  13. {
  14. _myService.Wait();
  15. }
  16. }
  17. }

Inject From Controller

  1. using KhatiMediaTr;
  2. using Microsoft.AspNetCore.Mvc;
  3. using MyProjectTest.CQRS.Command;
  4. using MyProjectTest.CQRS.Query;
  5. namespace MyProjectTest.Controllers
  6. {
  7. public class HomeController : Controller
  8. {
  9. private readonly IMediaTr<PrintQuery, string> _printQuery;
  10. private readonly IMediaTr<PrintTuple, (string message , string description)> _printTupleMediaTr;
  11. private readonly IMediaTr<PrintReference, List<MyModel>> _printRefernece;
  12. private readonly IMediaTr<Request, AddParms> _requestMediaTr;
  13. private readonly IMediaTr<DoSomething,bool> _requestDoSomething;
  14. public HomeController(
  15. IMediaTr<PrintQuery, string> printQuery,
  16. IMediaTr<PrintTuple, (string message, string description)> printTupleMediaTr,
  17. IMediaTr<PrintReference, List<MyModel>> printRefernece,
  18. IMediaTr<Request, AddParms> requestMediaTr,
  19. IMediaTr<DoSomething, bool> requestDoSomething)
  20. {
  21. _printQuery = printQuery;
  22. _printTupleMediaTr = printTupleMediaTr;
  23. _printRefernece = printRefernece;
  24. _requestMediaTr = requestMediaTr;
  25. _requestDoSomething = requestDoSomething;
  26. }
  27. public IActionResult PrintQuery()
  28. {
  29. var data = _printQuery.Send();
  30. return Ok(data);
  31. }
  32. public IActionResult PrintTuple()
  33. {
  34. var data = _printTupleMediaTr.Send();
  35. return Ok(new { message = data.message, description = data.description });
  36. }
  37. public IActionResult PrintReference()
  38. {
  39. var data = _printRefernece.Send();
  40. return Ok(data);
  41. }
  42. public IActionResult FakePostRequest()
  43. {
  44. string value = "value";
  45. string value1 = "value1";
  46. MyModel model = new MyModel()
  47. {
  48. MyObject = "MyObject"
  49. };
  50. var data = new List<MyModel>();
  51. data.Add(model);
  52. var request = _requestMediaTr.Send(new object[] { value, value1, data, model });
  53. return Ok(request);
  54. }
  55. public IActionResult DoSomething()
  56. {
  57. var data = _requestDoSomething.Send();
  58. return Ok(data);
  59. }
  60. }
  61. }

Open Source

https://github.com/ShamsiShakeeb/KhatiMediaTr

Implementation in ASP.NET MVC Project

https://github.com/ShamsiShakeeb/KhatiMediaTrNugetImplementation