Database foreign key model in C #

I am creating a calendar application. I have a meeting table and a Person table. 2 are linked by the PersonID personal field in each table.

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

+4
source share
3 answers

If you are not using ORM, you can take a look at the DAO template: 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 the "full" destination class:

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

Then DTO to get data from the database:

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

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

 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>(); 
+1
source

You cannot duplicate properties. After enetity / class should only have properties that are valid for this object.

If you want to call another table, then you must have a property that returns another object with a special 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 this helps.

+1
source

Source: https://habr.com/ru/post/1395411/


All Articles