使用 ref 改变部分不可变类的方法设计
在我的项目中,我有一个标头类,它表示系统内一条信息的全局唯一键,例如它属于谁,它存在的时间等。在同一个标头类中,我还有用于信息的字段特定于给定的数据实例,例如谁创建了该版本的信息、何时创建、是否需要将其新数据保存到数据库等。
下面是一个将某些信息存入数据的示例传输类并查询它。
var header = new IntvlDataHeader(
datapoint: Guid.NewGuid(),
element: Guid.NewGuid(),
intervalUtc: DateTime.Now.Date);
package.StockData_Decimal(header, 5m);
decimal cloneData;
package.TryGetData_Decimal(ref header, out cloneData);
// header now refers to a different object, that could have different flags/information
请注意我如何使 TryGetData_Decimal 通过引用传递标头变量。 IntvlDataHeader 是一个类,如果在 TryGetData 中找到数据,则引用将更改为指向 IntvlDataHeader 的新实例,该实例除了具有相同的唯一密钥信息之外,还具有特定的实例信息。
将密钥与实例特定信息相结合并使用 ref 参数作为输入和输出是否是一个糟糕的设计?拆分另一个类以便有两个 out 参数并且没有 ref 参数的工作是否会更好或避免任何潜在的问题?
该方法的签名是 public bool TryGetData_Decimal(ref IntvlDataHeader header, out Decimal data)
In my project I have a header class that represents a globally unique key for a piece of information inside the system, such as who it belongs to, what time it exists for, etc. Inside the same header class I also have fields for information that is specific to a given instance of data, such as who created this version of the information, when it was created, if its new data that needs to be saved to the database, etc.
Here is an example of stocking some information into a data transport class and querying it back out.
var header = new IntvlDataHeader(
datapoint: Guid.NewGuid(),
element: Guid.NewGuid(),
intervalUtc: DateTime.Now.Date);
package.StockData_Decimal(header, 5m);
decimal cloneData;
package.TryGetData_Decimal(ref header, out cloneData);
// header now refers to a different object, that could have different flags/information
Note how I made the TryGetData_Decimal pass the header variable by reference. IntvlDataHeader is a class, and if the data is found inside TryGetData then the reference is changed to point to a new instance of IntvlDataHeader that has the specific instance information in addition to having the same unique key information.
Is combining a key with instance specific information and using a ref parameter as both in and out a bad design? Would the effort of splitting out another class so that there would be two out parameters and no ref parameters be better or avoid any potential problems?
The signature of the method is public bool TryGetData_Decimal(ref IntvlDataHeader header, out decimal data)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您的
TryGetData_Decimal
的命名具有误导性,如果您传入的ref
参数在方法退出时指向一个新实例。对我来说,TryGetData_Decimal 听起来像是许多值类型上的 TryParse 方法的变体(它有一个包含解析值的输出参数 - 类似于cloneData
参数)。我想我不确定为什么标头对象必须指向一个新实例,所以我不确定我可以推荐一种设计。 那么它可能更具可读性
如果这就是您需要做的,我认为如果您的
TryGetData_XXX
方法具有类似这样的签名: header 被传入,但在方法退出时不会更改, 。该方法返回新实例,如果需要,您可以使用它。我不会更改cloneData
- 我认为out
参数只要不被过度使用就可以。我也会尝试将该方法的名称更改为更有意义的名称。
我希望这有帮助。
I think the naming of your
TryGetData_Decimal
is misleading, if theref
parameter your passing in will then point to a new instance when the method exits.TryGetData_Decimal
, to me, sounds like a variation of theTryParse
methods on a number of value types (which has an out parameter containing the parsed value - similar to thecloneData
parameter).I guess I'm not sure why the header object has to point to a new instance, so I'm not sure I can recommend a design. If that's what you need to do, I think it may be more readable if your
TryGetData_XXX
methods have a signature something like this:where header is passed in, but doesn't change when the method exits. The method returns the new instance, and you can use it if you need it. I wouldn't change the
cloneData
- I thinkout
parameters are OK as long as they aren't overused.I'd try to change the name of the method to something more meaningful, too.
I hope this helps.