Pregunta

I am creating a calendar application. I have a Appointment table, and a Person table. The 2 are linked by PersonID field in each table.

My question is, should my underlying .Net Appointment object contain a property for the PersonName, and I populate the object from a database view (or stored procedure that joins the tables) or is it more correct to have the Appointment class contain a People class? If the answer is the latter, what is the best way of populating the Appointment/Person object from the database?

¿Fue útil?

Solución

Supposing you're not using an ORM, you may take a look at the DAO pattern: http://en.wikipedia.org/wiki/Data_access_object

I would create two DTOs:

class Person
{
    public int id { get; set; }
    public String name { get; set; }
}

class Appointment
{
    public int id { get; set; }
    public Date when { get; set; }
    public Person who { get; set; }
}

And a "full" appointment class:

class FullAppointment
{
     private Person person;
     private List<Appointment> appointment;
}

Then a DTO to get data from the DB:

class AppointmentDTO
{
   public FullAppointment retrieveFromDb(int personId)
   {//...}
}

Otros consejos

I am not sure about reading the database. But the classes could look like:

public class Person
{
    public List<Appointment> Appointments { get; private set; }
    ...

    public Person()
    {
        Appointments = new List<Appointment>();
    }
}

public class Appointment
{
    public Person Person { get; set; } // Only if you need this relation from
    ...
}

And in your model:

Persons = new List<Person>();

You should not duplicate properties. Once enetity/class should only have properties which are valid for that entity.

If you want to call another table then you should have a property which would return another entity by a specific foreign key.

In your case I would have

public class Person
{
    List<Appointment> _appointments;
    public List<Appointment> Appointments
    {
        get
        {
            if (_appointments == null) _appointments = //LOAD APPOINTMENTS FOR THAT PERSON
            return _appointments;
        }
    }
}

Hope that helps.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top