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