Create a Data-Driven Messaging System: Listing A

// C#
protected virtual string GetMessageFromStorage()
{
  XElement xe = null;
  XElement xeMsg = null;

  mintWhereClauses = pdsaMsgWhereClause.None;

  if (mstrClassName != string.Empty &&
      mstrMessageName != string.Empty &&
      mintMessageNumber != Int32.MinValue)
    mintWhereClauses = pdsaMsgWhereClause.MsgNameClassNumber;
  else if (mstrClassName != string.Empty &&
               mstrMessageName != string.Empty)
    mintWhereClauses = pdsaMsgWhereClause.ClassNameMessageName;
  else if (mstrMessageName != string.Empty &&
              mintMessageNumber != Int32.MinValue)
    mintWhereClauses = pdsaMsgWhereClause.MessageNameNumber;
  else if (mstrMessageName != string.Empty)
    mintWhereClauses = pdsaMsgWhereClause.MessageNameOnly;
  else if (mintMessageID != Int32.MinValue)
    mintWhereClauses = pdsaMsgWhereClause.MessageIDOnly;

  xe = XElement.Load(mstrStorage);
  // Get Message using LINQ to XML
  switch (mintWhereClauses)
  {
    case pdsaMsgWhereClause.MsgNameClassNumber:
      xeMsg = (from msg in xe.Descendants("pdsaMessage")
               where msg.Element("MessageName").Value ==
                           mstrMessageName
               && msg.Element("MessageNumber").Value ==
                      mintMessageNumber.ToString()
               && msg.Element("ClassName").Value ==
                     mstrClassName
               select msg).SingleOrDefault();

      break;

    case pdsaMsgWhereClause.ClassNameMessageName:
      xeMsg = (from msg in xe.Descendants("pdsaMessage")
               where msg.Element("MessageName").Value ==
                           mstrMessageName
               && msg.Element("MessageNumber").Value == ""
               && msg.Element("ClassName").Value ==
                      mstrClassName
               select msg).SingleOrDefault();

      break;

    case pdsaMsgWhereClause.MessageIDOnly:
      xeMsg = (from msg in xe.Descendants("pdsaMessage")
               where msg.Element("MessageID").Value ==
                           mintMessageID.ToString()
               select msg).SingleOrDefault();

      break;

    case pdsaMsgWhereClause.MessageNameNumber:
      xeMsg = (from msg in xe.Descendants("pdsaMessage")
               where msg.Element("MessageName").Value ==
                           mstrMessageName
               && msg.Element("MessageNumber").Value ==
                      mintMessageNumber.ToString()
               && msg.Element("ClassName").Value == ""
               select msg).SingleOrDefault();

      break;

    case pdsaMsgWhereClause.MessageNameOnly:
      xeMsg = (from msg in xe.Descendants("pdsaMessage")
               where msg.Element("MessageName").Value ==
                           mstrMessageName
               && msg.Element("MessageNumber").Value == ""
               && msg.Element("ClassName").Value == ""
               select msg).SingleOrDefault();

      break;

    default:
      // Throw exception
      break;
  }

  if (xeMsg != null)
    mstrMessage = xeMsg.Element("Message").Value;

  return mstrMessage;
}

' VB.NET
Protected Overridable Function GetMessageFromStorage() As String
  Dim xe As XElement = Nothing
  Dim Message As XElement = Nothing

  mintWhereClauses = pdsaMsgWhereClause.None

  If mstrClassName <> String.Empty And _
     mstrMessageName <> String.Empty And _
     mintMessageNumber <> Int32.MinValue Then
    mintWhereClauses = pdsaMsgWhereClause.MsgNameClassNumber
  ElseIf mstrClassName <> String.Empty And _
             mstrMessageName <> String.Empty Then
    mintWhereClauses = pdsaMsgWhereClause.ClassNameMessageName
  ElseIf mstrMessageName <> String.Empty And _
             mintMessageNumber <> Int32.MinValue Then
    mintWhereClauses = pdsaMsgWhereClause.MessageNameNumber
  ElseIf mstrMessageName <> String.Empty Then
    mintWhereClauses = pdsaMsgWhereClause.MessageNameOnly
  ElseIf mintMessageID <> Int32.MinValue Then
    mintWhereClauses = pdsaMsgWhereClause.MessageIDOnly
  End If

  xe = XElement.Load(mstrStorage)
  ' Get Message using LINQ to XML
  Select Case mintWhereClauses

    Case pdsaMsgWhereClause.MsgNameClassNumber
      Message = (From msg In xe...<pdsaMessage> _
           Where msg.<MessageName>.Value = mstrMessageName _
           And msg.<MessageNumber>.Value = _
               mintMessageNumber.ToString() _
           And msg.<ClassName>.Value = mstrClassName _
           Select msg).SingleOrDefault()

    Case pdsaMsgWhereClause.ClassNameMessageName
      Message = (From msg In xe...<pdsaMessage> _
           Where msg.<MessageName>.Value = mstrMessageName _
           And msg.<MessageNumber>.Value = "" _
           And msg.<ClassName>.Value = mstrClassName _
           Select msg).SingleOrDefault()

    Case pdsaMsgWhereClause.MessageIDOnly
      Message = (From msg In xe...<pdsaMessage> _
           Where msg.<MessageID>.Value = mintMessageID.ToString() _
           Select msg).SingleOrDefault()

    Case pdsaMsgWhereClause.MessageNameNumber
      Message = (From msg In xe...<pdsaMessage> _
           Where msg.<MessageName>.Value = mstrMessageName _
           And msg.<MessageNumber>.Value = _
               mintMessageNumber.ToString() _
           And msg.<ClassName>.Value = "" _
           Select msg).SingleOrDefault()

    Case pdsaMsgWhereClause.MessageNameOnly
      Message = (From msg In xe...<pdsaMessage> _
           Where msg.<MessageName>.Value = mstrMessageName _
           And msg.<MessageNumber>.Value = "" _
           And msg.<ClassName>.Value = "" _
           Select msg).SingleOrDefault()

    Case Else
      ' Throw exception

  End Select

  If Message IsNot Nothing Then
    mstrMessage = Message.<Message>.Value
  End If

  Return mstrMessage
End Function
comments powered by Disqus
Most   Popular
Upcoming Events

.NET Insight

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.