在 C# 中显式运算符应该返回 null 吗?

发布于 2024-11-27 14:25:49 字数 855 浏览 1 评论 0原文

我正在编写一些显式运算符来将数据库模型类型转换为我的域模型。就像这样(简化的示例):

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

听风吹 2024-12-04 14:25:49

对于标题问题:允许显式运算符抛出(而隐式运算符则不应抛出)。

但是 null 是否是这样做的有效理由是一个设计决定,我认为不是。

考虑一下:

object x = null;
string t = (string)x;

哪个不会抛出异常。

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:

object x = null;
string t = (string)x;

which does not throw.

憧憬巴黎街头的黎明 2024-12-04 14:25:49

我会使用诸如 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.

灯下孤影 2024-12-04 14:25:49

我想是的。将 null 引用转换为另一种类型通常会产生 null 引用,以与正常语义相匹配。

例如,因为这有效:

object x = null;
var role = (DomainModel.Role)x;

这也应该有效:

Role x = null;
var role = (DomainModel.Role)x;

I think yes. Casting a null reference to another type should in general result in a null reference, to match with the normal semantics.

e.g. since this works:

object x = null;
var role = (DomainModel.Role)x;

This should also work:

Role x = null;
var role = (DomainModel.Role)x;
倥絔 2024-12-04 14:25:49

恕我直言,不,你不应该这样做。

我认为两者之间没有任何区别:

 string myString= (string)null;

在这两种情况下

 Object o = null;
 string myString= (string)o;

,我都希望返回 null 这是默认行为(我不会推荐 NullReferenceException 作为上面的两个代码片段完全有效:两个显式转换都返回 null)。

我会发现它有点令人困惑,一个 null 引用可以以某种方式转换为默认对象:

object o = null;
string myString = (string)o; //myString == String.Empty WTF?

如果这种情况有意义,我将通过构造函数或特定的静态方法来实现它,很明显你是创建一个新对象,从不通过隐式或显式运算符。

强制转换运算符应该执行其预期的操作:从一种类型转换为另一种类型。 null 只能“转换”为 null

IMHO opinion no, you should not do this.

I see no difference at all between:

 string myString= (string)null;

and

 Object o = null;
 string myString= (string)o;

In both cases I would want a null returned which is the default behavior (I would not recommend a NullReferenceException either as both code snippets above are completely valid: both explicit conversions return null).

I would find it somewhat bewildering that a null reference could somehow be converted into a default object:

object o = null;
string myString = (string)o; //myString == String.Empty WTF?

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" to null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文