C
C#

help

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

DDaVinki9/28/2022
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);
}
I'm just trying to have objects outside of the class fire events, but C# only allows invocations from inside of the declaring class
BBecquerel9/28/2022
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
DDaVinki9/28/2022
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
DDaVinki9/28/2022
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...
DDaVinki9/28/2022
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
BBecquerel9/28/2022
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
DDaVinki9/28/2022
I can make that work Thank you for that
BBecquerel9/28/2022
i would suggest looking into mediatr, though it seems more like what you are after
DDaVinki9/28/2022
I will look into everything you’ve told me about, I figured they were just like they were in Java and seemed unfit
BBecquerel9/28/2022
ablobnod
DDaVinki9/30/2022
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;
}
}

Looking for more? Join the community!

Want results from more Discord servers?
Add your server
Recommended Posts
How to get service that not registed with interfaceHelpAssigning all StackPanel child element marginI am trying to set the margin for all elements in stackpanel from resor. Can you point out why it's Generics and type inference```cs public TDest MapTo<TSource, TDest>(TSource src) { var res = _mapper.Map<TSourcCrash of the background worker because of the SQL connectionI have a background worker running on some machine, which getting random crashes because of the SQL Minimum need data to create an EntityMy entity: ```cs public class Book : BaseEntity { public string Name { get; set; } MediatR commands and DTOs [Answered]Currently, I am passing DTOs to my controllers, I am using MediatR and it supports commands. I use tHow can I send int value from 1 program, to another via internet?```fix Hello. Is there any easy way to connect 2 programs to each other over the internet? I mean,InterfaceHi, i've this interface but there is only 1 class which doesn't need any parameter in `getAll(long iTroubleshooting Visual studio .net 6, debuggerHi, Ran in to a so strange bug/problem this week. I'm working in a multi tier solution with several✅ Amazon Review Panic!!!Hello, I am a Freshman in college who was recently selected for a technical interview at Amazon. I aSearch for text and sort by most hits.I wrote a function like this, it looks for words in the database. I would like to make it sort by thValidation helpNeed help with some simple validationPrevent scrollbar from jumping down when viewing a list?Hi - I'm making a chat room in windows forms and have a listbox that displays all the messages recieWPF Border around transparent imageI'm trying to do a kind of "magic wand tool" selection that some image editors have, and I just can'Stack and Heap Memory [Answered]I've been correcting some of my misconception about stack and heap memory allocation. So, this is myPartialType from Nest.js in CsharpIs there is a possibility to recreate a behaviour like the partialType from Nestjs (=> https://docs.Trying to add GrossWeight each time it shows up in the listTrying to learn how to use a ForEach loop while doing my homework. I feel like this should be easierHow to read DateTime from SQLite database using System.Data.SQLite?I have a sqlite database with a DateTime entry. Since SQLite doesn't have a DateTime data type, it sBlazor VSCode --unable to find an installation of the browser on your system.-- [Answered]I'm getting an error `Unable to launch browser: "Unable to find an installation of the browser on yoCollection of different classesI have a situation where I need to store two different types together, and I'm not sure what's a goo