Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Multiple return points - is it OK practice?
Message
De
03/02/2013 03:45:39
 
 
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Divers
Thread ID:
01564936
Message ID:
01565027
Vues:
69
This message has been marked as a message which has helped to the initial question of the thread.
>>>>As to the parameter, my thought was that by using a struct you are passing in a single parameter object and that object will have fields of only the proper types. While it is true that does not otherwise validate the values, it does go a long way to remove param checking from the responsibility of the method it is passed to and can be handled elsewhere as the params are assmebled.
>>>>
>>>>The idea of passing in something like XML as a param sets my teeth on edge. ( though I suppose you could validate its creation elsewhere, you still end up parsing it out in a way that makes the receiving method clutter with stuff that would have to be duplicated anywhere that param was used.
>>>
>>>The XML string with parameters is passed from another application (written in C++) which we're not going to change. We're only re-writting one piece which is currently in VFP those job is to accept these parameters passed as XML (along with the method name in func MethodName /func) and execute it and return back either XML or a string.
>>>
>>>So, C# dll will receive the parameters the same way (as XML string) and needs to produce exactly the same output as VFP applicaton was producing.
>>
>>
>>Sure, but you can still receive the XML in your method, parse it into an object, and then pass the object to another method, thereby delegating the actual business logic to one method and the XML interface to that logic in another method.
>
>Can you please clarify your point with some code?
>
>Right now we have several helper methods - one to verify parameters and returned types and another is to retrieve parameter by name (as a string) from that Dictionary of parameters.
>
>So, all the methods currently have this as a start:
>
>
>internal static String GetGuestLiabilityAcceptance(Dictionary<String, String> parameters)
>        {
>            Logging.Log(1, "Entering GetGuestLiabilityAcceptance");
>
>            List<String> requiredParameters = new List<String>() { "tnGuestNo" };
>            //always required parameters (but not used)          { "tcSalesPoint", "tcOperator" };
>            
>            HashSet<String> availableReturnTypes = new HashSet<String>(StringComparer.OrdinalIgnoreCase) { "STR", "ADO", "XML", "XM2" };
>
>            String messageText = "";
>            Int32 errorValue = 0;
>            Int32 statusCode = 0;
>
>            // If all necessary parameters are present and valid, proceed...
>            if (Functions.CheckRequiredParameters(parameters, requiredParameters, availableReturnTypes, out statusCode, out messageText))
>            {
>                // Parse out the parameters
>                String tcGuestNo = Functions.GetParameterValue(parameters, "tnGuestNo");
>            
>                Decimal guestNo;
>
>How do you suggest to change the above?

If the number of possible XML fragments is limited then, as Mike suggests, mapping each to concrete C# classes is an option.

If they are not then I'd at least think about replacing the CheckRequiredParameters function with a method that returns a dynamic object. Pass in the element names you are expecting and use these as property names in the return object. Successful mapping would then guarantee that those property names were valid in the following code.

UPDATE: Simple example (with no error handling :-} ):
using System;
using System.Collections.Generic;
using System.Dynamic;

namespace ConsoleApplication1
{
  internal class Program
  {
    private static void Main()
    {
      string theXML = "<This>This thing</This><That>That thing</That>";
      List<string> requiredElements = new List<string>{"This","That"};

      dynamic d = GetObject(theXML, requiredElements);

      Console.WriteLine(d.This);
      Console.WriteLine(d.That);
    }

    static dynamic GetObject(string xml, IEnumerable<string> elements)
    {
      dynamic result = new ExpandoObject();
      var dict = result as IDictionary<string, Object>;
      foreach (string s in elements)
      {
        dict.Add(s,GetElement(s,xml));
      }
      return result;
    }

    static dynamic GetElement(string name,string xml)
    {
      //Cheating here.  You would need to parse out the value for the element
      return "This is the value for " + name;
    }
  }
}
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform