jeudi 24 août 2023

Design Pattern in C#

I maybe overthinking here and too conscious about using design patterns. But I am somehow stumped.

I have this main class called MainProcessor. It will have a method that will call around 20 classes' Execute() method asynchronously. These 2 classes will do its own thing in its respective 'Execute' method. It is unfortunately not Fire & Forget. The child class has to relay back a message to my MainProcessor class.

After these 20 little classes are done with the execution of their respective Execute() method, it has to convey back to the MainProcessor class if it's a success or not. The ordering of executing the Execute() method does not matter, as long as it is all async and each child class 'events' back a message to the MainProcessor class that it is done. I cannot use MSMQ or a Pub/Sub model here.

This is in .NET Core and I don't want to inject 20 'target' classes into my MainProcesor class's constructor because tomorrow we may add 10 more ChildProcessor classes and I cannot afford to change the MainProcessor class' constructor by adding more injecting logic, as it seems wrong. How can I make my class design so loosely coupled and dynamic?

If I go in with a simple abstract class with an Execute() method and implement it across my 20 child classes, how do I relay an event back to my calling class that the execute() method is done? I have never done events much to communicate between a calling and a called class, especially when it involves abstraction.

I have started off using a Concurrent Dictionary and I plan to use one of those Task.RunAll() async methods by looping through the ConcurrentDictionary. It is such an unfamiliar territory to me those Task related async calls. If I blink, I would be calling it all synchronously.

Is there a design pattern that I can think of here or what is the best way to implement this?

Thanks

Aucun commentaire:

Enregistrer un commentaire