Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Is it possible to simplify this code?
Message
 
 
À
10/09/2013 04:40:37
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
Application:
Web
Divers
Thread ID:
01581943
Message ID:
01582678
Vues:
44
>If you have many cases or the code per case gets bigger you can use a dictionary of delegates
>It requires some code to set it up - in your case stick to switch - but maybe for future use
>
>I recently had a case with about 30 possibilities where each case calls a method
>
>
>In your case you would have - never mind the class definitions of MKVP and GroupMember - had to test
>
>
>	static class Test_DictionaryDelegates
>	{
>		delegate void ChangeGroupMember(GroupMember groupMember, MKVP mkvp);
>
>		static Dictionary<string, ChangeGroupMember> Actions = new Dictionary<string, ChangeGroupMember>()
>		{	 {"PRI_KEY", ChangePrimaryKey}
>			,{"FIRST", ChangeFirstName}
>			,{"LAST", ChangeLastName}
>			,{"GSTNO", ChangeGuestNo}
>			,{"NOTES", ChangeNotes}
>		};
>
>		//methods per case
>		static void ChangePrimaryKey(GroupMember groupMember, MKVP mkvp)
>		{
>			groupMember.PrimaryKey = Convert.ToInt32(mkvp.Value);
>		}
>		static void ChangeFirstName(GroupMember groupMember, MKVP mkvp)
>		{
>			groupMember.FirstName = mkvp.Value;
>		}
>		static void ChangeLastName(GroupMember groupMember, MKVP mkvp)
>		{
>			groupMember.LastName = mkvp.Value;
>		}
>		static void ChangeGuestNo(GroupMember groupMember, MKVP mkvp)
>		{
>			groupMember.GuestNo = Convert.ToInt64(mkvp.Value);
>
>		}
>		static void ChangeNotes(GroupMember groupMember, MKVP mkvp)
>		{
>			groupMember.Notes = mkvp.Value;
>		}
>
>		// it happens here
>		internal static  void  Go()
>		{
>			GroupMember groupMember = new GroupMember(1, "Gregory", "Adam", 234, "none");
>			MKVP mkvp = new MKVP("First", "Alan");
>
>			ChangeGroupMember action;
>			
>			if( !Actions.TryGetValue(mkvp.Key.ToUpper(), out action))
>				throw new NotImplementedException(string.Format("Don't know what to do with {0}", mkvp.Key));
>
>			action(groupMember, mkvp);
>
>		}
>	
>	}
>
>
>	class MKVP
>	{
>		public string Key;
>		public string Value;
>		public MKVP(string key, string value)
>		{
>			Key = key;
>			Value = value;
>		}
>	}
>	class GroupMember
>	{
>		public int PrimaryKey ;
>		public string FirstName;
>		public string LastName;
>		public long GuestNo;
>		public string Notes;
>		public GroupMember(int primaryKey ,string firstName, string lastName, long guestNo, string notes)
>		{
>			PrimaryKey = primaryKey;
>			FirstName = firstName;
>			LastName = lastName;
>			GuestNo = guestNo;
>			Notes = notes;
>		}
>	}
>
Thanks, Gregory. We actually do use the exact same idea for our main Invoke method. This is what we have:
      private Dictionary<String, InvokeMethod> GetInvokeMethods()
      {
         Dictionary<String, InvokeMethod> methodDictionary = new Dictionary<String, InvokeMethod>(StringComparer.OrdinalIgnoreCase)
            {
                // Base
                {"GetServerVersion", GetServerVersion},
                
                //Data
                {"AppendRecs"                           , data.AppendRecs},            // JT 02/18/2013 Done
                {"GetItemNextModified"                  , data.GetItemNextModified},   // JT 03/01/2013 Done
                {"GetItemTable"                         , data.GetItemTable},          // JT 03/05/2013 Done
                {"GetITNextModified"                    , data.GetITNextModified},     // JT 03/07/2013 Done
                {"GetITTable"                           , data.GetITTable},            // JT 03/07/2013 Done
                {"GetMaxKey"                            , data.GetMaxKey},             // JT 03/08/2013 Done
                {"GetMatchingRecs"                      , data.GetMatchingRecs},       // JT 03/08/2013 Done
                {"GetModsAfter"                         , data.GetModsAfter},          // JT 03/11/2013 Done
                {"GetPrimaryKeyList"                    , data.GetPrimaryKeyList},     // JT 03/11/2013 Done
                {"GetRecordCount"                       , data.GetRecordCount},        // JT 03/13/2013 Done
                // {"GetSqlFunctionsAndProcedures"         , data.GetSqlFunctionsAndProcedures}, // Obsolete.  Replaced with database.serverSprocsAndFuncs dictionary
                // {"GetSqlProcedureName"                  , data.GetSqlProcedureName},          // JT 03/13/2013 Done. Refactored to: database.GetSqlProcedureName()
                {"GetTable"                             , data.GetTable},              // JT 03/13/2013 Done
                {"GetTableStructure"                    , data.GetTableStructure},     // JT 03/14/2013 Done
                {"GetTotal"                             , data.GetTotal},              // JT 03/14/2013 Done
                // {"GetUniqueKey"                         , data.GetUniqueKey},       // JT 03/15/2013 Done. Refactored to: database.GetUniqueKey()
                {"GetWithPrimaryKey"                    , data.GetWithPrimaryKey},     // JT 03/18/2013 Done
                {"ModifyRec"                            , data.ModifyRec},             // JT 03/18/2013 Done
etc. (other classes I coded mostly - about 300+ methods)
Instantiating the class
this.invokeMethods = GetInvokeMethods();
and then in the method that invokes them:
   try
         {
            cReturnValue = invokeMethods[parameters["func"]](parameters);
         }
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform