2

I am trying to create a social networking application for students and teachers. I am currently designing the user accounts module

I have thought of these two approaches

Approach 1

Public class Account
{
    public string AccountId{get;set;}
    public string AccountName{get;set;}
       .
       .
       .


}

public class Student : Account
{
    public Course[] Courses {get; set;}
    //other student properties
        .
        .
        .

}

public class Teacher : Account
{
     //other teacher properties

}

Approach 2

Public class Account
{
    public string AccountId{get;set;}
    public string AccountName{get;set;}
    public AccountType AccountType {get;set;}  //AccontType is enum having student and teacher
       .
       .
       .


}


public class User
{
    public string UserId { get;set;}
    public string UserName {get;set;}
    public Account Account {get;set;}
}


public class Student : User
{
    public Course[] Courses {get; set;}
    //other student properties
        .
        .
        .

}


public class Teacher : User
{
     //other teacher properties

}

Is there any better approach than these approaches?

taher chhabrawala
  • 4,110
  • 4
  • 35
  • 51

2 Answers2

2

Approach 2 is much clearer and preferred in my opinion.

What it comes down to is does a User have an account or is a User an account? For me its the former and you should use composition instead of inheritance in this case as you do in approach 2.

BrokenGlass
  • 158,293
  • 28
  • 286
  • 335
1

I like approach 2 better because it's more closer to the real world scenario. Students and Teachers are users and each of them have an account with the desired properties set.

Kakira
  • 846
  • 1
  • 8
  • 14