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 chapters : /// convert model to DTO, model to Entity, ... /// and equality protocols /// public static class ChapterExtensionMethods { // conversion methods public static Chapter ToModel(this ChapterEntity c) => new Chapter(c.Name, c.Id); public static Chapter ToModel(this ChapterDto c) => new Chapter(c.Name, c.Id); public static ChapterDto ToDto(this Chapter c) => new ChapterDto { Id = c.Id, Name = c.Name }; public static ChapterEntity ToEntity(this Chapter c) => new ChapterEntity { Id = c.Id, Name = c.Name }; // reuse other methods public static ChapterDto ToDto(this ChapterEntity c) => c.ToModel().ToDto(); public static ChapterEntity ToEntity(this ChapterDto c) => c.ToModel().ToEntity(); // equality protocols public static bool Equals(this Chapter c1, Chapter c2) => c1.Name == c2.Name; // reuse other methods public static bool Equals(this Chapter c1, ChapterDto c2) => c1.Equals(c2.ToModel()); public static bool Equals(this Chapter c1, ChapterEntity c2) => c1.Equals(c2.ToModel()); public static bool Equals(this ChapterDto c1, Chapter c2) => c1.ToModel().Equals(c2); public static bool Equals(this ChapterDto c1, ChapterDto c2) => c1.ToModel().Equals(c2.ToModel()); public static bool Equals(this ChapterDto c1, ChapterEntity c2) => c1.ToModel().Equals(c2.ToModel()); public static bool Equals(this ChapterEntity c1, Chapter c2) => c1.ToModel().Equals(c2); public static bool Equals(this ChapterEntity c1, ChapterDto c2) => c1.ToModel().Equals(c2.ToModel()); public static bool Equals(this ChapterEntity c1, ChapterEntity c2) => c1.ToModel().Equals(c2.ToModel()); /// /// equality protocol for a chapter /// /// the chapter /// an object /// true if the chapter and the object are sames, false otherwise public static bool Equals(this Chapter c, object? o) { if (o == null) return false; if (o.GetType() == typeof(Chapter)) return Equals(c, (Chapter)o); else if (o.GetType() == typeof(ChapterEntity)) return Equals(c, ((ChapterEntity)o)); else if (o.GetType() == typeof(ChapterDto)) return Equals(c, (ChapterDto)o); return false; } /// /// equality protocol for a chapter entity /// /// the chapter entity /// an object /// true if the chapter entity and the object are sames, false otherwise public static bool Equals(this ChapterEntity c, object? o) { if (o == null) return false; if (o.GetType() == typeof(Chapter)) return Equals(c, (Chapter)o); else if (o.GetType() == typeof(ChapterEntity)) return Equals(c, ((ChapterEntity)o)); else if (o.GetType() == typeof(ChapterDto)) return Equals(c, (ChapterDto)o); return false; } /// /// equality protocol for a chapter dto /// /// the chapter dto /// an object /// true if the chapter dto and the object are sames, false otherwise public static bool Equals(this ChapterDto c, object? o) { if (o == null) return false; if (o.GetType() == typeof(Chapter)) return Equals(c, (Chapter)o); else if (o.GetType() == typeof(ChapterEntity)) return Equals(c, ((ChapterEntity)o)); else if (o.GetType() == typeof(ChapterDto)) return Equals(c, (ChapterDto)o); return false; } } }