C # objects, interfaces and database
When working with objects and interfaces, what is the best way to write to the database? There are many opinions regarding the design of the object, but I don't understand about the end of the database. Simple example:
Suppose the base Contact class contains common fields such as contact name (Bill, Fred, Sally) and location (home, work, etc.). Add an IPhone interface (area code, phone number, extension) and an IEmail interface (email address, cc) to abstract the differences. Then create classes (Phone, Email) that inherit from them like so:
Phone: Contact, IPhone Email: Contact, IEMail
An alternative would be to create an IContact interface instead of the base Contact class like this:
Phone: IContact, IPhone Email: IContact, IEMail
Except for NHibernate or Entity Framework implementation, what is the best fit for data access code when these entities are written to the same database table? What I've seen seems pretty awkward.
source to share
In my experience, when it comes to databases, good relational design should come first if you want to have any semblance of performance. After that my model is pretty simple: one table, one class. If I have a self-contained referencing relationship, I create another class from the original. Connections are defined as lists of objects (for example, an order has a list of order details).
Once I have that, I use a DAL capable of reflecting these classes and generating the required SQL on the fly; after profiling I may have to create some SP, but its results are sent to the receiving DTO as well.
I have had no reason to use interfaces until now.
source to share
I think you need to consider the relationship between these three objects. Say the following statements out loud and see which is true for your application:
-
Phone
IS AContact
-
Phone
HAS AContact
Just from what you posted here, it looks like you can significantly reduce the complexity of your application by going with the second option.
You are curious about data access, however, and you mentioned that there is one database table that makes me wonder why you need separate objects for Contact
, Phone
and Email
. I would recommend that you either refactor your code to include a single object to represent your data model, as this will reduce the complexity of your codebase or refactor your database schema to reflect what you really want (based on your sample code) ...
source to share
Create your database first to make sure you can actually store whatever you need. Then create general queries such as "All customers and their main phone number" and "One customer with all phone numbers".
It will become apparent as if you didn't have enough phone numbers or there was no way for him to provide a real phone number.
Your database will be at least three times as long as your application and is difficult to modify, so make sure you get it right from the start.
source to share
It looks like you are trying to simulate phone contacts and email contacts, but I think you actually have contacts with the main contact method: phone or email. The way I would structure this would be with composition - if you want the flexibility to have multiple phone numbers / emails. If you can live one at a time, you can save it directly to the contact table. If you need multiple addresses, treat it the same way as phone / email, with a separate table.
Contacts Table ContactID FirstName LastName MI StreetAddress1 StreetAddress2 City StateProvince PostalCode PreferredContactMethod (0 = Phone, 1 = Email) ... more details ... PhoneNumbers Table PhoneID ContactID PhoneNumber IsPrimary ... more details ... EmailAddresses Table EmailID ContactID EmailAddress IsPrimary ... more details ...
For your classes, then you will have a Contact class that contains one or more PhoneNumbers and one or more EmailAddresses. If you had other things besides contacts with phone numbers or email addresses, then it would make sense to have interfaces indicating that they are IPhoneable or IEmailable - this means you can add / remove phone numbers or addresses from them Email
public interface IPhoneable
{
public PhoneNumber GetPrimaryNumber();
public void AddNumber( PhoneNUmber number );
public IEnumerable<PhoneNumber> GetNumbers();
}
public interface IEmailable
{
public EmailAddress GetPrimaryAddress();
public void AddEmailAddress( EmailAddress address );
public IEnumerable<EmailAddress> GetEmailAddresses();
}
public class Contact : IPhoneable, IEmailable
{
private List<PhoneNumber> phoneNumbers;
private List<EmailAddresses> emailAddresses;
public int ContactID { get; private set; }
public string FirstName { get; set; }
...
public Contact()
{
this.phoneNumbers = new List<PhoneNumber>();
this.emailAddresses = new List<EmailAddress>();
}
public PhoneNumber GetPrimaryNumber()
{
foreach (PhoneNumber number in this.phoneNumbers)
{
if (number.IsPrimary)
{
return number;
}
}
return null;
}
...
}
If you want, you can have PhoneableContact and EmailableContact which will be subclassed from Contact and differentiated based on the preferred contact method, but I really don't see the need for that. If you do this, Contact will not implement any interface - subclasses will implement correspondingly.
source to share
In this case ... I think the best option is to use a database object like DB4O.
"Db4o is an open source object database that allows Java and .NET developers to store and retrieve any application object, including Theme Maps, with just one line of code, eliminating the need to predefine or maintain a separate rigid data model."
source to share