How to manage type conversion in C #

Is there a way to control type conversion in C #? So, for example, if I have two types with essentially the same details, but one is used for the internal operation of my application, and the other is the DTO, used to communicate with non-net applications:

public sealed class Player { public Player(string name, long score) { Name = name; Score = score; ID = Guid.NewGuid(); } public string Name { get; private set; } public Guid ID { get; private set; } public long Score { get; private set; } } public sealed class PlayerDTO { public PlayerDTO(string name, long score, string id) { Name = name; Score = score; ID = id; } public string Name { get; private set; } // the client is not .Net and doesn't have Guid public string ID { get; private set; } public long Score { get; private set; } } 

Right now, I need to create a new PlayerDTO instance each time from my Player instance, and I'm looking for a better, cleaner way to do this. One of my ideas was to add the AsPlayerDTO () method to the player class, but it would be nice if I could control the type conversion process so that I could do this instead:

 var playerDto = player as PlayerDTO; 

Does anyone know if this is possible and how I can do it?

Thanks,

+7
c # types type-conversion
source share
7 answers

You can implement either implicit or explicit type conversion: http://msdn.microsoft.com/en-us/library/ms173105.aspx .

Alternatively, if you want each class not to know about the other, you can use either a custom mapping or an existing mapping library, such as AutoMapper .

+3
source share

You can implement an explicit convection operator between two types.

You can also use AutoMapper for the task.

+5
source share

What about the conversion operator:

 public static explicit operator PlayerDTO (Player value)... public static implicit operator PlayerDTO (Player value)... 
0
source share

You still have to do the code to create the DTO object, but you can overload the casting operator to apply it from the implementation object to the DTO object. You can even do an implicit listing.

see transfer overload and implicit

0
source share

You can use conversions of type explicit .

 public sealed class Player { public static explicit operator PlayerDTO(Player p) { PlayerDTO dto; // construct return dto; } } public sealed class PlayerDTO { public static explicit operator Player(PlayerDTO dto) { Player p; // construct return p; } } 
0
source share
0
source share

It looks like you should use the interface.

Then add to the interface: Example below

 public interface IPlayer { string Name { get; set; } } public class Player : IPlayer { string Name { get; set; } } IPlayer playerDto = player as IPlayer; Player player = Player(playerDto); 

Then you should use IPlayer as a DTO object. I assume this is because the Player class does not exist in another assembly, and you do not want it anymore. Then you can use IPlayer in the general assembly ... to transfer between assemblies.

Hope this helps ...; -)

Update

#

Auto Mapper is the best route! I think,

0
source share

All Articles