C # enums and booleans - finding a more elegant way

I have a class that has an enum property and a boolean property based on the fact that it calls a specific method with specific parameters. I use the switch statement to enumerate and if for the logical switch in each case. This is a long list and does not consider me the most elegant solution. Has anyone got a more elegant or simpler way to implement this?

            switch (ReadDecision)
            {
                case ReadDecisions.ReadNext:
                    {
                        if (UseTimeout)
                        {
                            Message = queue.Receive(Timeout);
                        }
                        else
                        {
                            Message = queue.Receive();
                        }
                        break;
                    }
                case ReadDecisions.PeekNext:
                    {
                        if (UseTimeout)
                        {
                            Message = queue.Peek(Timeout);
                        }
                        else
                        {
                            Message = queue.Peek();
                        }
                        break;
                    }
                case ReadDecisions.ReadMessageId:
                    {
                        if (UseTimeout)
                        {
                            Message = queue.ReceiveById(Id, Timeout);
                        }
                        else
                        {
                            Message = queue.ReceiveById(Id);
                        }
                        break;
                    }
                case ReadDecisions.PeekMessageId:
                    {
                        if (UseTimeout)
                        {
                            Message = queue.PeekById(Id, Timeout);
                        }
                        else
                        {
                            Message = queue.PeekById(Id);
                        }
                        break;
                    }
                case ReadDecisions.ReadCorrelationId:
                    {
                        if (UseTimeout)
                        {
                            Message = queue.ReceiveByCorrelationId(Id, Timeout);
                        }
                        else
                        {
                            Message = queue.ReceiveByCorrelationId(Id);
                        }
                        break;
                    }
                case ReadDecisions.PeekCorrelationId:
                    {
                        if (UseTimeout)
                        {
                            Message = queue.PeekByCorrelationId(Id, Timeout);
                        }
                        else
                        {
                            Message = queue.PeekByCorrelationId(Id);
                        }
                        break;
                    }
                default:
                    {
                        throw new Exception("Unknown ReadDecisions provided");
                    }
            }
+3
source share
11 answers

A commonly used convention is that a timeout of zero means no timeout. Maybe you can remove UseTimeout (property?) And use a null value instead. This will eliminate some things.

+3

, queue, Peek() Peek(bool) Peek(bool?)? ( .)

, :

if (UseTimeout)
{
    Message = queue.Peek(Timeout);
}
else
{
    Message = queue.Peek();
}

:

Message = queue.Peek(UseTimeout ? Timeout : null);

, :

if(UseTimeout)
    Message = queue.PerformAction(ReadDecision, Timeout)
else
    Message = queue.PerformAction(ReadDecision)
+5

, , - foo (ID) foo (ID, timeout). :

  • foo(ID, timeout, bool useTimeout), foo(ID) foo(ID, timeout).
  • foo(ID) foo(ID, null, false) foo(ID, timeout) foo(ID, timeout, true)
  • switch, foo(ID, Timeout, UseTimeout).
  • .

, , - , .

+3

, -, -. , , - . OTOH, switch - , enum- > classes.

switch (ReadDecision)
 {
  case ReadDecisions.ReadNext:
   {
    Message = queue.Receive(Timeout);
    break;
   }
  case ReadDecisions.PeekNext:
   {
    Message = queue.Peek(Timeout);
    break;
   }
  case ReadDecisions.ReadMessageId:
   {
    Message = queue.ReceiveById(Id, Timeout);
    break;
   }
  case ReadDecisions.PeekMessageId:
   {
    Message = queue.PeekById(Id, Timeout);
    break;
   }
  case ReadDecisions.ReadCorrelationId:
   {
    Message = queue.ReceiveByCorrelationId(Id, Timeout);
    break;
   }
  case ReadDecisions.PeekCorrelationId:
   {
    Message = queue.PeekByCorrelationId(Id, Timeout);
    break;
   }
  default:
   {
    throw new Exception("Unknown ReadDecisions provided");
   }
  }
+2

, , , :

Dictionary<ReadDecisions,YourAction> decisions = new
           Dictionary<ReadDecisions,YourAction>();

decisions[ReadDecisions.ReadNext] = queue.Receive;
decisions[ReadDecisions.PeekNext] = queue.PeekById;
// you understand the main idea
//.... 

:

if(UseTimeout)
   decisions[ReadDecision](id, Timeout);
else
   decisions[ReadDecision](id, 0);  //or another value 
                                    //telling you there is no timeout .
                                    // it just have to be the same signature

, .

+1

- , ReadDecision UseTimeout?
, ...

0

int? optionalTimeout = UseTimeout ? Timeout : null;

switch (ReadDecision) {
    case ReadDecisions.ReadNext:
        Message = queue.Receive( optionalTimeout ); break;

    case ReadDecisions.PeekNext:
        Message = queue.Peek( optionalTimeout ); break;

    // and so on ...

queue null, Timeout " "

0

:

1] ""

NULL. , :

Queue.Peek(TimeOut)
Queue.Receive(TimeOut)

Queue.ReceiveById(Id, TimeOut)
Queue.PeekById(Id, TimeOut)
...

2]

delegate Message MethodType1(Timeout)
delegate Message MethodType2(Id, TimeOut)

3] GetDelegate(),

object GetDelegate(ReadDecisions param)
{
 switch(param)
 {

   case ReadNext:
             MethodType1 receiveDlgt = new MethodType1(queue.Receive);
   case PeekMessageId:
             MethodType2 peekDlgt = new MethodType2(queue.Peek);
   ...
 }
}

4]

InvokingMethod()
{
  object methodToExecute = GetDelegate(ReadDecision)
  if (methodToExecute.GetType() == typeof(MethodType1)) 
    {
    methodToExecute(TimeOut)
    }
    else
    {
    methodToExecute(Id, TimeOut)
    }

}

, . .

0

, q, . , ? MessageQueue queue = new MessageQueue (queuePath); // queue.ReceiveCompleted + = QueueMessageReceived;// // ""  queue.BeginReceive();

0

, , . , . .

For example, the use of a fancy template suggested by someone else regarding the use of classes instead of your enumeration is likely to lead to the creation of a new class for each case statement, and they will probably go into the file each and you will need a different file for the base interface. .. and that would be a hell of a lot. I don’t know how someone could justify this feature of many things as an easier way to express what you have already done quite well.

-1
source

All Articles