继承和角色
让我们使用一个非常简单的例子,其中包含员工和公司(-ies)
。
public abstract class Employee
{
// bunch of its features
}
public sealed class SalesManager : Employee
{
}
public sealed class SEO : Employee
{
}
员工可以担任不同的职位或扮演不同的角色。因此,使用继承(可能还带有工厂模式)并不能为具体员工实例
提供改变其角色的灵活性。
您有什么建议,不幸的是我还没有看到这种方法。我还没有遇到一本能够阐明这个问题的书。
编辑
谢谢各位!在我的编辑中,我想再问一件事。使用通用角色
可以将这样的BLL传输到DAL。我听说实体框架不支持泛型??
谢谢!
Let's use a quite plain example with employees and company(-ies)
.
public abstract class Employee
{
// bunch of its features
}
public sealed class SalesManager : Employee
{
}
public sealed class SEO : Employee
{
}
Employee can take different posts or play different roles. So using inheritance (maybe with factory patterns in addition) doesn't give such a flexibility for concrete employee instance
to change its role.
What would you advice, unfortunately I haven't seen the kind of approaches yet. And I haven't met a book which lights up the problem.
Edit
Thank you guys! In my edit I wanted to ask one more thing. Using generic role
is it possible to transfer such a BLL to DAL. I have heard that generics are not supported in Entity Framework
??
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 has-a 关系
或者您可以将
Role
设为一个类来存储除角色名称之外的其他信息。为了说明@Aasmund Eldhuset 的评论:
Use a has-a relationship
Or you can make
Role
a class to store additional information in addition to the name of their role.To illustrate @Aasmund Eldhuset's comment:
按照使用类的想法运行,您可以使其通用:
或者
让不同的类型从此抽象继承:
然后有一个通用的工厂实现:
Running with the idea of using a class, you can make it generic:
or
And have different types inherit from this abstraction:
Then have a generic Factory implementation: