Quick way to find a GUID

I have many (+2000) GUIDs (in some kind of network class), and my program should find one of them when it receives a message and performs a task related to it.
On the plus side, I have a hard code generator, but the fastest way is my goal (and I don’t know how to implement it).

my code should do something like this:

switch(received guid) { case guid1: do job 1; break; case guid2: do job 2; break; case guid3: do job 3; break; case guid4: do job 4; break; .... } 
+4
source share
6 answers

Create an interface for the job, then implement 2,000 classes that do the work, each of which knows its own guid. Then add the classes to the dictionary using its guid as the key. Then, when you get the guid, you look at the object in the dictionary and call the method on the interface.

 public interface IJobDoer { void DoJob(); Guid Guid{get;} } public class FirstJobType : IJobDoer { void DoJob() { /// whatever... } Guid Guid { get{return "insert-guid-here";}} } 
+8
source

You can create a dictionary with instructions as a key and a delegate link as a value. This will provide a quick search.

+15
source

Use a hash table that maps Guid to the delegate or class that represents the task, such as Dictionary<Guid, Action> or Dictionary<Guid, Task> .

+6
source

A Dictionary<Guid, JobDelegate> is likely to be faster than a switch statement.

But you will definitely need a profile.

+4
source

I like to show variations of the vocabulary approach that others have already suggested. Based on this decision, you can do the following.

1 Define the base class:

 public abstract class JobDoer { public abstract void DoJob(); } 

2 Define an attribute for the design of workers.

 public sealed class JobDoerAttribute : Attribute { JobDoerAttribute(string jobDoerId) { this.JobDoerId = new Guid(jobDoerId); } public Guid JobDoerId { get; private set; } } 

3 Define the actual job classes that are decorated with this attribute. For instance:

 [JobDoer("063EE2B2-3759-11DF-B738-49BB56D89593")] public sealed class SpecificJobDoer : JobDoer { public override void DoJob() { // Do a specific job } } 

4 Define a JobDoerFactory that allows you to retrieve instances of JobDoer by their identifier, as defined in the attribute:

 public static class JobDoerFactory { static Dictionary<Guid, JobDoer> cache; static JobDoerFactory() { // Building the cache is slow, but it will only run once // during the lifetime of the AppDomain. cache = BuildCache(); } public static JobDoer GetInstanceById(Guid jobDoerId) { // Retrieving a JobDoer is as fast as using a switch statement. return cache[jobDoerId]; } private static Dictionary<Guid, JobDoer> BuildCache() { // See implementation below. } } 

In the BuildCache method BuildCache you can load instances of JobDoer using reflection.

 private static Dictionary<Guid, JobDoer> BuildCache() { // This is a bit naive implementation; we miss some error checking, // but you'll get the idea :-) var jobDoers = (from assembly in AppDomain.CurrentDomain.GetAssemblies() from type in assembly.GetTypes() where type.IsSubclassOf(typeof(JobDoer)) let attributes = type.GetCustomAttribute(typeof(JobDoerAttribute), true) where attributes.Length > 0 let attribute = attributes[0] as JobDoerAttribute select new { attribute.JobDoerId, type }).ToArray(); var cache = new Dictionary<Guid, JobDoer>(jobDoers.Length); foreach (jobDoer in jobDoers) { // Note that actually a single instance of the job doer is // cached by ID. This means that every Job Doer must be // thread-safe and usable multiple times. If this is not // feasable, you can also create store a set of Func<JobDoer> // objects that enable creating a new instance on each call. cache[jobDoer.JobDoerId] = (JobDoer)Activator.CreateInstance(jobDoer.type); } return cache; } 

I have not tested this code, so I don’t know if it compiles, but I used this mechanism in the project several years ago. Thus, it is easy to define new classes without having to connect them to some dictionary. This is done automatically at runtime.

This may sound a bit like overkill, but if you have the +2000 JobDoer classes, this can help you a lot.

Update: Please note: if you do not like the idea of JobDoerAttribute , you can implement it as an abstract property in the abstract JobDoer class. However, I found that using the attribute makes the code very explicit and expressive.

+3
source

Create a dictionary with instructions and actions and search on it.

0
source

Source: https://habr.com/ru/post/1305001/


All Articles