The same class implements, only another parent, how to handle this?

I have two classes that look like this:

public class RoleButton: Button
{
   protected bool PrimaryRole;
    protected bool SecondaryRole;

    private string _role;
    private AuthenticatedPage CurrentPage
    {
        get { return (AuthenticatedPage)Page; }
    }

    protected UserInfo CurrentUser
    {
        get { return CurrentPage.CurrentUser; }
    }

    protected void SetRole(string role)
    {
        _role = role;
    }

    protected override void OnPreRender(EventArgs e)
    {
        base.OnInit(e);

        if (CurrentUser.IsSuperAdmin) return;
        DataTable dt = CommandController.GetButtonRoleForUserByPortalID(CurrentUser.UserID,
                                                                        ConvertUtility.ToInt32(AppEnv.PortalId()));

        if (dt.Rows.Count > 0)
        {
            if (dt.Rows.Count > 1)
            {
                PrimaryRole = ConvertUtility.ToBoolean(dt.Rows[0][_role]);
                SecondaryRole = ConvertUtility.ToBoolean(dt.Rows[1][_role]);

                if (!PrimaryRole && !SecondaryRole)
                {
                    Visible = false;
                }
                else
                {
                    Visible = true;
                }
            }
            else
            {
                PrimaryRole = ConvertUtility.ToBoolean(dt.Rows[0][_role]);
                Visible = PrimaryRole;
            }
        }
        else
        {
            Visible = false;
        }     
}


public class RoleImageButton: ImageButton
{
   ///
   ///same as above
   ///
}

The implementation of these classes is exactly the same, so I want to remove this duplication, but I do not know how to do it.

Can you show me a way to do this?

+5
source share
4 answers

The disconnect point is the need to access private variables of any of these classes or properties that are specific to them.

If you want to encapsulate and share behavior, you use the Command Pattern. It looks something like this:

public interface ICommand {
    void ExecuteOnPreRender(WebControl control, EventArgs args);
}

//  This class encapsulates the functionality common
//  to both OnPreRender commands
public class SharedPreRenderCommand : ICommand {
    public void ExecuteOnPreRender(WebControl control, EventArgs args) {
        //  Modify the size, border, etc... any property that is 
        //  common to the controls in question
    }
}

public class RoleImageButton : ImageButton {

    private ICommand onPreRenderCommand = null;

    public void SetPreRenderCommand (ICommand command) {
        onPreRenderCommand = command;
    }

    protected override void OnPreRender(EventArgs args) {
        if (null != onPreRenderCommand) {
            onPreRenderCommand.ExecuteOnPreRender(this, args);
        }
        else {
            base.OnPreRender(args);
        }
    }
}


public class RoleButton : Button {

    private ICommand onPreRenderCommand = null;

    public void SetPreRenderCommand (ICommand command) {
        onPreRenderCommand = command;
    }

    protected override void OnPreRender(EventArgs args) {
        if (null != onPreRenderCommand) {
            onPreRenderCommand.ExecuteOnPreRender(this, args);
        }
        else {
            base.OnPreRender(args);
        }
    }
}

... , , , RoleButton .., . RoleButton/RoleImageButton- ; , - :)

/ , , , RoleButton RoleImageButton ... . , , , , .

, , - , , # .




... , , , :

//  Encapsulate these fields if you want to be PC
public class Roles {
    public bool PrimaryRole;
    public bool SecondaryRole;
}

public class RoleButton: Button {
    protected Roles buttonRoles;    
    ...
}

public class SharedPreRenderCommand : ICommand {

    public void ExecuteOnPreRender(WebControl control, Roles roles, EventArgs args) {
        //  Modify the Roles class, which the RoleButton or 
        //  RoleImageButton has a handle to
    }

}

Roles . , . -, , -.

... , , , . : D

HTH,

+3

BaseButton, Button. - , BaseButton :

public class BaseButton : Button
{
   ///
   protected override void OnPreRender(EventArgs e)
   {
      // Common base implementationi
   }         
}

public class ImageButton: BaseButton
{
   // Specific implementation
   public ImageButton()
   {
      this.Name = "ImageButton";
   }
}

public class RoleButton: BaseButton
{
   // Specific implementation
   public RoleButton()
   {
      this.Name = "RoleButton";
   }
}
+1

RoleButton, ?

Button ImageButton , IButton, - :

class RoleButton : IButton
{
  private Button _realButton;

  public RoleButton(Button realButton) { _realButton = realButton; }

  // IButton implementation delegates non-role stuff to _realButton
}

, RoleButton:

var roleButton = new RoleButton(myButton);
var roleImageButton = new RoleButton(myImageButton);
+1
source

You can use the T4 template . This will allow you to have one file (template) containing the implementation once, and it will generate two classes with identical implementations for you. (Of course, the code will still be duplicated in the compiled assembly, but at least this is not a problem related to maintaining synchronization.)

0
source

All Articles