Design Pattern for mapping generic class type to implementation

Hey all,

I'm working on an executor service that maps an executor record of a generic type to an executor that can handle performing operations on that type. Currently, I'm receiving errors on a type conversion in my executor methods for being unable to convert a type and I am not sure how to fix it in my design. I believe this design pattern is similar to mapping a command to a handler, but I am just having trouble implementing it. If you can give me feedback on how to my design or how to do it properly it would help a ton, I have been stuck on this for a bit trying different things.

To start off, I have a ExecutorService thats job is to take an executor record and map it to an executor and return the results. Each executor has it's own implementation with a catch all "object" executor as default. This requires a few components:
  1. ExecutorService, which is my service that my controller uses.
  2. ExecutorManager, which is my executor manager that registers all executors from DI and allows the executor service to get a executor by key and pass the executor record to that executor to perform an operation.
  3. Executor, which is a handler for performing basic crud operations on executor records of a specific type.
  4. ExecutorRecord, which is class for storing generic types of data while containing basic information about that data that is common across executors.
  5. ExampleRepository, which is a repository that ExecutorRecords will get from DI for what repository to call for basic CRUD operations.
I use my executor service in my controller by creating a record of a specific type and passing it to my ExecutorService with a executor key to map to an specific executor. This approach works well and gives the consumers a nice generic API to work with.
c#
    [ApiController]
    [Route("[controller]")]
    public class ExecutorController : ControllerBase
    {
        private readonly ILogger<ExecutorController> _logger;

        private readonly IExecutorService _executorService;

        public ExecutorController(ILogger<ExecutorController> logger, IExecutorService executorService)
        {
            Requires.NotNull(logger, nameof(logger));
            Requires.NotNull(executorService, nameof(executorService));

            _logger = logger;
            _executorService = executorService;
        }

        [HttpPost(Name = "CreateRecord")]
        public IActionResult Create()
        {
            ExampleExecutorData exampleExecutorData = new ExampleExecutorData();
            ExecutorRecord<object> executorRecord = new ExecutorRecord<object>(Guid.NewGuid(), exampleExecutorData);

            IExecutorRecord<object> createdRecord = _executorService.Create(executorRecord, ExecutorKey.Default).Result;

            return new JsonResult(createdRecord);
        }
    }
Was this page helpful?