如何简化C#中的代码?

时间:2012-10-25 06:17:04

标签: c# .net

这是我想要简化的代码:

       public void Method1(Context context, EventLog log = null)
        {
            Class myClass = ConvertToMyClass();
            ApiCall1 apiCall = new ApiCall1(context);
            if (log != null)
            {
                eventLog.WriteEntry("Starting");
            }

            try
            {
                apiCall.Call1(myClass, null, false);
                IsCallSuccess = true;
            }
            catch (Exception e)
            {
                if (log != null)
                {
                    eventLog.WriteEntry("error");
                }

                IsCallSuccess= false;
                CallErrorMessage = e.Message;
            }
        }

        public void Method2(Context context, EventLog log = null)
        {
            Class myClass = ConvertToMyClass();
            ApiCall2 apiCall = new ApiCall2(context);
            if (log != null)
            {
                eventLog.WriteEntry("Starting");
            }

            try
            {
                apiCall.Call1(myClass);
                NewItemID = myClass.ItemID;
                IsCallSuccess = true;
            }
            catch (Exception e)
            {
                if (log != null)
                {
                    eventLog.WriteEntry("error");
                }

                IsCallSuccess= false;
                CallErrorMessage = e.Message;
            }
        }

        public void Method3Context context, EventLog log = null)
        {
            Class myClass = ConvertToMyClass();
            ApiCall3 apiCall = new ApiCall3(context);
            if (log != null)
            {
                eventLog.WriteEntry("Starting");
            }

            try
            {
                apiCall.Call3(myClass, "param1");
                UpdatedItemID = myClass.UpdatedItemID;
                IsCallSuccess = true;
            }
            catch (Exception e)
            {
                if (log != null)
                {
                    eventLog.WriteEntry("error");
                }

                IsCallSuccess= false;
                CallErrorMessage = e.Message;
            }
        }

有3种方法。我一直在考虑如何使用委托或lambdas简化它们并且没有找到任何东西。

你的想法?

1 个答案:

答案 0 :(得分:1)

两种方式......

1)使方法具有通用性,因为这三种方法只是它们影响的类别不同。这看起来像这样:

public void Method<T>(Context context, EventLog log = null) where T : BaseAPICall
{
    Class myClass = ConvertToMyClass();
    T apiCall = (T)Activator.CreateInstance(typeof(T), new object[] { context });
    if (log != null)
    {
        eventLog.WriteEntry("Starting");
    }

    try
    {
        apiCall.Call(myClass, null, false);
        IsCallSuccess = true;
    }
    catch (Exception e)
    {
        if (log != null)
        {
            eventLog.WriteEntry("error");
        }

        IsCallSuccess= false;
        CallErrorMessage = e.Message;
    }
}

2)将这些方法放在适当类的父类中,在这种情况下,它将是ApiCall1 / 2/3的父类。这将是:

public class BaseAPICall
{
    public abstract Call(Class c, object o, bool b);

    public virtual Method(Context context, EventLog log = null)
    {
        Class myClass = ConvertToMyClass();
        if (log != null)
        {
            eventLog.WriteEntry("Starting");
        }

        try
        {
            this.Call(myClass, null, false);
            IsCallSuccess = true;
        }
        catch (Exception e)
        {
            if (log != null)
            {
                eventLog.WriteEntry("error");
            }

            IsCallSuccess = false;
            CallErrorMessage = e.Message;
        }
    }
}