I have two interfaces:
interface IDynamicControl { string Id { get; set; } string Label { get; set; } string Value { get; set; } } interface IDynamicList : IDynamicControl { IList ListItems { get; set; } }
I have a ControlResolver class that returns IDynamicControl - for my purposes, I have wrapper classes for ASP.NET, CheckBoxes, and DropDownLists text fields that implement IDynamicControl (DropDown implements IDynamicList). The bottom line is that I can give resolver the name of the control, for example "Textbox", and it will return to me IDynamicControl, which is the changed ASP.NET text field.
This works fine, but the problem is with DropDownList. Maybe I have a brain fart, but the problem I am facing is that when the control is a drop-down list, I need to explicitly cast to IDynamicList (since resolver returns IDynamicControl), so I can add elements to this to display. This is usually not a problem, but the goal of dynamic control is that I can store the type of the field from the outside and read it, so I would need to do something ugly, like:
string controlType = SomeService.GetControlType(); if (controlType == "dropdown") { var control = (IDynamicList)ControlResolver.ResolveControl(controlType);
but it seems pretty ugly. I could include the ListItems property in the database and just throw NotImplemented into classes that don't use it, but this violates the ISP and is even nicer than the if statement. In short, I would like my resolver to return one type of control, so I don't need to have different Resolve () methods, but there is additional work in the consumption code that I have to do if and only if the control is a drop-down menu.
If I'm wrong or I forgot something basic, is there a better solution for this, or should I just use an if statement? I canβt use the base class because all my βdynamicβ classes inherit from the base classes of the ASP.NET user interface.
source share