1

I have a handful of email templates and in each template I have a header and footer that all share the same info.

The header and footer are represented by EmailModel.cs

public class EmailModel
{
    public string CompanyName { get { return ConfigurationManager.AppSettings["CompanyName"]; } }
    public string PhoneNumber { get { return ConfigurationManager.AppSettings["PhoneNumber"]; } }
    public string FacebookUrl { get { return ConfigurationManager.AppSettings["FacebookUrl"]; } }
    public string TwitterUrl { get { return ConfigurationManager.AppSettings["TwitterUrl"]; } }
    public string YouTubeUrl { get { return ConfigurationManager.AppSettings["YouTubeUrl"]; } }
    //Additional methods for sending these templates as emails
}

Now for a specific email template I have a view model.NewSignUpEmailViewModel.cs

Should I do this:

public class NewSignUpEmailViewModel : EmailModel
{
    public string FirstName { get; set; }
    public string CompanyName { get; set; }
    public string UserName { get; set; }
    public Guid UserId { get; set; }
}

or this:

public class NewSignUpEmailViewModel
{
    public EmailModel Email {get; set;}
    public string FirstName { get; set; }
    public string CompanyName { get; set; }
    public string UserName { get; set; }
    public Guid UserId { get; set; }
}

I just used email as an example, is there pros/cons to each?

The only con I can see is that in some cases you will run into duplicate property name issue.

The Muffin Man
  • 19,585
  • 30
  • 119
  • 191
  • Yes you already have a CompanyName property collision when inheriting. I personally would prefer composition over inheritance. Do the 2 CompanyName properties represent the same piece of information? – danludwig Jan 12 '12 at 20:46
  • The CompanyName property in the base class represents my company, and the CompanyName in the view model represents a client. I ended up renaming the properties in the EmailModel with a prefix of "Our". – The Muffin Man Jan 12 '12 at 21:02
  • possible duplicate of [ASP.NET MVC Architecture : ViewModel by composition, inheritance or duplication?](http://stackoverflow.com/questions/6954102/asp-net-mvc-architecture-viewmodel-by-composition-inheritance-or-duplication) – nawfal Jan 12 '15 at 20:24
  • 1
    @nawfal This question is 3 years old with an accepted answer and you vote to close it now? Get a life buddy. – The Muffin Man Jan 12 '15 at 20:28
  • I am not sure if SO has such a policy towards duplicates. I always see duplicate questions getting closed regardless of age. Nothing personal, just doing my bit to make this a better place. – nawfal Jan 12 '15 at 20:43

1 Answers1

2

Composition is often preferred over inheritance, but both have their place. One good rule of thumb is to determine if there is an "is-a" or a "has-a" relationship between your objects. If object 1 has object 2 as a component, composition is definitely the way to go.

As an example, let's approach your data model a bit differently:

public class SocialLinks
{
    public string FacebookUrl { get; set; }
    public string TwitterUrl { get; set; }
    public string YouTubeUrl { get; set; }
}

public class User 
{
    public SocialLinks links { get; set; }
    public string Name { get; set; }
    // and so on
}

In this example, it's obvious that a user HAS social web links, as opposed to the user being a specialized version of the SocialLinks class. Hope that helps!

Dan A.
  • 2,914
  • 18
  • 23