0

What is the best way to develop my application with the following data scenario?

For simplicity, my model has a base table called Project with the standard common fields, ProjectId, Name, StartDate, Owner etc.

It has a one-to-one relation with other tables that maintain data for different project types, for example, ConstructionProject, FinanceProject, HolidayProject etc. Each of these tables are linked back to the base Project table via their primary key, ProjectId.

Should my classes be based on a base class Project and other classes inherit from this? Such as :

public class Project
{
    public int ProjectId { get; set; }
    public string Name { get; set; }
    public DateTime StartDate { get; set; }
    public string Owner { get; set; }
}

public class ConstructionProject : Project
{
    public string Location { get; set; }
    public string Contractor { get; set; }
}

Or should I have the other types of projects as their own class with a Project object? For example:

public class ConstructionProject
{
    public Project Project { get; set; }
    public string Location { get; set; }
    public string Contractor { get; set; }
}

As a standard MVC project then I can have the controllers handle the crud operations for the second scenario above as such:

public ActionResult Update(Project project)
{
    // update project object
}

public ActionResult Update(ConstructionProject constructionProject)
{
    // update constructionProject object
    // update project object
}

Not sure how the controllers would look handling inherited objects in the first scenario?

The project is a standard C# MVC project using Entity Framework. There could potentially be several linked tables for the different project types. So which option is the best practice and how would the controllers be modelled (if different to the way I propose?) Or is there a better way?

Thanks

Edit - just to add some more info - the base class can also exist on it's own rather than be just a base class, in other words, the Project table can have records that do not have related records in the other tables, if that helps?

DeclanMcD
  • 1,518
  • 4
  • 22
  • 41
  • use something like the first option - prevents duplication of effort - allows easy extensibility. – user1666620 Dec 04 '14 at 14:30
  • From a database point of view I would choose the second option – Marthijn Dec 04 '14 at 14:32
  • See also: [How to decide whether use IS A or HAS A Relation](http://stackoverflow.com/questions/6395366/how-to-decide-whether-use-is-a-or-has-a-relation) – Corak Dec 04 '14 at 14:37
  • Or this one: http://stackoverflow.com/questions/263355/is-a-vs-has-a-which-one-is-better – helb Dec 04 '14 at 14:43

1 Answers1

2

It's a common question and it has a common answer. Inheritance or composition. Well answer is : if A is B then A inherits B, else A uses B. For example, cat is animal but table has an array of four legs and one board

In your case it seems to be a first option.

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • I prefer "aggregation" over "referencing" / "using" because it is more precise. Uses is so universal. I also heard of the "IS A" vs. "HAS A" comparison. – helb Dec 04 '14 at 14:38
  • @helb i prefer `usage`, so I edited my answer respectively – Alex Zhukovskiy Dec 04 '14 at 14:39
  • What? A `table` *has* four legs and might *represent* that via an `array`. – Corak Dec 04 '14 at 14:40
  • @Corak yes, table _has_ legs but isn't. So it uses them in its construction. _Array_ word can be safely removed from this sentence – Alex Zhukovskiy Dec 04 '14 at 14:42
  • 1
    @AlexJoukovsky - yes, but usually when taking about inheritance/composition the more natural way to describe those relations is "is a" and "has a". -- so "cat *is a* animal" makes sense as well as "table *has a* (number of) legs". -- "table *uses* legs" just sounds strange. – Corak Dec 04 '14 at 14:45
  • @Corak well, I agree with you, edited – Alex Zhukovskiy Dec 04 '14 at 14:52
  • I agree, but the difference is that in this case the base class can also exists on it's own. Which is different to the cat/animal scenario – DeclanMcD Dec 04 '14 at 15:55