Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generics Question 1

Status
Not open for further replies.

Smeat

Programmer
Mar 27, 2004
193
GB
Hi

I would like to create an abstract base class that inherits from a generic type so that my subclasses can dictate what type to inherit from.

For example:

Code:
/// <summary>
/// Provides common functions.
/// </summary>
abstract class CommonBase<T>: T
{
	protected CommonBase()
	{

	}
}

Doing this would allow me to do things such as the following:

Code:
public partial class StandardMaster1 : CommonBase<System.Web.UI.MasterPage>
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

and

Code:
public partial class Login: CommonBase<System.Web.UI.Page>
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Doing this would allow me to get around the single inheritance restriction and prevent me having to duplicate code in my web applications.

Although the examples above are web application related, the question is a C# question so I reckon this is the correct forum to post my question.

Is this possible?

TIA

Smeat
 
yes, it's possible. this is one way developer implement DAO objects. usually you would restrict the generic type to contain common functionalty.

for example with an ORM like NHibernate is common for entities to inherit an abstract object [tt]abstract class DomainObject<TIdentity>[/tt] where identity is the type of ID. the base DAO would then be defined by
Code:
abstract class DAO<TEntity, TIdentity> where TEntity : DomainObject<TIdentity>
{
  public abstract void Save(TEntity entity);
  public abstract void Delete(TEntity entity);
  public abstract void GetBy(TIdentity id);
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I figured I could do the following:

Common Base
Code:
public abstract class CommonBase<T> where T : Control 
{
	protected CommonBase()
	{

	}
}

PageBase
Code:
public class PageBase : CommonBase<System.Web.UI.Page>
{
	public PageBase()
	{

	}
}

MaterPageBase
Code:
public class MasterPageBase : CommonBase<System.Web.UI.MasterPage>
{
    public MasterPageBase()
	{

	}
}

My master pages then inherit like this:
Code:
public partial class Master1 : MasterPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (MasterPage pg = this)
        {
            pg.Response.Write("<p/>Master1 Page Load Method<p/>");
        }
    }
}

and my web pages then inherit like this:
Code:
public partial class _Default : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        using (Page pg = this)
        {
            pg.Response.Write("<p/>Default Page Load Method<p/>");
        }
    }
    
}

but this produces the following runtime error:

which does compile but throws the following runtime error:

<
Code:
The compiler failed with error code 1.


I'm wondering if this is even possible.

Smeat
 
Hi jmeckley

Thanks for reply, I submitted my second post before I saw your it.

Code:
abstract class DAO<TEntity, TIdentity> where TEntity : DomainObject<TIdentity>

I think this example is saying the generic type TEntity must inherit from type DomainObject which in turn must be passed the TIdentity type.

If this is the case I don't think it will work in this instance as the only common types I have for System.Web.UI.MasterPage and System.Web.UI.Page are Control or object. This would not allow me to then run standard events such as Page_Load

Or am I missing the point

Smeat
 
why are you writing raw html in the code behind? this can all be done without generic base objects, master pages and the like.

you also would never do
Code:
using(Page p = this)
{
   do something with p
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Hi again

Yes, I am writing html in the code behind beause I am just trying to output some sample text from different events whilst I prove this will work (assuming I ever do).

I also only have
Code:
using(Page p = this)
{
   do something with p
}

because I pasted code I was playing with to try get around the problem I mentioned above with having only control or object as a common type between master pages and web pages.

Smeat
 
what are you trying to do specifically, becase I think you can do this with basic inheritance without the generics.
Code:
public class MyWebForm : BasePage
{
}

public abstract BasePage : Page 
{
  //common functionality to all pages
}
the master page shouldn't need any of this. master page is more for formatting and standard controls (ascx/content)

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
My problem is that we often have common functions required in both master pages and web pages but cannot inherit from a single common base file as each requires a different base type.

Smeat
 
then I would use a strategy instead of inheritance, or simpilfied inheritance.
Code:
public class Form1 : BasePage
{
}

public abstract BasePage : Page
{
   public BasePage()
   {
      LoadingStragetyFactory.Implement(this);
   }

   protected ILoadingStragety Loader
   {
      get; set;
   }
}

public class LoadingStragetyFactory
{
   pirvate IDictionary<Type, ILoadingStragety> strageties = null;

   public static void Implement(BasePage page)
   {
      LoadLoaders();
      Type t = page.GetType();
      if(!strageties.ContainsKey(t))
          throw new MissingLoadingStagetyExpection(t);
      page.Loader = strageties[t];
   }

   private static void LoadLoaders()
   {
      if (strageties == null)
      {
          strageties = new Dictionary<Type, ILoadStragety>();
          strageties[typeof(Form1)] = new LoaderOne();
          strageties[typeof(Form2)] = new LoaderTwo();
          ...
      }
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I don't think your solution will work in this scenario as it requires my base page inherits from the System.Web.UI.Page class which will prevent me from subclassing this base from my master pages.

I do appreciate you taking the time to post sample code though so the least I can do is throw a star your way.

Smeat
 
Do the pages need a common call interface, or a common set of shared functionality?

Because if it's the first, just create a C# interface.

If it's the latter, then abstract the shared functions out to a shared assembly.

Chip H.


____________________________________________________________________
www.chipholland.com
 
Thanks chiph

It is the shared functonallity I need and yes, I think the shared assembly is the way im going to have to go, shame I couldn't get my generics to work though.

Thanks both for your input.

Smeat
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top