开发者

Should explicit operators return null in c#?

开发者 https://www.devze.com 2023-03-24 07:04 出处:网络
I\'m writing some explicit operators to convert database model types to my domain model. Like so (simplified example):

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 expli开发者_如何学JAVAcit 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?


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.


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.


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;


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号