using DTOs; using Entities; using Model; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ExtensionsClassLibrairie { /// /// define some methods to manipulate entity, model and dto players : /// convert model to DTO, model to Entity, ... /// and equality protocols /// public static class PlayerExtensionMethods { // conversion methods public static Player ToModel(this PlayerEntity p) => new Player(p.Nickname, p.HashedPassword, p.Id); public static Player ToModel(this PlayerDto p) => new Player(p.Nickname, p.HashedPassword, p.Id); public static PlayerDto ToDto(this Player p) => new PlayerDto { Id = p.Id, Nickname = p.Nickname, HashedPassword = p.HashedPassword}; public static PlayerEntity ToEntity(this Player p) => new PlayerEntity { Id = p.Id, Nickname = p.Nickname, HashedPassword = p.HashedPassword }; // reuse other methods public static PlayerDto ToDto(this PlayerEntity p) => p.ToModel().ToDto(); public static PlayerEntity ToEntity(this PlayerDto p) => p.ToModel().ToEntity(); // equality protocols public static bool Equals(this Player p1, Player p2) => p1.Nickname == p2.Nickname; // reuse other methods public static bool Equals(this Player p1, PlayerDto p2) => p1.Equals(p2.ToModel()); public static bool Equals(this Player p1, PlayerEntity p2) => p1.Equals(p2.ToModel()); public static bool Equals(this PlayerDto p1, Player p2) => p1.ToModel().Equals(p2); public static bool Equals(this PlayerDto p1, PlayerDto p2) => p1.ToModel().Equals(p2.ToModel()); public static bool Equals(this PlayerDto p1, PlayerEntity p2) => p1.ToModel().Equals(p2.ToModel()); public static bool Equals(this PlayerEntity p1, Player p2) => p1.ToModel().Equals(p2); public static bool Equals(this PlayerEntity p1, PlayerDto p2) => p1.ToModel().Equals(p2.ToModel()); public static bool Equals(this PlayerEntity p1, PlayerEntity p2) => p1.ToModel().Equals(p2.ToModel()); /// /// equality protocol for a player /// /// the player /// an object /// true if the player and the object are sames, false otherwise public static bool Equals(this Player p, object? o) { if (o == null) return false; if (o.GetType() == typeof(Player)) return Equals(p, (Player)o); else if (o.GetType() == typeof(PlayerEntity)) return Equals(p, ((PlayerEntity)o)); else if (o.GetType() == typeof(PlayerDto)) return Equals(p, (PlayerDto)o); return false; } /// /// equality protocol for a player entity /// /// the player entity /// an object /// true if the player entity and the object are sames, false otherwise public static bool Equals(this PlayerEntity p, object? o) { if (o == null) return false; if (o.GetType() == typeof(Player)) return Equals(p, (Player)o); else if (o.GetType() == typeof(PlayerEntity)) return Equals(p, ((PlayerEntity)o)); else if (o.GetType() == typeof(PlayerDto)) return Equals(p, (PlayerDto)o); return false; } /// /// equality protocol for a player dto /// /// the player dto /// an object /// true if the player dto and the object are sames, false otherwise public static bool Equals(this PlayerDto p, object? o) { if (o == null) return false; if (o.GetType() == typeof(Player)) return Equals(p, (Player)o); else if (o.GetType() == typeof(PlayerEntity)) return Equals(p, ((PlayerEntity)o)); else if (o.GetType() == typeof(PlayerDto)) return Equals(p, (PlayerDto)o); return false; } } }