是否正确使用枚举
我正在尝试设置一些数据库表并为教师、学生、课程、学期等主题创建一些模型类。这是针对 MVC3 Web 应用程序的。如果我有一个用户配置文件实体和模型类。如果我在用户配置文件模型类中创建一个分类枚举并且该枚举是教师或学生,它会起作用吗?我不知道这是否可行。我真的不知道如何才能做到这一点,因为我需要学生注册的课程,而对于老师来说,我需要他/她正在教授和教授的课程。任何建议将不胜感激。
例如,在 userprofile 类中可能是这样的:
class UserProfile {
public Guid UserID { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public Classification classification { get; set; }
}
public enum Classification {
Student, Teacher
}
I'm trying to set up some database tables and create some model classes for the subject of Teacher, Student, Courses, Semester. This is for a MVC3 Web App. If I have a userprofile entity and model class. Would it work if I created a classification enum in the userprofile modelclass and that enum would be either Teacher or Student. Idk If this could work. I don't really know how I can do this b/c I'm going to need the courses that the student is enrolled in and for a teacher, I would need the courses that he/she is teaching and taught. Any advice would be much appreciated.
So for example maybe something like this in the userprofile class:
class UserProfile {
public Guid UserID { get; set; }
public string fname { get; set; }
public string lname { get; set; }
public Classification classification { get; set; }
}
public enum Classification {
Student, Teacher
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的情况是为派生类型量身定做的。
UserProfile
应该是抽象的,有两个具体类型,称为Teacher
和Student
。这些继承的类型可以各自具有不同的属性。
Your situation is tailor-made for derived type.
UserProfile
should be abstract, with two concrete types calledTeacher
andStudent
.These inherited types can then each have different properties.
你的设计对我来说看起来不错。
您还可以使用布尔变量,例如
UserIsPatient
或UserIsTeacher
,但是通过使用枚举,您创建了一些可以在您的需求稍后发生变化时轻松扩展的内容 - 例如例如,如果您想添加实习教师、实习生或其他人员。Your design looks good to me.
You could also use a boolean variable, for instance
UserIsPatient
orUserIsTeacher
, but by using an enumeration you've created something that can be easily expanded if your requirements change later - for instance, if you wanted to add student teachers, interns, or whatever.