在 C# 中显式运算符应该返回 null 吗?
我正在编写一些显式运算符来将数据库模型类型转换为我的域模型。就像这样(简化的示例):
public static explicit operator DomainModel.Role(Role roleEntity)
{
DomainModel.Role role = new DomainModel.Role
{
RoleId = roleEntity.RoleId,
Name = roleEntity.Name
};
return role;
}
但是,roleEntity
参数可能为 null。大多数时候,在 .net 框架中,空实例的显式转换会导致异常。像这样:
user.Role = (DomainModel.Role)_userRepository.GetRole(user); // Would normally results in a NullReferenceException
但是如果显式运算符被调整,上面的函数将按预期运行:
public static explicit operator DomainModel.Role(Role roleEntity)
{
DomainModel.Role role = roleEntity == null ? null : new DomainModel.Role
{
RoleId = roleEntity.RoleId,
Name = roleEntity.Name
};
return role;
}
问题:
- 创建这样的显式运算符是否符合逻辑?
I'm writing some explicit operators to convert database model types to my domain model. Like so (simplified example):
public static explicit operator DomainModel.Role(Role roleEntity)
{
DomainModel.Role role = new DomainModel.Role
{
RoleId = roleEntity.RoleId,
Name = roleEntity.Name
};
return role;
}
However it's possible that the roleEntity
parameter is null. Most of times in the .net framework an explicit cast of a null instance results in an exception. Like so:
user.Role = (DomainModel.Role)_userRepository.GetRole(user); // Would normally results in a NullReferenceException
But if the explicit operator would be adjusted, the above would function as expected:
public static explicit operator DomainModel.Role(Role roleEntity)
{
DomainModel.Role role = roleEntity == null ? null : new DomainModel.Role
{
RoleId = roleEntity.RoleId,
Name = roleEntity.Name
};
return role;
}
Question:
- Is it logical to create such explicit operators?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于标题问题:允许显式运算符抛出(而隐式运算符则不应抛出)。
但是
null
是否是这样做的有效理由是一个设计决定,我认为不是。考虑一下:
哪个不会抛出异常。
To the title-question: An explicit operator is allowed to throw (while an implicit operator should not).
But whether a
null
is a valid reason to do so is a design decision, I would think not.Consider:
which does not throw.
我会使用诸如 AutoMapper 之类的库来促进相似类型之间的转换。您可以获得更大的灵活性并编写更少的代码。恕我直言,在阅读代码时使用显式转换运算符不太容易理解,它给人的印象是,当它可能是相当复杂的映射代码时,它是一种廉价的操作。
I would use a library such as a AutoMapper to facilitate conversion between similar types. You can get greater flexibility and write less code. IMHO, using explicit cast operators is less understandable when reading code, it gives an impression that it is a cheap operation when it could be pretty complex mapping code.
我想是的。将
null
引用转换为另一种类型通常会产生null
引用,以与正常语义相匹配。例如,因为这有效:
这也应该有效:
I think yes. Casting a
null
reference to another type should in general result in anull
reference, to match with the normal semantics.e.g. since this works:
This should also work:
恕我直言,不,你不应该这样做。
我认为两者之间没有任何区别:
在这两种情况下
,我都希望返回
null
这是默认行为(我不会推荐NullReferenceException
作为上面的两个代码片段完全有效:两个显式转换都返回null
)。我会发现它有点令人困惑,一个
null
引用可以以某种方式转换为默认对象:如果这种情况有意义,我将通过构造函数或特定的静态方法来实现它,很明显你是创建一个新对象,从不通过隐式或显式运算符。
强制转换运算符应该执行其预期的操作:从一种类型转换为另一种类型。
null
只能“转换”为null
。IMHO opinion no, you should not do this.
I see no difference at all between:
and
In both cases I would want a
null
returned which is the default behavior (I would not recommend aNullReferenceException
either as both code snippets above are completely valid: both explicit conversions returnnull
).I would find it somewhat bewildering that a
null
reference could somehow be converted into a default object:If this scenario makes sense I would implement it through a constructor or a specific static method where it is clear that you are creating a new object, never through an implicit or explicit operator.
Cast operators should do just what the are meant to do: convert from one type to another.
null
can only be "converted" tonull
.