开发者

I've got a helper class. Should I be considering removing it?

I am using Telerik's Rad Controls for AJAX. I have created my own controls; their classes inherit from the RadControl classes. The closest common parent class for all of the Telerik controls I am inheriting from is 'WebControl.' The controls I am discussing all implement ICormantControl which doesn't have much to it:

public interface ICormantControl<T>
{
    T GetSettings();
}

Now, in my helper class I have a handful of methods. I'd like to start with one that I believe I have the best chance of doing something with.

    /// <summary>
    /// Saves a control -- writing it to Session and the DB. 
    /// </summary>
    /// <typeparam name="T"></typeparam>
    /// <param name="control"></param>
    public static void Save<T>(ICormantControl<T> control)
    {
        T settings = control.GetSettings();
        string controlID = ((ISetting)settings).ID;

        SerializableDictionary<string, T> states = GetStates<SerializableDictionary<string, T>>();

        bool isKnown = states.ContainsKey(controlID);

        if (isKnown)
        {
            Logger.DebugFormat("{0} is known. Overwriting data.", controlID);
            states[controlID] = settings;
        }
        else
        {
            Logger.DebugFormat("{0} is unknown. Saving data.", controlID);
            states.Add(controlID, settings);
        }

        SetStates<SerializableDictio开发者_Go百科nary<string, T>>(states);
    }

Currently, to save the state of a control, I call StateManager.Save<RadDockSetting>(dock), for example.

I believe a better implementation would result in me being able to say dock.Save().

As I see it I am pinned between a rock and a hardplace. I am under the impression that if I move the declaration of Save to my ICormantControl interface then I will have to move the implementation of Save to each of the controls which implement ICormantControl. This would result in a large amount of duplicated code.

Should I be leaving my implementation in my helper class, moving the declaration to ICormantControl, and then, in each class which implements ICormantControl, I redirect the implementation to my helper method?

As far as I can tell I am not able to have all of my controls inherit from an intermediary class. The common parent is too far up the hierarchy of inherited parent controls. It may be possible to extend WebControl and have it implement Save, but that seems orders of magnitude worse than my above suggestion.

Do I have other options?


What you want is an extension method. Change your declaration to this:

public static void Save<T>(this ICormantControl<T> control)

Then you'll be able to do dock.Save() without needing to modify the ICormantControl interface or any of the other classes.


The extension methods would solve your problem. Example of how a extension method is written.

Pass HtmlHelper instance to another method MVC3 with Razor

 public static void Save<T>(this  ICormantControl<T> control)
 { 
 }

The magic is in this keyword so now you can use doc.Save() where this points to doc.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜