views:

346

answers:

3

I'm thinking about how to do this, but I have several different shapes of Data in my Database, Articles, NewsItems, etc.

They All have something in common, they all have IDs (in the DB they're named ArticleID, NewsID etc. )

They all have a Title

They all have BodyText.

They all have a Status

They all have a DateAdded

What I'd like to do is standard class inheritance.

I'd like a Master Class (I don't need to write this to the database) called Content with fields like:

  • ID
  • Title
  • SubTitle
  • BodyText
  • Status
  • AddedDate

I'm not sure how I can do this with the ORM. Why I want this is because then I can pass a list of COntent to my UserControl which is responsible for Rendering it. It will only need the information that is common to all objects.

Is this even possible?

+1  A: 

I'm not sure whether you're talking about LINQ to SQL, but there are a few resources online about how to create inheritance with it:

  1. LINQ To SQL Discriminator Column Example - Inheritance Mapping Tutorial
  2. Inheritance in LINQ to SQL Screencast

...and more.

HTH

Omer van Kloeten
A: 

I found one page that looks like it has something along the lines of what I'm looking for (The last post)... but I'm not sure about using weakly typed objects:

LINQ inheritance

Atømix
+2  A: 

This is what Interfaces are for. Have each class implement an IContent interface that contains your Title, BodyText, Status and DateAdded properties. Now you can pass around a collection ( List<IContent> ) around that could containt different types of content.

If you're using LinqToSql you can create partial class files to have the autogenerated classes implement the interface you want.

public partial class SomeContent : IContent
Vyrotek