继承和角色

发布于 2024-11-18 21:32:32 字数 467 浏览 3 评论 0原文

让我们使用一个非常简单的例子,其中包含员工和公司(-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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断肠人 2024-11-25 21:32:32

使用 has-a 关系

public class Employee 
{
    public Role EmployeeRole { get; set; }
}

public enum Role 
{
    SalesManager,
    SalesPerson
}

或者您可以将 Role 设为一个类来存储除角色名称之外的其他信息。

public class Role
{
    public string Name { get; set; }
    public decimal BaseSalary { get; set; }
}

为了说明@Aasmund Eldhuset 的评论:

public abstract class Role
{
    public string Name { get; set; }
    public decimal BaseSalary { get; set; }

    public abstract void PerformRole();
}

public class SalesPerson : Role
{
    public void PerformRole()
    {
        // Do something
    }
}

Use a has-a relationship

public class Employee 
{
    public Role EmployeeRole { get; set; }
}

public enum Role 
{
    SalesManager,
    SalesPerson
}

Or you can make Role a class to store additional information in addition to the name of their role.

public class Role
{
    public string Name { get; set; }
    public decimal BaseSalary { get; set; }
}

To illustrate @Aasmund Eldhuset's comment:

public abstract class Role
{
    public string Name { get; set; }
    public decimal BaseSalary { get; set; }

    public abstract void PerformRole();
}

public class SalesPerson : Role
{
    public void PerformRole()
    {
        // Do something
    }
}
苹果你个爱泡泡 2024-11-25 21:32:32

按照使用类的想法运行,您可以使其通用:

abstract class EmployeeRole { } 

或者

interface EmployeeRole { }

让不同的类型从此抽象继承:

class CEO : EmployeeRole { }

class SalesMgr : EmployeeRole { }

class Employee<T> where T : EmployeeRole
{

}

然后有一个通用的工厂实现:

public Employee<T> MakeEmployee<T>() where T : EmployeeRole
{

}

Running with the idea of using a class, you can make it generic:

abstract class EmployeeRole { } 

or

interface EmployeeRole { }

And have different types inherit from this abstraction:

class CEO : EmployeeRole { }

class SalesMgr : EmployeeRole { }

class Employee<T> where T : EmployeeRole
{

}

Then have a generic Factory implementation:

public Employee<T> MakeEmployee<T>() where T : EmployeeRole
{

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文