jeudi 9 février 2017

Looking for elegant / efficient solution for integer keyed collection of models

The "AddToLeg" method seems long winded is there a better pattern or more efficient way to implement this? I thought about using a dictionary but I want the key to remain an integer. I'm pretty new to linq / generics so I maybe missing something more obvious. When I've looked through documentation there aren't really any examples that match my scenario.

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    public class Program
    {
        public static void Main()
        {
            var model = new TrainspotterItenaryViewModel();

            var manchester = new Station() { Name = "Manchester", ExpectedTime = "13:30" };
            var leeds = new Station() { Name = "Leeds", ExpectedTime = "15:00" };
            var york = new Station() { Name = "York", ExpectedTime = "15:30" };
            var london = new Station() { Name = "London", ExpectedTime = "21:00" };

            model.AddToLeg(1, manchester);
            model.AddToLeg(1, leeds);

            model.AddToLeg(2, leeds);
            model.AddToLeg(2, london);
            model.AddToLeg(1, york); //another destination added to leg 1

            //any number of legs can be added...
            model.AddToLeg(3, manchester);

            //show results contents
            for(var i=1; i <= model.Legs.Count; i++)
            {
                var displayLeg = model.Legs.Single(x=>x.LegNumber==i);
                foreach(var station in displayLeg.Stations){
                    string output = $"leg: {displayLeg.LegNumber} station: {station.Name}, expected:{station.ExpectedTime}";
                        Console.WriteLine(output);  
                }
            }
        }
    }

    public class TrainspotterItenaryViewModel
    {
        public List<Leg> Legs { get; set; }

        public void AddToLeg(int legNumber, Station station)
        {
            if (Legs == null)
            {
                Legs = new List<Leg>();
            }

            var legCount = Legs.Count(x => x.LegNumber == legNumber);
            if (legCount == 0)
            {
                var leg = new Leg
                {
                    LegNumber = legNumber,
                    Stations = new List<Station> {station}
                };

                Legs.Add(leg);

                Console.WriteLine($"Leg {leg.LegNumber} Not Found- Added new leg and {station.Name}");
            }
            else
            {
                foreach (var leg in Legs)
                {
                    if (leg.LegNumber == legNumber)
                    {
                        leg.Stations.Add(station);
                        Console.WriteLine($"Leg {legNumber} Found- adding {station.Name}");
                    }
                }
            }
        }
    }

    public class Leg
    {
        public int LegNumber { get; set; }
        public List<Station> Stations { get; set; }
    }

    public class Station
    {
        public string Name { get; set; }
        public string ExpectedTime { get; set; }
    }
}

Aucun commentaire:

Enregistrer un commentaire