重构函数以使更通用

时间:2011-06-07 17:19:52

标签: c# refactoring

我多次复制/粘贴以下代码。更改的值是字符串文字(“TabStates”更改为“ContentStates”等...)和字典的值(RadTabSetting - > ContentSetting)。

public static SerializableDictionary<string, RadTabSetting> GetTabStates()
{
    SerializableDictionary<string, RadTabSetting> _tabStates = new SerializableDictionary<string, RadTabSetting>();

    if (!object.Equals(DashboardSessionRepository.Instance.GetSession("TabStates"), null))
    {
        _tabStates = DashboardSessionRepository.Instance.GetSession("TabStates") as SerializableDictionary<string, RadTabSetting>;
    }
    else
    {
        XmlSerializer serializer = new XmlSerializer(_tabStates.GetType());

        string data = DashoardDatabaseRepository.Instance.GetWebLayoutData("TabStates");

        if ( !string.IsNullOrEmpty(data) )
        {
            byte[] dataAsArray = Convert.FromBase64String(data);
            MemoryStream stream = new MemoryStream(dataAsArray);
            _tabStates = serializer.Deserialize(stream) as SerializableDictionary<string, RadTabSetting>;
            }
            DashboardSessionRepository.Instance.SetSession("TabStates", _tabStates);
        }

        return _tabStates;
    }

    public static void SetTabStates(SerializableDictionary<string, RadTabSetting> tabStates)
    {
        DashboardSessionRepository.Instance.SetSession("TabStates", tabStates);
        DashboardDatabaseRepository.Instance.SaveToDatabase("TabStates", tabStates);
    }

我不是在寻找答案,只是好奇我应该学习如何重写这一点。我确信它很简单,只是不确定它叫什么。它只是函数模板吗?

    public static T GetStates<T>() where T: new()
    {
        T _states = new T();//(T)Activator.CreateInstance(typeof(T));

        string stateName = StateDictionary.GetStateName(typeof(T));

        if (!object.Equals(DashboardSessionRepository.Instance.GetSession(stateName), null))
        {
            _states = (T)DashboardSessionRepository.Instance.GetSession(stateName);

            //Work-Around
            System.IO.MemoryStream memoryStream = new System.IO.MemoryStream();
            System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(_states.GetType());
            xmlSerializer.Serialize(memoryStream, _states);
            string data = System.Convert.ToBase64String(memoryStream.ToArray());
            string otherData = DashboardDatabaseRepository.Instance.GetWebLayoutData(stateName);

            if (!string.IsNullOrEmpty(data))
            {
                XmlSerializer serializer = new XmlSerializer(_states.GetType());
                byte[] dataAsArray = Convert.FromBase64String(data);
                MemoryStream stream = new MemoryStream(dataAsArray);

                _states = (T)serializer.Deserialize(stream);
            }
            //Work-Around
        }
        else
        {
            XmlSerializer serializer = new XmlSerializer(_states.GetType());

            string data = DashboardDatabaseRepository.Instance.GetWebLayoutData(stateName);

            if (!string.IsNullOrEmpty(data))
            {
                byte[] dataAsArray = Convert.FromBase64String(data);
                MemoryStream stream = new MemoryStream(dataAsArray);
                _states = (T)serializer.Deserialize(stream);
            }
            DashboardSessionRepository.Instance.SetSession(stateName, _states);
        }

        return _states;
    }

    public static void SetStates<T>(T states) where T: new()
    {
        string stateName = StateDictionary.GetStateName(typeof(T));

        DashboardSessionRepository.Instance.SetSession(stateName, states);
        DashboardDatabaseRepository.Instance.SaveToDatabase(stateName);
    }

static class StateDictionary
{   
    //TODO: Might (should?) be able to redo this polymorphically.
    static IDictionary<Type, string> _stateDictionary = new Dictionary<Type, string>
    {
        {typeof(SerializableDictionary<string, RadTabSetting>), "TabStates"},
        {typeof(SerializableDictionary<string, RadDockContentSetting>), "ContentStates"},
        {typeof(SerializableDictionary<string, RadPaneSetting>), "PaneStates"},
        {typeof(SerializableDictionary<string, RadDockSetting>), "DockStates"},
        {typeof(SerializableDictionary<string, RadDockZoneSetting>), "DockZoneStates"},
        {typeof(SerializableDictionary<string, RadSplitterSetting>), "SplitterStates"},
        {typeof(SerializableDictionary<string, RadSplitBarSetting>), "SplitBarStates"},
        {typeof(KnownGlobalSettings), "GlobalSettings"},
    };

    public static string GetStateName(Type type)
    {
        string stateName = string.Empty;

        if (_stateDictionary.ContainsKey(type))
        {
            stateName = _stateDictionary[type];
        }

        return stateName;
    }
}

1 个答案:

答案 0 :(得分:4)

Martin Fowler's book是重构的标准来源。

但是对于您的具体示例,只需创建一个新方法,该方法接受表示示例中更改的内容的参数。因此,您需要一个字符串参数作为“TabStates”或“ContentStates”值,另一个表示您的字典中Key ContentSetting的值。

这有意义吗?我完全理解你的问题吗?

修改

根据您的评论,您希望使用泛型。这样的事情可以让你前进:

public static Dictionary<string, T> GetTabStates<T>()

请注意,除非添加一些通用约束,否则无法对类型T执行太多操作。

如果要创建T的新实例,则需要

public static Dictionary<string, T> GetTabStates<T>() where T : new() {

如果你想访问T实例的实际属性,那么希望T的所有可能值都会实现某种接口,在这种情况下你会说:

public interface IFoo {
    int Id { get; set; }
}

public static Dictionary<string, T> GetTabStates<T>() where T : new(), IFoo {
   T Tval = new T();
   Tval.Id = 1;
   //etc