如何在系统中设计两种类型的账户
我正在尝试为学生和教师创建一个社交网络应用程序。 我目前正在设计用户帐户模块
我想到了这两种方法
方法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
}
方法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
}
有没有比这些方法更好的方法?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为方法 2 更加清晰,也是首选。
归根结底是
用户
是否拥有帐户或者是用户
帐户?对我来说是前者,在这种情况下您应该使用组合而不是继承,就像在方法 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 aUser
an account? For me its the former and you should use composition instead of inheritance in this case as you do in approach 2.我更喜欢方法 2,因为它更接近现实世界的场景。学生和教师都是用户,他们每个人都拥有一个帐户并设置了所需的属性。
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.