1

I'm mapping different tables on C# classes. i.e. If I've a table named "T" with columns "A" and "B", in C# I create something like that in my code:

class T{
  A{get;set;}
  B{get;set;}
}

My problem is with joined tables, what is a correct way to represent two joined tables in 1-1, 1-n, n-n relations?

I'm not using Entity Framework and I cannot use it.

Andrea Cattaneo
  • 568
  • 1
  • 6
  • 18
  • https://stackoverflow.com/questions/37324/what-is-the-syntax-for-an-inner-join-in-linq-to-sql – Barnstokkr Apr 10 '18 at 06:28
  • I'm not using entity framework. I'm talking about how to model classes. – Andrea Cattaneo Apr 10 '18 at 06:36
  • How about something like this: 1-1: one class `X` with a property of type `Y` (and *maybe* a property of type `X` in class `Y`). -- 1-n: "parent" class `X` with property of type `IEnumerable` (or similar to `IEnumerable`; and *maybe* a property of type `X` in "child" class `Y` for reference to "parent"). -- n-n: class `X` with property of type `IEnumerable` and class `Y` with property of type `IEnumerable` (and *maybe* another one each for backreferences, to model relations like "uses" and "is used by"). – Corak Apr 10 '18 at 06:55

1 Answers1

3

I'll try to show you with some examples. First assume we have a school and students, let's say that the school has many students and a student is in one school Then you will have something like that in your class, this will be for the 1-n

public class School
{
    private string name;
    private List<Student> students; // which is the list of the student
}

public class Student
{
   private string name;
   private School school; //which represent the school of the student
}

Now let's assume that a student has class, he can have more than one, so it's a n-n relation:

public class Student
{
   private string name;
   private School school; //which represent the school of the student
   private List<Class> classes; //Represent all the class he attends
}

public class Class
{
   private string name;
   private List<Student> students; //Represent all the students attending the class
}

And basically for the 1-1 you don't represent it, because in database when you have 1-1 the relation is useless and you merge both table to make just one.

Hope it helps

Edit: Sometimes it seems to be useful to have a 1-1 relation in your class. So let's take the example of a car and engine.

public class Car
{
    private string name;
    private Engine engine; // the engine of the car
}

public class Engine
{
   private string name;
   private string power;
   private Car car; // the car of the engine
}

In that case, it would be useful for the clarity of the code. Thanks to Corak

Lucas Tambarin
  • 395
  • 3
  • 14
  • Good. Although I *could* see meaningful 1-1 relations you'd want to keep as separate classes and tables. For example something like `car` and `engine` where you probably wouldn't want to have all the engine properties be part of the car structure. – Corak Apr 10 '18 at 07:31
  • Yeah in certain condition it could be useful, I'll had it to the answer with your exemple :) – Lucas Tambarin Apr 10 '18 at 08:06
  • I think that this can be a good solution.. my only concern is about getting the data. If I have a large db with a lot of relations between tables it becomes difficult to pre-populate all the linked objects. What's your approach in that cases? You take the data when accessing to them? Anyway I think that this solution is the best I can imagine.. – Andrea Cattaneo Apr 10 '18 at 08:46
  • 1
    You should create a class that will load the data from your database and then first you populate the class without taking care of the relation and once you've populate all your class, you can add your relation. I think that's how i would do it – Lucas Tambarin Apr 10 '18 at 09:21