From 2a02d8d4ecde5a2c39bd1ab441e0a8e8530fb829 Mon Sep 17 00:00:00 2001 From: Rainer Stropek Date: Tue, 20 Sep 2022 08:03:32 +0200 Subject: [PATCH] Refactoring (records) --- .../Controllers/ConnectionsController.cs | 9 ++--- StarshipTraveler.Api/Data/SampleData.cs | 20 +++++------ StarshipTraveler.Client/Data/StarshipApi.cs | 4 +-- StarshipTraveler.Model/Model.cs | 33 +++---------------- StarshipTraveler.Model/StarshipApi.cs | 12 +++---- .../StarshipTraveler.Model.csproj | 7 ++-- .../Data/StarshipApi.cs | 6 ++-- 7 files changed, 29 insertions(+), 62 deletions(-) diff --git a/StarshipTraveler.Api/Controllers/ConnectionsController.cs b/StarshipTraveler.Api/Controllers/ConnectionsController.cs index 33704b2..9cb36d9 100644 --- a/StarshipTraveler.Api/Controllers/ConnectionsController.cs +++ b/StarshipTraveler.Api/Controllers/ConnectionsController.cs @@ -12,11 +12,6 @@ public class ConnectionsController : ControllerBase { [HttpGet] public IEnumerable Connections() => - SampleData.Connections.Union(SampleData.Connections.Select(c => new Connection - { - From = c.To, - To = c.From, - Distance = c.Distance, - Price = c.Price - })).ToArray(); + SampleData.Connections.Union(SampleData.Connections.Select( + c => new Connection(c.To, c.From, c.Distance, c.Price))).ToArray(); } diff --git a/StarshipTraveler.Api/Data/SampleData.cs b/StarshipTraveler.Api/Data/SampleData.cs index 13f781a..69db096 100644 --- a/StarshipTraveler.Api/Data/SampleData.cs +++ b/StarshipTraveler.Api/Data/SampleData.cs @@ -33,15 +33,15 @@ public static class SampleData public static Connection[] Connections { get; } = new[] { - new Connection { From = "Decapod 10", To = "Earth", Distance = 1000, Price = 100m }, - new Connection { From = "Cybertron", To = "Earth", Distance = 100, Price = 375.75m }, - new Connection { From = "Krypton", To = "Earth", Distance = 12300, Price = 7499.99m }, - new Connection { From = "Tatooine", To = "Vulcan", Distance = 5200, Price = 89.90m }, - new Connection { From = "Earth", To = "Pandora", Distance = 3750, Price = 301.01m }, - new Connection { From = "Earth", To = "Magrathea", Distance = 4242, Price = 4242.42m }, - new Connection { From = "Arrakis", To = "Decapod 10", Distance = 7300, Price = 1349.90m }, - new Connection { From = "Arrakis", To = "Pandora", Distance = 7300, Price = 1349.90m }, - new Connection { From = "Pandora", To = "Magrathea", Distance = 4242, Price = 4242.42m }, - new Connection { From = "Tatooine", To = "Krypton", Distance = 1234, Price = 1345.67m }, + new Connection("Decapod 10", "Earth", 1000, 100m), + new Connection("Cybertron", "Earth", 100, 375.75m), + new Connection("Krypton", "Earth", 12300, 7499.99m), + new Connection("Tatooine", "Vulcan", 5200, 89.90m), + new Connection("Earth", "Pandora", 3750, 301.01m), + new Connection("Earth", "Magrathea", 4242, 4242.42m), + new Connection("Arrakis", "Decapod 10", 7300, 1349.90m), + new Connection("Arrakis", "Pandora", 7300, 1349.90m), + new Connection("Pandora", "Magrathea", 4242, 4242.42m), + new Connection("Tatooine", "Krypton", 1234, 1345.67m), }; } diff --git a/StarshipTraveler.Client/Data/StarshipApi.cs b/StarshipTraveler.Client/Data/StarshipApi.cs index 2a8fc84..dca76f2 100644 --- a/StarshipTraveler.Client/Data/StarshipApi.cs +++ b/StarshipTraveler.Client/Data/StarshipApi.cs @@ -8,12 +8,12 @@ namespace StartshipTraveler.Client.Data; public class StarshipApi : StarshipApiBase { public StarshipApi(HttpClient client, IWebAssemblyHostEnvironment hostEnvironment) + : base(client) { // Note Blazor environment. For more details see // https://docs.microsoft.com/en-us/aspnet/core/blazor/fundamentals/environments?view=aspnetcore-5.0 var apiUrl = hostEnvironment.IsDevelopment() ? "http://localhost:5000/api/" : "https://starshipapi.azurewebsites.net/api/"; - client.BaseAddress = new Uri(apiUrl); - Client = client; + Client.BaseAddress = new Uri(apiUrl); } } diff --git a/StarshipTraveler.Model/Model.cs b/StarshipTraveler.Model/Model.cs index 4591626..5cbace0 100644 --- a/StarshipTraveler.Model/Model.cs +++ b/StarshipTraveler.Model/Model.cs @@ -1,27 +1,11 @@ -using System; -using System.ComponentModel.DataAnnotations; -using System.Linq; +using System.ComponentModel.DataAnnotations; namespace StarshipTraveler.Model; -public class Base -{ - public string? Name { get; set; } - public string? Image { get; set; } - - public Base() { } - - public Base(string name, string image) - { - Name = name; - Image = image; - } -} +public record Base(string Name, string Image); -public class GraphBase : Base +public record GraphBase : Base { - public GraphBase() { } - public GraphBase(uint graphID, string name, string image) : base(name, image) { GraphID = graphID; @@ -62,13 +46,4 @@ public enum TimeRelation Upcoming } -public class Connection : IEquatable -{ - public string? From { get; set; } - public string? To { get; set; } - public int Distance { get; set; } - public decimal Price { get; set; } - - public bool Equals(Connection other) => - From == other.From && To == other.To && Distance == other.Distance && Price == other.Price; -} +public record Connection(string From, string To, int Distance, decimal Price); diff --git a/StarshipTraveler.Model/StarshipApi.cs b/StarshipTraveler.Model/StarshipApi.cs index eaa7944..ce2bbf1 100644 --- a/StarshipTraveler.Model/StarshipApi.cs +++ b/StarshipTraveler.Model/StarshipApi.cs @@ -1,22 +1,20 @@ using StarshipTraveler.Model; -using System; -using System.Collections.Generic; -using System.Net.Http; using System.Text; using System.Text.Json; -using System.Threading.Tasks; namespace StartshipTraveler.Model; public abstract class StarshipApiBase : IStarshipApi { - private HttpClient? client; + private readonly HttpClient client; - protected HttpClient Client + public StarshipApiBase(HttpClient client) { - set { client = value; } + this.client = client; } + protected HttpClient Client => client; + public async Task GetBaseAsync(string baseId) { if (client == null) diff --git a/StarshipTraveler.Model/StarshipTraveler.Model.csproj b/StarshipTraveler.Model/StarshipTraveler.Model.csproj index d70d664..2163326 100644 --- a/StarshipTraveler.Model/StarshipTraveler.Model.csproj +++ b/StarshipTraveler.Model/StarshipTraveler.Model.csproj @@ -1,9 +1,10 @@  - netstandard2.1 - preview - enable + net7.0 + preview + enable + enable diff --git a/StarshipTraveler.ServerSide/Data/StarshipApi.cs b/StarshipTraveler.ServerSide/Data/StarshipApi.cs index 42bacc8..d03bc1e 100644 --- a/StarshipTraveler.ServerSide/Data/StarshipApi.cs +++ b/StarshipTraveler.ServerSide/Data/StarshipApi.cs @@ -5,11 +5,9 @@ namespace StarshipTraveler.ServerSide.Data; public class StarshipApi : StarshipApiBase { public StarshipApi(IHttpClientFactory factory, IHostEnvironment hostEnvironment) + : base(factory.CreateClient()) { var apiUrl = hostEnvironment.IsDevelopment() ? "http://localhost:5000/api/" : "https://starshipapi.azurewebsites.net/api/"; - - var client = factory.CreateClient(); - client.BaseAddress = new Uri(apiUrl); - Client = client; + Client.BaseAddress = new Uri(apiUrl); } }