C
C#3y ago
DaVinki

Is there any way to simplify this, or a design pattern I should be following?

public event EventHandler<FizzbuzzDetermineNumberRequest>? DetermineNumberRequestHandlers;
public event EventHandler<FizzbuzzNumberDeterminedEvent>? NumberDeterminedEventHandlers;
public event EventHandler<FizzbuzzPrintInfo>? PrintRequestHandlers;
public event EventHandler<FizzbuzzPrintInfo>? PrintEventHandlers;

public void RequestDetermineNumber(object? sender, FizzbuzzDetermineNumberRequest? rq)
{
DetermineNumberRequestHandlers?.Invoke(sender, rq);
}

public void FireNumberDeterminedEvent(object? sender, FizzbuzzNumberDeterminedEvent ev)
{
NumberDeterminedEventHandlers?.Invoke(sender, ev);
}

public void RequestPrint(object? sender, FizzbuzzPrintInfo rq)
{
PrintRequestHandlers?.Invoke(sender, rq);
}

public void FirePrintEvent(object? sender, FizzbuzzPrintInfo ev)
{
PrintEventHandlers?.Invoke(sender, ev);
}
public event EventHandler<FizzbuzzDetermineNumberRequest>? DetermineNumberRequestHandlers;
public event EventHandler<FizzbuzzNumberDeterminedEvent>? NumberDeterminedEventHandlers;
public event EventHandler<FizzbuzzPrintInfo>? PrintRequestHandlers;
public event EventHandler<FizzbuzzPrintInfo>? PrintEventHandlers;

public void RequestDetermineNumber(object? sender, FizzbuzzDetermineNumberRequest? rq)
{
DetermineNumberRequestHandlers?.Invoke(sender, rq);
}

public void FireNumberDeterminedEvent(object? sender, FizzbuzzNumberDeterminedEvent ev)
{
NumberDeterminedEventHandlers?.Invoke(sender, ev);
}

public void RequestPrint(object? sender, FizzbuzzPrintInfo rq)
{
PrintRequestHandlers?.Invoke(sender, rq);
}

public void FirePrintEvent(object? sender, FizzbuzzPrintInfo ev)
{
PrintEventHandlers?.Invoke(sender, ev);
}
11 Replies
DaVinki
DaVinkiOP3y ago
I'm just trying to have objects outside of the class fire events, but C# only allows invocations from inside of the declaring class
becquerel
becquerel3y ago
this is the best way I know of to fire events inside another class however i'd question if you actually want events if you need to do this consider having public Action<>, Func<>, or straight delegates instead or, use the mediator pattern to take it to another level of abstractness the mediatr nuget package is highly-regarded
DaVinki
DaVinkiOP3y ago
Do actions and funcs have subscribers like events? I read about some design pattern that isn’t well known called CORE, stands for context objects requests events Everything is a module called upon through requests and events
DaVinki
DaVinkiOP3y ago
CORE design pattern — the way out from overly complicated code | Ha...
Have you ever found yourself stuck in the crazy amount of overly complicated code? No? But you know what it is, right? So let’s suppose you are a developer (at any level of evolution) or you are anyhow involved in product development, then this article is for you! Why? Let’s see the short answer, and then you’re welcome to read more about the so...
DaVinki
DaVinkiOP3y ago
So if code wants something done, it makes a request in a context and the module receiving those requests does that and makes an event with the results
becquerel
becquerel3y ago
events are identical to actions/funcs under the hood the 'subscriber' to an event is just a method added to the underlying delegate's invocation list so you can do
Action myAction = Method1;
myAction += Method2;
myAction += Method3;

myAction(); // all three methods will run
Action myAction = Method1;
myAction += Method2;
myAction += Method3;

myAction(); // all three methods will run
DaVinki
DaVinkiOP3y ago
I can make that work Thank you for that
becquerel
becquerel3y ago
i would suggest looking into mediatr, though it seems more like what you are after
DaVinki
DaVinkiOP3y ago
I will look into everything you’ve told me about, I figured they were just like they were in Java and seemed unfit
becquerel
becquerel3y ago
ablobnod
DaVinki
DaVinkiOP3y ago
So I tested out Actions and Funcs today, neat stuff and created a bucket sort algorithm using a key extractor function But I have an issue with actions I have this bit of code that I used to test subscribing methods to the invocation
public static void Main(string[] args)
{
var actions = new Action(() => { });

DoSomething.AddGreetingToProgram(actions);
actions += () => Console.WriteLine(1);
actions += () => Console.WriteLine(2);
actions += () => Console.WriteLine(3);

actions.Invoke();
}
public static void Main(string[] args)
{
var actions = new Action(() => { });

DoSomething.AddGreetingToProgram(actions);
actions += () => Console.WriteLine(1);
actions += () => Console.WriteLine(2);
actions += () => Console.WriteLine(3);

actions.Invoke();
}
So it prints 1 2 and 3 just fine But when I subscribe something else to the action, it is not run when the action is invoked I did it through another class to see if it's just like events, where you can only subscribe from inside of the declaring class
internal static class DoSomething
{
public static void AddGreetingToProgram(Action actions)
{
void PrintGreetings() => Console.WriteLine("Greetings from static context");
actions += PrintGreetings;
}
}
internal static class DoSomething
{
public static void AddGreetingToProgram(Action actions)
{
void PrintGreetings() => Console.WriteLine("Greetings from static context");
actions += PrintGreetings;
}
}

Did you find this page helpful?