创建一个类似于 class 的 "Name" 属性 的枚举
Create an enum similar to the "Name" property of a class
我们的团队一直在慢慢重构代码以实施 SOLID 实践并实施更好的命名约定。我们 运行 遇到一个问题,我们有一个枚举当前与我们需要创建的另一个 class 命名相同 - "Department."
现在,我们有一个代表不同部门的枚举,例如:
Department.HumanResource
Department.InformationTechnology
这使我们能够使用该枚举在以下情况下基于友好名称而不是基础整数 ID 快速引用部门:
Employee.IsInDepartment(Department.InformationTechnology)
它不是 "Type" 或 "Status" 或类似的命名枚举的常用方法。我们想可能是 "DepartmentName" 之类的东西,但感觉有点奇怪,因为部门 class 将有 "Name" 属性,而这个枚举也应该是 属性 部门 class.
我意识到命名是主观的,所以我想提出问题:
我们是不是看错了?是否有另一种我们忽略的方法来实现这一目标?
在所有这些情况下(它经常弹出,尽管您的示例存在严重缺陷)我通常将枚举命名为 <thing>Type
,或者在您的情况下为 DepartmentType
。它是否 100% 正确可以争论,但它很容易理解这一点。
至于为什么这是有缺陷的,实际的部门列表应该来自数据库,因为这个列表会随着时间的推移而演变。您不希望每次在企业中添加新部门时都重新编译项目。
抱歉,这是 DepartmentType,也是命名约定最佳实践的方法,无论您喜欢与否,您都有一个具体的 class,如果您想遵循最佳实践,则必须将其命名为 Department,剩下的枚举必须是 DepartmentType 遵循命名约定的最佳实践,所以既然你需要 class 和枚举,我会像这样实现它:
创建枚举:
public enum DepartmentType {IT, HR,..... }
在部门 class 中,也将 Enum 添加为 属性:
public Class Department
{
public Name {get;set;}
.
.
.
public DepartmentType {get;set;}
}
现在您可以使用 DepartmentType 枚举,无论您有 department 对象还是想在任何方法中将其作为普通对象使用。
编辑
另一种方法是摆脱 Enum 并在您的部门中创建静态 int 值 Class:
public class Department
{
Public string Name {get;set;}
...
...
...
public static readonly int IT = 1;
public static readonly int HR = 2;
}
所以现在你可以将它用作 Department.IT
, Department.HR
,..... 而无需实例化 Department class 的对象,简而言之,它像枚举一样使用.
视情况而定。您希望通过 enum
实现什么目标?如果您希望关闭不同的部门 types 而不管该部门恰好被命名为什么,那么拥有 enum DepartmentType
是非常有意义的。例如:
if (department.Name == "Information Technology")
如果您的 IT 部门的名称已更改但您仍想对该部门执行相同的操作,则可能会很糟糕。然而,
if (department.Type == DepartmentType.InformationTechnology)
不在乎部门名称是什么;只要它代表一个 IT 部门,您的应用程序仍然有效。
此外,如其他答案中所述,如果您希望唯一标识一个部门,您确实希望使用其主键。您仍然可以通过将 static readonly
字段分配给 class:
来提高代码的可读性
public static readonly int InformationTechnology = 1;
这让您无需明确引用号码即可访问 ID:
if (department.Id = Department.InformationTechnology)
尽管这种方法的缺点是,如果您想在没有魔术整数的情况下继续检查,则在添加新 Id
时仍然需要重新编译代码。
在你的 class:
中使用常量
public class Department
{
//Property to hold the department ID
public int DepartmentID{ get; set; }
public const int HumanResources = 1;
public const int InformationTechnology = 2;
//And so on..
}
然后,如果 Employee.IsInDepartment 需要一个整数,您可以使用:
Employee.IsInDepartment(Department.HumanResources)...
由于您提到希望 "enum" 是功能性 classes 的表示(但不一定在每次使用引用时都加载 class' 数据),您可能会考虑这样的方法。它将使代码 感觉 像枚举,功能像 class,但不会进行任何不必要的访问数据存储以填充属性,直到需要它们为止。
public class Department
{
public int ID { get; private set; }
//follow this pattern for property values that need to be populated from a data store.
private string name;
public string Name
{
get{ EnsureLoad(); return name; }
set{ EnsureLoad(); name = value; }
}
public static Department HR{ get{ return GetEmptyDepartment( 1 ); } }
public static Department IT{ get{ return GetEmptyDepartment( 2 ); } }
private static Department GetEmptyDepartment(int departmentId)
{
return new Department()
{
ID = departmentId
};
}
private void EnsureLoad()
{
//if not loaded
//lazy load properties using the ID property against the data store.
}
}
我们的团队一直在慢慢重构代码以实施 SOLID 实践并实施更好的命名约定。我们 运行 遇到一个问题,我们有一个枚举当前与我们需要创建的另一个 class 命名相同 - "Department."
现在,我们有一个代表不同部门的枚举,例如:
Department.HumanResource
Department.InformationTechnology
这使我们能够使用该枚举在以下情况下基于友好名称而不是基础整数 ID 快速引用部门:
Employee.IsInDepartment(Department.InformationTechnology)
它不是 "Type" 或 "Status" 或类似的命名枚举的常用方法。我们想可能是 "DepartmentName" 之类的东西,但感觉有点奇怪,因为部门 class 将有 "Name" 属性,而这个枚举也应该是 属性 部门 class.
我意识到命名是主观的,所以我想提出问题:
我们是不是看错了?是否有另一种我们忽略的方法来实现这一目标?
在所有这些情况下(它经常弹出,尽管您的示例存在严重缺陷)我通常将枚举命名为 <thing>Type
,或者在您的情况下为 DepartmentType
。它是否 100% 正确可以争论,但它很容易理解这一点。
至于为什么这是有缺陷的,实际的部门列表应该来自数据库,因为这个列表会随着时间的推移而演变。您不希望每次在企业中添加新部门时都重新编译项目。
抱歉,这是 DepartmentType,也是命名约定最佳实践的方法,无论您喜欢与否,您都有一个具体的 class,如果您想遵循最佳实践,则必须将其命名为 Department,剩下的枚举必须是 DepartmentType 遵循命名约定的最佳实践,所以既然你需要 class 和枚举,我会像这样实现它:
创建枚举:
public enum DepartmentType {IT, HR,..... }
在部门 class 中,也将 Enum 添加为 属性:
public Class Department
{
public Name {get;set;}
.
.
.
public DepartmentType {get;set;}
}
现在您可以使用 DepartmentType 枚举,无论您有 department 对象还是想在任何方法中将其作为普通对象使用。
编辑
另一种方法是摆脱 Enum 并在您的部门中创建静态 int 值 Class:
public class Department
{
Public string Name {get;set;}
...
...
...
public static readonly int IT = 1;
public static readonly int HR = 2;
}
所以现在你可以将它用作 Department.IT
, Department.HR
,..... 而无需实例化 Department class 的对象,简而言之,它像枚举一样使用.
视情况而定。您希望通过 enum
实现什么目标?如果您希望关闭不同的部门 types 而不管该部门恰好被命名为什么,那么拥有 enum DepartmentType
是非常有意义的。例如:
if (department.Name == "Information Technology")
如果您的 IT 部门的名称已更改但您仍想对该部门执行相同的操作,则可能会很糟糕。然而,
if (department.Type == DepartmentType.InformationTechnology)
不在乎部门名称是什么;只要它代表一个 IT 部门,您的应用程序仍然有效。
此外,如其他答案中所述,如果您希望唯一标识一个部门,您确实希望使用其主键。您仍然可以通过将 static readonly
字段分配给 class:
public static readonly int InformationTechnology = 1;
这让您无需明确引用号码即可访问 ID:
if (department.Id = Department.InformationTechnology)
尽管这种方法的缺点是,如果您想在没有魔术整数的情况下继续检查,则在添加新 Id
时仍然需要重新编译代码。
在你的 class:
中使用常量public class Department
{
//Property to hold the department ID
public int DepartmentID{ get; set; }
public const int HumanResources = 1;
public const int InformationTechnology = 2;
//And so on..
}
然后,如果 Employee.IsInDepartment 需要一个整数,您可以使用:
Employee.IsInDepartment(Department.HumanResources)...
由于您提到希望 "enum" 是功能性 classes 的表示(但不一定在每次使用引用时都加载 class' 数据),您可能会考虑这样的方法。它将使代码 感觉 像枚举,功能像 class,但不会进行任何不必要的访问数据存储以填充属性,直到需要它们为止。
public class Department
{
public int ID { get; private set; }
//follow this pattern for property values that need to be populated from a data store.
private string name;
public string Name
{
get{ EnsureLoad(); return name; }
set{ EnsureLoad(); name = value; }
}
public static Department HR{ get{ return GetEmptyDepartment( 1 ); } }
public static Department IT{ get{ return GetEmptyDepartment( 2 ); } }
private static Department GetEmptyDepartment(int departmentId)
{
return new Department()
{
ID = departmentId
};
}
private void EnsureLoad()
{
//if not loaded
//lazy load properties using the ID property against the data store.
}
}