使用枚举的数据层对象的“地位”在C# [英] Using Enum for a data layer object's 'status' in C#

查看:149
本文介绍了使用枚举的数据层对象的“地位”在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据对象(让我们说这就是所谓的入门),有一组是这个样子的潜在状态的:

I have a data object (let's say it's called 'Entry') that has a set of potential states that look something like this:

1 - Created
2 - File added
3 - Approved
4 - Invalid

这是psented在数据库与状态表的自动编号主键,然后在主表中的StatusId现场重新$ P $,与建立适当的关系。

This is represented in the database with a 'Status' table with an autonumber primary key, then a 'StatusId' field in the main table, with the appropriate relationships set up.

在我的(自定义)数据层,我有'入口'的对象,目前,我还声明了一个枚举,其中列出上述规定的状态。最后,我宣布这个枚举的私有实例以及相应的公共财产。

In my (custom) data layer, I have the 'Entry' object, and, currently, I also declare an Enum with the states listed above specified. Finally I declare a private instance of this Enum along with the appropriate public property.

在我的提交()方法,我投了枚举的实例为整数,并把它传递到一个更新的存储过程。

In my 'Commit()' method I cast the instance of the Enum to an integer and pass it to an Update stored procedure.

在我的静态'GetEntry()方法,我显然有一个整数通过从数据库返回的。然后我使用'Enum.Parse()'方法以提取一个对象,它是我的枚举的对应于返回的状态整数一个实例。我投这对我的枚举类型,并将其分配给本地私有变量。

In my static 'GetEntry()' method I will obviously have an integer passed back from the database. I then use the 'Enum.Parse()' method to extract an object which is an instance of my Enum which corresponds to the returned status integer. I cast this to the type of my Enum and assign it to the local private variable.

我的问题是pretty的简单 - 就是这种做法适当,如果没有什么替代,其他不仅仅是存储原始整型值(我是不一定反对),是更好的

My question is pretty simple - is this approach appropriate, and if not what alternative, other than just storing the raw integer value (which i'm not necessarily averse to), is better.

我之所以问的是,这一切似乎只是令人难以置信的凌乱对我来说,所有的铸造和保持相同的组值的两个列表的内容。我接受的优势在于对数据对象的消费者更好的体验,但即便如此...

My reason for asking is that this all just seems incredibly messy to me, what with all the casting and maintaining two lists of the same set of values. I accept the benefit lies in a better experience for the consumer of the data object, but even so...

谢谢!

推荐答案

我们有一些熟悉的东西在我们的项目之一。 我们有一个表containting类型的项目。这些类型都有一个ID,在code,我们有相同的id的枚举。 问题是,在数据库中,我们不使用自动编号(标识),所以我们有充分的控制ID的。 和保存我们的对象的时候,我们只取枚举的ID保存对象。 我也认为这种做法是乱,但它并不坏毕竟。

We have something familiar in one of our projects. We have a table containting types of items. These types have an id, in the code we have an enum with the same id's. The thing is, in the database we don't use autonumber (identity) so we have full control of the id. And when saving our object we just take the id of the enum to save the object. I also thought this approach was messy but it's not that bad afterall.

这篇关于使用枚举的数据层对象的“地位”在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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