我应该使用值还是键?

时间:2022-08-13 07:07:33

I am warpping an ArcGIS IFeature object with a class that has my required properties to get and set them easily. Basically, the get and set operations just use feature.get_value(index) and feature.set_value(indes, value), and expose the strongly typed value.
I have several fields that use a domain (basically, an IDictionary<string, object>) to represent common properties across the application.
At first I figured I would only use the keys of the domain (which are normal int values) in my wrapping class, and use the domain in my ToString() method, to translate to strings for the UI.
Later I figured out I can use strings in my applicaiton (which makes it easier to pass around nulls, as the actual domain fields are nullable most of the time), and only change those fields' getters and setters to use GetDomainValue(index) and SetDomainValue(index, value) method that will translate between the key and value to/from the underlying feature object.

我正在使用具有我所需属性的类来扭曲ArcGIS IFeature对象,以便轻松获取和设置它们。基本上,get和set操作只使用feature.get_value(index)和feature.set_value(indes,value),并公开强类型值。我有几个字段使用域(基本上是IDictionary )来表示应用程序中的公共属性。起初我想我只会在我的包装类中使用域的键(这是普通的int值),并在我的ToString()方法中使用域来转换为UI的字符串。后来我发现我可以在我的应用程序中使用字符串(这使得更容易传递空值,因为实际的域字段在大多数时间都可以为空),并且只更改这些字段的getter和setter以使用GetDomainValue(index)和SetDomainValue(index,value)方法,它将在键和值之间转换为基础要素对象。 ,object>

What approach do you think is better? I figured the string approach is a bit more "persistent ignorant", as my class doesn't care how the values are being saved, just their string representation. On the other hand, it makes the code jump through loops a bit - instead of returning what's in the feature, every getter needs to iterate the domain.

你认为哪种方法更好?我认为字符串方法更加“持久无知”,因为我的课程并不关心如何保存值,只是它们的字符串表示。另一方面,它使代码跳过循环 - 而不是返回功能中的内容,每个getter都需要迭代域。

2 个解决方案

#1


You might want to think about representing your domain fields with Nullable< Int32>. This would give you a way to represent features that have a domain value specified but it also allows you to directly specify null where appropriate.

您可能想要考虑使用Nullable 表示域字段。这将为您提供一种表示具有指定域值的功能的方法,但它也允许您在适当的地方直接指定null。

#2


I ended up sticking with the domain values, so that I can also verify that the calling code passed a valid value.

我最终坚持使用域值,这样我也可以验证调用代码是否传递了有效值。

#1


You might want to think about representing your domain fields with Nullable< Int32>. This would give you a way to represent features that have a domain value specified but it also allows you to directly specify null where appropriate.

您可能想要考虑使用Nullable 表示域字段。这将为您提供一种表示具有指定域值的功能的方法,但它也允许您在适当的地方直接指定null。

#2


I ended up sticking with the domain values, so that I can also verify that the calling code passed a valid value.

我最终坚持使用域值,这样我也可以验证调用代码是否传递了有效值。