我必须为“用户”实体建模类和数据库表。
此“用户”实体具有以下属性:
User
Name
Age
Gender
Email
但我有2个用户类型:“付费用户”和“免费用户”。每个人都有自己的属性:
Paid User
Fee
Rate
Free User
Expiration Date
Level
“付费用户”和“免费用户”不同,但都是“用户”。
我正在使用ASP.NET MVC,NHibernate和Fluent Maps。
为这个类和数据库表建模的最佳方法和正确的设计模式是什么?
非常感谢!
答案 0 :(得分:3)
我经常发现最好的方法是使用继承模型。所有公共字段都进入User
表。您首先在User
中创建一个条目,然后根据需要使用生成的UserID
在Paid User
或Free User
中创建其他条目。
User
UserID
Name
Age
Gender
Email
Paid User
UserID <-- FK to User
Fee
Rate
Free User
UserID <-- FK to User
Expiration Date
Level
然后您可以选择(或创建VIEW
),如下所示:
select u.UserID, U.Name, ...
pu.Fee, pu.Rate,
fu.ExpirationDate, fu.Level,
case when pu.UserID is null then 0 else 1 end as IsPaidUser,
case when fu.UserID is null then 0 else 1 end as IsFreeUser
from User u
left outer join PaidUser pu on u.UserID = pu.UserID
left outer join FreeUser fu on u.UserID = fu.UserID
注意:上述架构非常幼稚,并不处理用户可以是免费用户,然后是付费用户,然后再次是免费用户的情况。您如何为此设计实际上取决于您的应用程序和业务规则。
答案 1 :(得分:2)
假设在您的对象模型中,用户将被视为抽象,请考虑以下用户表定义:
用户
ID 名称
年龄
性别
电子邮件
输入&lt; - 免费/付费/等付费用户
ID
UserID&lt; --- FK到用户表
费
率免费用户
ID
UserID&lt; --- FK到用户表
到期日期
水平
这将允许您以这种方式查询:
从用户中选择*,其中Type = Free
答案 2 :(得分:0)
theese用户的哪些操作类型?
任何设计都应该从操作开始,然后是数据结构,而不是相反。