示例#1
0
 /// <summary>
 /// Determines whether this GraphicsCard object is equal to the GraphicsCard
 /// argument object. Both are deemed equal if they both have the same
 /// manufacturer and model. Once two GraphicsCards have been declared equal,
 /// then they are both assigned the same ID elsewhere in the application.
 /// </summary>
 /// <param name="obj">The object this one will be compared to.</param>
 /// <returns>True if the GraphicsCard object is equal to this, false otherwise.</returns>
 public override bool Equals(object obj)
 {
     if (obj is GraphicsCard)
     {
         GraphicsCard that = (GraphicsCard)obj;
         return(this.Manufacturer.Equals(that.Manufacturer) && this.Model.Equals(that.Model));
     }
     else
     {
         return(false);
     }
 }
        public static void Initialise(IApplicationBuilder app)
        {
            //Obtain application database context
            ApplicationDbContext context = app.ApplicationServices.GetRequiredService <ApplicationDbContext>();

            context.Database.Migrate();

            //Only perform seeding operation if no domain objects exist in the database
            if (!context.Computers.Any())
            {
                //Extract raw data rows from flat text file
                string[] rows = File.ReadAllLines("wwwroot/data/SeedData.txt");

                foreach (string row in rows)
                {
                    //Parse each column
                    string[] cells           = row.Split("\t".ToCharArray());
                    string   rawMemory       = cells[0];
                    string   rawStorage      = cells[1];
                    string   rawConnections  = cells[2];
                    string   rawGraphicsCard = cells[3];
                    string   rawWeight       = cells[4];
                    string   rawPower        = cells[5];
                    string   rawProcessor    = cells[6];

                    //Create processor object
                    Processor processor = new Processor
                    {
                        Manufacturer = rawProcessor.Split(' ')[0],
                        Model        = rawProcessor.Substring(rawProcessor.IndexOf(' ') + 1)
                    };
                    if (!SeedProcessors.Contains(processor))
                    {
                        SeedProcessors.Add(processor);
                    }

                    //Create graphics card object
                    GraphicsCard graphicsCard = new GraphicsCard
                    {
                        Manufacturer = rawGraphicsCard.Split(' ')[0],
                        Model        = rawGraphicsCard.Substring(rawGraphicsCard.IndexOf(' ') + 1)
                    };
                    if (!SeedGraphicsCards.Contains(graphicsCard))
                    {
                        SeedGraphicsCards.Add(graphicsCard);
                    }

                    //Construction connection objects and add to database context
                    string[] connectionInstances = rawConnections.Split(',').Select(s => s.Trim()).ToArray();
                    SeedPorts.AddRange(connectionInstances
                                       .Select(c => new Port {
                        Name = c.Split("x")[1].Trim()
                    })
                                       .Where(p => !SeedPorts.Contains(p)));

                    List <Connection> connections = connectionInstances
                                                    .Select(c => new Connection {
                        Quantity = int.Parse(c.Split("x")[0].Trim()),
                        Port     = SeedPorts.Where(p => c.Split("x")[1].Trim().Equals(p.Name)).FirstOrDefault()
                    }).ToList();

                    //Construct computer object and add to database context
                    Computer computer = new Computer
                    {
                        Memory          = CalculateMemory(rawMemory),
                        Power           = CalculatePower(rawPower),
                        StorageCapacity = CalculateStorageCapacity(rawStorage),
                        StorageType     = rawStorage.Split(' ')[2],
                        Weight          = CalculateWeight(rawWeight),
                        Processor       = SeedProcessors.Where(p => p.Equals(processor)).FirstOrDefault(),
                        GraphicsCard    = SeedGraphicsCards.Where(gc => gc.Equals(graphicsCard)).FirstOrDefault(),
                        Connections     = connections
                    };
                    context.Computers.Add(computer);
                }
                context.SaveChanges();
            }
        }