开发者

C# Avoid Repetitive Code

开发者 https://www.devze.com 2022-12-09 04:01 出处:网络
I am sure someone may have already asked this type of questions before, but I can\'t seem to find a similar question.

I am sure someone may have already asked this type of questions before, but I can't seem to find a similar question.

I have something like this:

client = Client.GetInstance(type.objectA));
if (client != null)
{
  result += client.SaveObjectA(object);
}


client = Client.GetInstance(type.objectB));
if (client != null)
{
  result += client.SaveObjectB(object);
}


client = Client.GetInstance(type.objectC));
if (client != null)
{
  result += client.SaveObjectC(object);
}


client = Client.GetInstance(type.objectD));
if (client != null)
{
  result += client.SaveObjectD(object);
}     

I wanna find a good way to reduce this repetitive code.

Please let me know your good thoughts.

Thank you

*** Additional to what i put in previously Forgot to mention a very important part Those methods were generated from a webservice. Here is the interface.

public i开发者_JAVA百科nterface Client
    {
        string SaveObjectA(object);
        string SaveObjectB(object);
        string SaveObjectC(object);
        string SaveObjectD(object);

    }    


Sounds like inheritance would do the trick. Let each client inherit from the same Interface consisting of the method

SaveObject(object o);

then you can just write:

if (client!=null)
{
    result+=client.SaveObject(object);
}

and the polymorphism selects the correct version of SaveObject, depending on the type of the client.


It depends on where you want to put the responsibility for saving objects, but I can think of a couple of different ways using interfaces or a factory that knows both how to create and save objects, perhaps a combination of the two.

string result = string.Empty;
foreach (var type in new Type[] { type.objectA, type.objectB, type.objectC, type.objectD })
{
   var client = Client.GetInstance(type) as IPersistable;
   result += client.Save();
}

where each client implements the IPersistable interface which defines a Save() method.

or

string result = string.Empty;
foreach (var type in new Type[] { type.objectA, type.objectB, type.objectC, type.objectD })
{
   var client = Client.GetInstance(type);
   result += Client.Save( client );
}

where the Client class knows how to Save each type of object it creates.


This can be done the way you want (see below), however, I recommend implementing a generic, reflection-based solution which will work with most object types.

If you must have a different name for each save method
(giving each save method a different name is usually not good design),
use a hashtable (dictionary) for best performance and less repetitive code:

(If you add the hashtable to the using class instead of a static extension class, you will end up with less code).

static ClientExtensions : class
{
    private delegate string MyDelegate(IClient, object);

    private static Dictionary<Type, MyDelegate> myDictionary
        = new Dictionary<Type, MyDelegate>();

    /// <summary>Static Contstructor</summary>
    static MyExtenderType()
    {
        myDictionary.Add(typeof(ClientA), SaveObjectAExtensionMethod);
        myDictionary.Add(typeof(ClientB), SaveObjectBExtensionMethod);
        myDictionary.Add(typeof(ClientC), SaveObjectCExtensionMethod);
        myDictionary.Add(typeof(ClientD), SaveObjectDExtensionMethod);
    }

    // TODO: copy for B, C & D
    public static string SaveObjectAExtensionMethod(this IClient client, object obj)
    {
        return client.SaveObjectA(obj);
    }

    public static string SaveObject(this IClient client, object obj)
    {
        MyDelegate dele;
        if (this.myDictionary.TryGetValue(typeof(client), out dele))
            return dele(client, obj);

        throw new NotSupported...
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消