Posts

Showing posts from June, 2013

EF Code First: seeding with foreign keys

This is just a quick tip, more for future reference for myself than anything else. When using Entity Framework Code First, one of the things you might want to do while developing the application is to seed your database with some test data. This can be something as easy as: protected override void Seed(InventoryDB context) { context.Products.AddOrUpdate( p => p.Name, new Product { Name = "Hammer", SalePrice = 11.99m, }, new Product { Name = "Nail Pack x100", SalePrice = 0.05m, }, new Product { Name = "Saw", SalePrice = 19.99m, }, new Product { Name = "Toolkit", SalePrice = 39.99m, } ); //... } (I am using the AddOrUpdate method so the code detects whether those products already exist and doesn't add them again if they do.) The Product class is quite simple and – most importantly – doesn't depend on any other: public class Product { public int Id { g

Comparing Excel files, take two

Image
I already wrote this project once, on GitHub , but I can't say that I like the result. I wrote that code without TDD, mostly to see if I can still work in the "normal" fashion. I can, but I had a bug that took me a while to even discover and then was difficult to write tests for. I also hate the temporal dependency in that design (if you don't call the SortBy method first, the ExcludeRecords method will return incorrect results). Anyway; here's attempt number two, writing that code using TDD. Structure I start by creating my usual structure for projects of this type - a library for the logic, a console application for the main program and a test project for tests. I also use the Enable NuGet Package Restore option (right-click on the solution node) to add the three NuGet files that will allow someone to automatically download all the packages I'm referencing without wasting space in the source code repository. (Right now I've only added the Moq pa

Has to

One of the things I've come to hate when it comes to jobs is when the boss says "it has to be done by …". Whenever I hear that, I see red. Seriously. What does that even mean? The world will end if that task is not done in time? Global warming, zombie invasion, what? As far as I can tell, what it actually means is "I / someone made a booboo and promised the customer that it will be done by … Can you save my / their ass?" Now, this makes sense. A mistake was made ( not by me) and I am being asked to fix it. The mistake might actually be costly - the company might lose a hundred thousand dollars if the task is not done on time. Which leads me to the second problem: why is it that, when someone else makes the mistake and I am supposed to fix it, I never get any of the benefits? Why is this problem never put as "I've made a mistake that is going to cost the company $100K unless you finish the job by Friday. However, if you do, I'll give you a $30K

Fail-over algorithm

oDesk and similar sites are an interesting source of programming ideas - there are a couple of job descriptions I've seen that I have found interesting. One of them went something like this (paraphrasing): I need to continuously get data from a site and write it to a database. They have a main API that I coded for, but it fails sometimes. There is a secondary API and I coded for that too, but the first one fails unexpectedly and I haven't yet figured out all cases. Plus, the secondary one can fail too, I want to be informed if that happens. Expressing this requirement in a different way, and assuming that the "want to be informed" part can also fail, the algorithm would look something like this: try the first API if that fails, log and try the second API if that fails, log and inform the client if that fails, log and write to an alert file if that fails… do nothing, it's more important for the program to keep running "Challenge accepted," as th