Im simply trying to mock up a data model in POCO. But I’m a little confused about something. Lets just assume for this example that I will be using Entity Framework for ORM. First I have a Phone and Address class:
public class Phone
{
int Id { get; set; }
int Number { get; set; }
}
public class Address
{
public int Id { get; set; }
public string Street { get; set; }
public string Suite { get; set; }
public string City { get; set; }
public string State { get; set; }
public int Zip { get; set; }
}
Next I have a Person and a Company class:
public class Person
{
int Id { get; set; }
string FirstName { get; set; }
string MiddleName { get; set; }
string LastName { get; set; }
string Prefix { get; set; }
}
public class Company
{
int Id { get; set; }
int Name { get; set; }
}
Say I want both Person and company to have Phone or address lists so logically the way I might think about this from a code perspective would to create an abstract class, AContact:
public abstract class AContact
{
ICollection<Phone> Phone { get; set; }
ICollection<Address> Address { get; set; }
}
And make my Company and Person then inherit this:
public class Company : AContact
{
int Id { get; set; }
int Name { get; set; }
}
public class Person : AContact
{
int Id { get; set; }
string FirstName { get; set; }
string MiddleName { get; set; }
string LastName { get; set; }
string Prefix { get; set; }
}
Alternatively I might create an Interface:
public interface IContact
{
ICollection<Phone> Phone { get; set; }
ICollection<Address> Address { get; set; }
}
And Implement the interface:
public class Company : IContact
{
public int Id { get; set; }
public int Name { get; set; }
public ICollection<Phone> Phone { get; set;}
public ICollection<Address> Address {get; set;}
}
Now I understand the difference here between abstract classes and interfaces. But how is this supposed to be implemented for Entity Framework to function? From a little searching around on the web it seems that as of 4.1 you can not do the Interface thing, so does that mean I have to use the abstract class? OR do I just have to explicitly define both properties in every class I want to use them in? Also what about something like RavenDB would this work there as well?
Both options are possible.
When using your interface your DbContext will look like:
You will get back Companies and People objects that also happen to implement the interface you specify.
When using the abstract class you will have the following:
With the abstract base class, you will have one entity set. With the
OfType<T>you can filter the collection down to specific times.RavenDB is a whole other subject. It’s a NoSql database that works really smooth. It serializes your objects to JSON and just stores them for you. You can use (almost) every class structure you want.