【发布时间】:2014-02-08 11:38:31
【问题描述】:
我的应用程序中每个业务对象的type 必须保存到数据库中的某个表中。我需要用一些enum 或排序来代表每个type。
在另一个 dll 中存在一个独立于我的模型的基本框架,并且应该是。我的模型类首先必须从外部框架继承一个基类/接口。问题是我不能在外部 dll 中拥有代表我的业务对象的enum,因为它应该独立于任何模型。例如,
外部 dll 中的基类:
namespace external
{
public enum EnumThatDenotesPoco { Vehicle, Animal, Foo }
public abstract class Framework
{
public abstract EnumThatDenotesPoco RecordType { get; }
}
}
和我的项目:
namespace ourApplication
{
public class Vehicle : Framework
{
public override EnumThatDenotesPoco RecordType { get { return EnumThatDenotesPoco.Vehicle; } }
}
}
因为Vehicle, Animal, Foo 在我的应用程序项目中,所以无法工作。在这种情况下,什么是更好的设计?
我有两种方法可以解决,但不确定这是否是正确的方法。
1.
namespace external
{
public abstract class Framework
{
public abstract Enum RecordType { get; } //base class of all enums
}
}
namespace ourApplication
{
public enum EnumThatDenotesPoco { Vehicle, Animal, Foo }
public class Vehicle : Framework
{
public override Enum RecordType { get { return EnumThatDenotesPoco.Vehicle; } }
}
}
这行得通。 vehicle.RecordType. 正确地产生 0。
2.
namespace external
{
public class EntityBase // an empty enum class
{
}
public abstract class Framework
{
public abstract EntityBase RecordType { get; }
}
}
namespace ourApplication
{
public sealed class Entity : EntityBase
{
public static readonly Entity Vehicle = 1;
public static readonly Entity Animal = 2;
public static readonly Entity Foo = 3; //etc
int value;
public static implicit operator Entity(int x)
{
return new Entity { value = x };
}
public override string ToString()
{
return value.ToString();
}
}
public class Vehicle : Framework
{
public override EntityBase RecordType { get { return Entity.Vehicle; } }
}
}
两者都有效。
【问题讨论】:
-
使用T4模板从DB表生成枚举代码...
-
你能给我一个相关的链接吗?或者一些关键词来做谷歌搜索?
-
-> "T4 模板枚举 C# 代码"
标签: c# database-design types enums