【问题标题】:Represent every type by an enum in different assembly so that they can be saved to database在不同的程序集中用枚举表示每种类型,以便可以将它们保存到数据库中
【发布时间】: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


【解决方案1】:

在考虑 T4 模板时,我发现它比需要的复杂。我采用了第二种方法并进行了一些修改。

Enum 方法的问题:

  1. 我可以将System.Enum 直接保存(Convert.ToInt32(System.Enum)System.Enum.ToString() 等可以给出实际的基础值)到 db,但我无法从 db 中以System.Enum 干净地检索它。

  2. System.Enum 是完全通用的,可能会让人感到困惑,因为它什么也没透露。

我采用了第二种方法,但稍作修改:

namespace external
{
    public abstract class Framework
    {
        public abstract Entity RecordType { get; } 
    }



    public sealed class Entity
    {
        int value;

        Entity()
        {

        }

        public static implicit operator Entity(int i)
        {
            return new Entity { value = i };
        }

        public bool Equals(Entity other)
        {
            if (ReferenceEquals(this, other))
                return true;

            if (ReferenceEquals(null, other))
                return false;

            return value == other.value;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as Entity);
        }

        public static bool operator ==(Entity lhs, Entity rhs)
        {
            if (ReferenceEquals(lhs, null))
                return ReferenceEquals(rhs, null);

            return lhs.Equals(rhs);
        }

        public static bool operator !=(Entity lhs, Entity rhs)
        {
            return !(lhs == rhs);
        }

        public override int GetHashCode()
        {
            return value;
        }

        public override string ToString()
        {
            return value.ToString();
        }
    }
}

我通过提供所有逻辑使基本枚举类 (Entity) 非空。所以现在我不需要在引用的项目中编写验证代码。我可以在主程序集中的某处写枚举值:

namespace ourApplication
{
    public static class Global
    {
        public static readonly Entity Vehicle = 1;
        public static readonly Entity Animal = 2;
        public static readonly Entity Foo = 3; //etc
    }

    public class Vehicle : Framework
    {
        public override Entity RecordType { get { return  Global.Vehicle; } }
    }
}

现在保存和检索变得轻而易举:

//insert as just vehicle.RecordType

//select as (Entity)reader.GetInt32(index)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-20
    • 2013-09-17
    • 2012-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多