2

In my MVC app, several view models are going to pretty much identical. Rather than replicate the model each time, I'm thinking I could just create a class instead. What I'm not sure about then is how to include that class in each model.

For example, let's say one my models would look like this:

public class AccountProfileViewModel
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
    public AccountProfileViewModel() { }
}

But I know that FirstName and LastName are going to be used extensively across many models. So, I create a class library with AccountProfile in it:

namespace foobar.classes
{
    public class AccountProfile
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }
}

Back in the model, how would I include the class, so that FirstName and LastName are in the model, but not created specifically?

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193

4 Answers4

6

Create a Base class and then using inheritance, you have access to those common properties.

public class AccountProfile
    {
        public string FirstName { get; set; }
        public string Lastname { get; set; }
    }

public class OtherClass : AccountProfile 
    {
        //here you have access to FirstName and Lastname by inheritance
        public string Property1 { get; set; }
        public string Property2 { get; set; }
    }
JOBG
  • 4,544
  • 4
  • 26
  • 47
3

Besides using inheritance, you can accomplish the same goal, using composition.

See Prefer composition over inheritance

It would be something like:

public class AccountProfile
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
}

public class AccountProfileViewModel
{
    // Creates a new instance for convenience
    public AnotherViewModel() { Profile = new AccountProfile(); }

    public AccountProfile Profile { get; set; }
}

public class AnotherViewModel
{
    public AccountProfile Profile { get; set; }

    public string Property1 { get; set; }
    public string Property2 { get; set; }
}
Community
  • 1
  • 1
RMalke
  • 4,048
  • 29
  • 42
  • Ok, ok. This is more what I had in mind. I'll read the thread to which you linked. Lot's to digest there. – Casey Crookston Jul 08 '15 at 17:24
  • Question: In your third example above, how would I then access FirstName? Profile.FirstName isn't working for me. – Casey Crookston Jul 08 '15 at 17:26
  • 1
    adding constructors to initialize the properties might save you some headaches `public AnotherViewModel() { Profile = new AccountProfile(); }` – JamieD77 Jul 08 '15 at 17:34
  • @CaseyCrookston It may be the case as JamieD77 pointed out, the Profile reference probably was null ? – RMalke Jul 08 '15 at 17:39
2

You could also implement an interface like IProfileInfo, this may be preferable as classes can implement multiple interfaces but only inherit from one class. In the future you may wish to add in some other uniform aspect to the code that requires it to be inherited but you may not necessarily want some classes that inherit from the base class having Firstname and Lastname properties. If you're using visual studio it will implement interfaces for you automatically so there no real extra effort involved.

public class AccountProfile : IProfileInfo
{
    public string FirstName { get; set; }
    public string Lastname { get; set; }
}

public interface IProfileInfo 
{
    string Firstname {get;set;}
    string Lastname {get;set;}
}
Luke Baughan
  • 4,658
  • 3
  • 31
  • 54
  • 1
    Wow, okay. Lots of great answers. And lots to learn. I'm off to learn more about the differences between a class and an interface. Much appreciated. – Casey Crookston Jul 08 '15 at 17:32
  • This is a great answer to the Interface vs Class question http://stackoverflow.com/a/1913185/1370442 – Luke Baughan Jul 08 '15 at 17:53
1

This was too long to stick in a comment so this is just a comment based off the answers you've already received.

You should only use inheritance if the new class you're creating is basically the same type of object just slightly different. The property method should be used if you're trying associate 2 separate classes together. an inheritance would be something like

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DOB { get; set; }
}

public class Teacher : Person 
{
    public string RoomNumber { get; set; }
    public DateTime HireDate { get; set; }
}

public class Student : Person
{
    public string HomeRoomNumber { get; set; }
    public string LockerNumber { get; set; }
}

Composition should be used like this.

public class Address 
{
    public string Address1 { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public string Zip { get; set; }
}

public class StudentViewModel
{
    public StudentViewModel ()
    {
        Student = new Student();
        Address = new Address();
    }
    public Student Student { get; set; }
    public Address Address { get; set; }

}
JamieD77
  • 13,796
  • 1
  • 17
  • 27