Casey Crookston
Casey Crookston

Reputation: 13945

.NET MVC -- Use a class as the model?

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?

Upvotes: 2

Views: 1771

Answers (4)

JamieD77
JamieD77

Reputation: 13949

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; }

}

Upvotes: 1

RMalke
RMalke

Reputation: 4094

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; }
}

Upvotes: 3

Luke Baughan
Luke Baughan

Reputation: 4686

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;}
}

Upvotes: 2

JOBG
JOBG

Reputation: 4624

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; }
    }

Upvotes: 6

Related Questions