c# - Use Decorator Pattern for asp.net controls -
i've created custom textboxes inherited textbox. next step want register javascript wrapper.
decorator pattern allow me if can inherit textbox , pass custom textbox constructor parameter.
problem how can use constructor when add control aspx page or how can use decorator pattern asp.net controls.
edit:
simply validation base class (ifield validation interface. can ignored):
public abstract class validationbase : textbox, ifield { private readonly ifield _field; protected validationbase(ifield field) { _field = field; } public int minlength { { return _field.minlength; } set { _field.minlength = value; } } public bool required { { return _field.required; } set { _field.required = value; } } // other porperties etc... protected override void onprerender(eventargs e) { // stuff... base.onprerender(e); } }
and concrete class (emailfield concrete impl. of ifield ignore...):
public class validationemail : validationbase { public validationemail() : base(new emailfield(string.empty)) { } }
and want implement (i've made mind on wordpad can't exact impl.):
public class jsregisterdecorator : validationbase { private readonly validationbase _validationobj; //i not quite sure constructor can handle public jsregisterdecorator(validationbase concretevalidationobj) : base(concretevalidationobj) { _validationobj = concretevalidationobj; } //wrap properties protected override void onprerender(eventargs e) { //register js files... _validationobj.onprerender(e); } }
the problem how can use decorator? because asp.net construct controls automatically:
<vc:validationemail id="validationemail1" runat="server"/>
i don't know can use (where can put constructor parameter?):
<vc:jsregisterdecorator id="validationemailwithjs1" runat="server"/>
i don't think decorator pattern fits here. in general saw more applications of builder , factory method asp.net controls.
to partially solve task can use controlbuilder. give ability change type of control validationbase
jsregisterdecorator
or validationemail
. need decorate validationbase
class controlbuilderattribute
, inherit builder class controlbuilder
, override init
method.
[controlbuilder(typeof(validationbasebuilder))] public abstract class validationbase : textbox, ifield { } public class validationbasebuilder: controlbuilder { public override void init(templateparser parser, controlbuilder parentbuilder, type type, string tagname, string id, system.collections.idictionary attribs) { var newtype = typeof(/*here can put jsregisterdecorator type*/); base.init(parser, parentbuilder, t, tagname, id, attribs); } }
but i'm not sure such approach. controlbuilder cannot give easy control on constructor. surely can override processgeneratedcode in controlbuilder , david ebbo has blog post worth reading not easy task rewrite constructor control , make solution simple.
as alternative work can suggest add abstract (or virtual) method registerscripts
inside validationbase
, call in onprerender
. every control know scripts needs , process of new validator control creation clean , simple. if want separate knowledge of js scripts concrete implementations approach seen in asp.net dynamicdata (read metatable) used.
another thing can see idea close enough dynamiccontrol , maybe possible more ideas asp.net dynamicdata field templates , ifieltemplatefactory
.
Comments
Post a Comment