保存类的类型数据库的最佳方式? [英] Best way to save type of class in database?

查看:90
本文介绍了保存类的类型数据库的最佳方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是一个很好的方式来表示数据库中的输入

What is a good way to denote "type" in database?

我有一个基类行动这是由众多子类继承。 动作的成员如编号名称等都对应在称为数据库表中的列相当于动作。因此,动作表如下所示:

I have a base class Action which is inherited by numerous child classes. Action has members like Id, Name etc all which corresponds to equivalent columns in a table in the database called action. So action table looks like this:

id | name | type 



键入列表示什么动作它是。在我的代码它们所对应的子类从父动作导出。现在我该怎样在类型的类保存到数据库中的类型字段

The type column denotes what action it is. In my code they correspond to the child classes deriving from parent Action. Now how do I save the type of class to the type field in database?

在数据库的类型列。可以是任何数据类型

The type column in db could be of any data type.

选项:


  1. 保存 action.GetType()的ToString()在数据库字符串。并通过使用反射类型的字符串表示转换为原来​​的类型从数据库获取回操作类型。 但是,这将是有问题的,如果类名称在将来改变。

  1. Save action.GetType().ToString() as string in db. And get the action type from db back by converting the string representation of the type to its original type using reflection. But this will be problematic if class names change in future.

创建一个枚举来表示每个子类,并通过装饰它 TypeAttribute ,是这样的:

Create an enum to denote each child class and decorate it by a TypeAttribute, something like:

public abstract class Action
{
    public enum Kind 
    {
        [Type(typeof(ControlAction))]
        ControlAction = 1, 

        [Type(typeof(UpdateAction))]
        UpdateAction = 2, 

        etc 
    }

    public abstract Kind ActionType { get; }
}

public class ControlAction : Action { public override Kind ActionType { get { return Kind.ControlAction; } } }
public class UpdateAction : Action { public override Kind ActionType { get { return Kind.UpdateAction; } } }
//etc



这看起来不错,只是每种从这里一流的起我要创建一个枚举。并感觉像做了一点工作太多了。

This looks good, except that for each class from here onwards I have to create an enum. And it feels like a little too much work to be done.

建立一个单独的静态<的哈希表; INT,类型> 是关系类到 INT 值。 可能有点不可读。

Build a separate static hash table of <int, Type> that ties a class to a int value. May be a little bit unreadable.

有没有更好的解决这个?

Is there a better solution to this?

推荐答案

我最终使用选项2,但属性减少混乱。事情是这样的:

I ended up using option 2, but with less clutter of attributes. Something like this:

public abstract class Action
{
    public enum Kind 
    {
        ControlAction = 1, 

        UpdateAction = 2, 

        etc 
    }

    public abstract Kind ActionType { get; }
}

public class ControlAction : Action { public override Kind ActionType { get { return Kind.ControlAction; } } }
public class UpdateAction : Action { public override Kind ActionType { get { return Kind.UpdateAction; } } }



这样做的最大好处是,(即使这意味着更多的输入),<强>它强制数值要与类类型

现在类为int相关的就是的:

var value = (int)instance.ActionType;



非常快的。

Very fast.

但要转换的int类的实例(或类型),我将不得不创建每个子动作类型的实例,并比较其操作类型属性来匹配输入的int值。这将是缓慢的。但我可以缓存出头,使其速度更快。类似的:

But to convert int to class instance (or class type), I will have to create an instance of each sub action types, and compare its ActionType property to match the input int value. This is going to be slow. But I can cache somethings and make it faster. Something like:

static readonly Dictionary<Action.Kind, Type> actionTypes = 
   GetDefaultInstanceOfAllActions().ToDictionary(x => x.ActionType, x => x.GetType());
public static Action ToAction(this Action.Kind value)
{
    return (Action)Activator.CreateInstance(actionTypes[value]);
}



GetDefaultInstanceOfAllActions 做一些反思(一次)来获取所有类型的动作(我使用类似对于这个答案)。我甚至可以让使实例更快地去表达途径。

The GetDefaultInstanceOfAllActions does some reflection (once) to get all types of actions (I use something like this answer for that). I can even make the make the instantiation faster by going the expression route.

好处:在创建时


  1. 少些麻烦一个新的类(无属性)。

  1. Less hassle when creating a new class (no attributes).

强制执行一个int被绑定到一个类的类型。

Enforces an int to be tied to a class type.

适度快速提供足够的缓存。

Moderately fast with adequate caching.

这篇关于保存类的类型数据库的最佳方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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