John
John

Reputation: 711

How to Share Interface Implementation

I am facing a situation in which both the codebehind file of an ASPX master page and that of a regular ASPX page that does not use the master implement an interfance. The implementation is exactly the same.

Is it possible to make the two codebehinds share the implemenation instead of each has its copy of the same implementation? If yes, how should I approach this?

Thank you in advance for any insight.

John

Upvotes: 0

Views: 117

Answers (1)

Icarus
Icarus

Reputation: 63964

How about using composition, on which both, the Master and the ASPX page have a reference to a class that implements the interface?

public interface IFace
{
    int MyProperty { get; set; }
    void MyMethod(string pVariable);
}

[Serializable]
public class ClassA:IFace
{
    public ClassA()
    {

    }

    #region IFace Members

    public int MyProperty
    {
        get
        {
            throw new NotImplementedException();
        }
        set
        {
            throw new NotImplementedException();
        }
    }

    public void MyMethod(string pVariable)
    {
        throw new NotImplementedException();
    }

    #endregion
}

public partial class MasterPage : System.Web.UI.MasterPage
{
    private ClassA IntefaceImplementor = new ClassA();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

public partial class _Default : System.Web.UI.Page
{

    private ClassA InterfaceImplementor = new ClassA();
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Upvotes: 2

Related Questions