发行使用的成分“是 - a”关系 [英] Issue in using Composition for “is – a “ relationship

查看:189
本文介绍了发行使用的成分“是 - a”关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个人力资源系统正在开发的系统。有会计师员工和程序员的员工。对于加入该公司的第一个月,员工没有给出任何作用。一个雇员可以是会计师,同时一个程序员。 。我有下面的代码所示设计



现在,我需要实施新的功能,以提升系统:




终止所有会计师。 (终止员工的手段作为IsActive =假的设置状态)。问题是我不能直接将所有的会计师为不活动不检查。我需要检查自己是否有任何其他作用。




如何才能做到终止功能更自然的面向对象的改造这些类?



更新



我要寻找的具有@AlexDev答案EF数据库优先解决模型和数据库架构的答案。



C#代码

 列表<会计师> allAccountants =获取所有的数据库

公共类员工
{
公众诠释的EmpID {会计师搞定;组; }
公众的DateTime JoinedDate {搞定;组; }
公众诠释薪水{搞定;组; }
公共布尔IsActive {搞定;组; }
}


公共类注册会计师:员工
{
公务员EmployeeData工作{搞定;组; }
}

公共类程序员:员工
{
公务员EmployeeData工作{搞定;组; }
}





@AlexDev答案

 公共类Employee 
{
...
&IList的LT;作用>角色;
BOOL isActive;

公共无效TerminateRole(角色角色)
{
Roles.Remove(作用);
如果(Roles.Count == 0)
{
isActive = FALSE;
}
}
}

公共类角色
{
抽象的字符串名称{;}
}

酒店的公共类ProgrammerRole:角色
{
重写字符串名称{{返回程序员 }}
}



参考




  1. DDD的方式获取外部信息

  2. 了宗教成分继承?

  3. 继承VS域模型

  4. 实体框架:在储备库中获得子类对象


解决方案

要使用你正在使用你需要的人谁是一名会计和一名程序员,除了新角色可能会被添加到系统中,并没有在C#中存在多重继承的结构。你应该考虑不同的设计。一种可能性:

 公共类Employee 
{
...
的IList<作用>角色;
BOOL isActive;

公共无效TerminateRole(角色角色)
{
Roles.Remove(作用);
如果(Roles.Count == 0)
{
isActive = FALSE;
}
}
}

公共类角色
{
抽象的字符串名称{;}
}

酒店的公共类ProgrammerRole:角色
{
重写字符串名称{{返回程序员 }}
}



然后,你也可以继承为每种类型的角色,你可以决定终止只是一个角色,或全部。


I have system being developed for an HR system. There are Accountant employees and Programmer employees. For the first month of joining the company, the employee is not given any role. One employee can be an Accountant and a programmer at the same time. I have a design shown by the following code.

Now, I need to enhance the system by implementing a new functionality:

Terminate all Accountants. (Terminate means set status of employee as IsActive = false). The issue is I cannot set all accountants directly as inactive without checking. I need to check whether he has got any other role.

How to remodel these classes in order to do the terminate function more natural OO ?

UPDATE

I am looking for an answer that has EF Database First solution model and database schema for @AlexDev answer.

C# Code

List<Accountant> allAccountants =  Get All accountants from database

public class Employee
{
    public int EmpID { get; set; }
    public DateTime JoinedDate { get; set; }
    public int Salary { get; set; }
    public bool IsActive { get; set; }
}


public class Accountant : Employee
{
    public Employee EmployeeData { get; set; }
}

public class Programmer : Employee
{
    public Employee EmployeeData { get; set; }
}

@AlexDev Answer

public class Employee
{
...
IList<Role> Roles;
bool isActive;

public void TerminateRole(Role role)
{
    Roles.Remove(role);
    if(Roles.Count == 0)
    {
        isActive = false;
    }
}
}

public class Role
{
 abstract string Name { get;}
}

public class ProgrammerRole : Role
{
 override string Name { get { return "Programmer"; } }
}

REFERENCE

  1. DDD Approach to Access External Information
  2. Prefer composition over inheritance?
  3. Inheritance vs enum properties in the domain model
  4. Entity Framework: Get Subclass objects in Repository

解决方案

To use the structure you are using you would need multiple inheritance for someone who is an accountant and a programmer, besides new roles might be added to the system, and that doesn't exist in C#. You should consider a different design. One possibility:

public class Employee
{
    ...
    IList<Role> Roles;
    bool isActive;

    public void TerminateRole(Role role)
    {
        Roles.Remove(role);
        if(Roles.Count == 0)
        {
            isActive = false;
        }
    }
}

public class Role
{
    abstract string Name { get;}
}

public class ProgrammerRole : Role
{
    override string Name { get { return "Programmer"; } }
}

Then you can subclass Role for each type, and you can decide to terminate just one role, or all of them.

这篇关于发行使用的成分“是 - a”关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆