Auto-populating a field in an EF Core entity

I am work­ing on the back end for a web-based SAAS appli­ca­tion, writ­ten in C#, and using .NET Core 2.1.  The data­base is most­ly han­dled by Entity Framework with cus­tom code that sup­ports the archi­tec­ture as a whole.

I had the sim­ple require­ment of pop­u­lat­ing a col­umn in our data­base with the “own­er” (or cre­ator) of the record.  The own­er is an appli­ca­tion user, and it is com­plete­ly depen­dent on the con­text of the cur­rent request. I want­ed to make this require­ment trans­par­ent to the rest of the application.

Continue read­ing…

Challenge accepted!

// You are building an educational website and want to create a simple
// calculator for students to use. The calculator will only allow
// addition and subtraction of positive integers.
//
// We also want to allow parentheses in our input. Given an expression
// string using the "+", "-", "(", and ")" operators like "5+(16-2)",
// write a function to parse the string and evaluate the result.

// Sample input:
//     expression1 = "5+16-((9-6)-(4-2))"
//     expression2 = "22+(2-4)"
 
// Sample output:
//     evaluate(expression1) => 20
//     evaluate(expression2) => 20

Recently, I did a live ses­sion where I was chal­lenged to imple­ment some code in a short peri­od of time.  I got flus­tered, and I diverged from my usu­al­ly strict prin­ci­ples of code eti­quette and wrote some messy code that nev­er end­ed up work­ing by the end of the ses­sion.  I had an ele­gant solu­tion in mind, but I was wor­ried about time and end­ed up with this mess.

Continue read­ing…

Hot-plugging USB into a Xen VM from a script

Xen Panda

I have been run­ning a hybrid Linux/Windows box for some time now, using Xen.  Xen is a bare-met­al hyper­vi­sor with many pow­er­ful fea­tures, includ­ing the abil­i­ty to map phys­i­cal hard­ware (say, a graph­ics card) to guest VMs.  I have strug­gled with get­ting USB to work seam­less­ly, how­ev­er.  My hard­ware pre­vents me from map­ping a phys­i­cal USB device to the guest, so I need­ed a dif­fer­ent solution.

Xen allows emu­lat­ed USB con­trollers to be mapped to the guest, and this works well.  One impor­tant fea­ture is miss­ing, how­ev­er.  It is not pos­si­ble to auto­mat­i­cal­ly con­nect a device to the guest when it is plugged in.

A lot of research and some div­ing into the code brought me to a solu­tion, how­ev­er.  Utilizing the udev inter­face in lin­ux and the low-lev­el hyper­vi­sor inter­faces, I can hot-plug devices at run-time.  This post describes how I got there.

The source code dis­cussed in this arti­cle can be found, in full, at https://github.com/stephen-czetty/xen-auto-usb.

Continue read­ing…

Abstract classes as action parameters in .NET Web API 4.0

(I had this sit­ting in draft form for years now, and I just dug it up. Most of it is pret­ty obso­lete, with .net core and all, but I’ll just leave this here.)

In a pre­vi­ous post, I dis­cussed a method for using abstract class­es in ASP.NET MVC 4.0. With it, I was able to clean up and hide the details of imple­men­ta­tion from much of my code. I assumed that it would be rel­a­tive­ly sim­ple to do the equiv­a­lent in Web API, but  I found that the task to be quite different.

The equiv­a­lent func­tion­al­i­ty requires use of asyn­chro­nous pro­gram­ming, code gen­er­a­tion, and some com­plex pro­cess­ing for han­dling things like IEnumerable. I also found some race con­di­tions that need­ed to be addressed.

Continue read­ing…

Using abstract classes as controller parameters in ASP.NET MVC4

As a part of the API for our prod­uct, we have the need to allow the UI to send us a par­cel of data, and we need to process it dif­fer­ent­ly based sole­ly on a sin­gle para­me­ter in the data.  We have decid­ed to cre­ate an abstract mod­el class to han­dle this, but MVC’s DefaultModelBinder does­n’t work out of the box on abstract class­es (or inter­faces).  There are many dis­cus­sions out there on this, and the accept­ed way to solve this prob­lem is to cre­ate a new ModelBinder class that has more knowl­edge of the inter­nals of your appli­ca­tion than the default does.  I thought about this some, and I decid­ed to try out a few tweaks to the process.

Continue read­ing…

Accepting multiple parameters the “RESTful” way using ASP .NET Web API

I came upon a require­ment at work to sup­port accept­ing mul­ti­ple ids in the Url of a “REST” request. I have no idea if this is actu­al­ly con­sid­ered RESTful or not, but the task was to for­mu­late the URL like so:

GET /FooResource/id1;id2;id3;id4/

The inten­tion was to be able to retrieve mul­ti­ple instances of FooResource with one request.

So, I came up with the fol­low­ing MVC ActionFilter to make my life a lit­tle bit easier:

Continue read­ing…

TDD and System. DirectoryServices. AccountManagement

So I am hav­ing a bit of a prob­lem here.  Working on a sim­ple library for a client to help them main­tain their Active Directory entries pro­gra­mat­i­cal­ly.  Started with my tests.  (I took this oppor­tu­ni­ty to spike Machine.Specifications, which I am find­ing that I quite like, but that’s for a dif­fer­ent post.)  Then researched into the APIs avail­able from Microsoft.  At first, I was quite excit­ed about the System.DirectoryServices.AccountManagement name­space added in the 3.5 frame­work, until I tried to inject its class­es into my own.

Now I’m run­ning myself in cir­cles, try­ing to fig­ure out how to test my code with­out cre­at­ing (or worse, delet­ing) accounts on my devel­op­ment box.

The team room and types of developers

The devel­op­ment team has recent­ly relin­quished our indi­vid­ual offices in favor of a team room where we are all work­ing togeth­er.  At first, I was skep­ti­cal that I would be able to keep a high lev­el of pro­duc­tiv­i­ty with the dis­trac­tions that a team room, by its very nature, cre­ates.  I could­n’t have been more wrong.  We quick­ly meshed as a team and have become notice­ably more pro­duc­tive.  (You can see the slope change on our burn­down; it is dramatic!)

One of the side-effects of work­ing in a big room with the rest of the team is that I’ve start­ed to notice the dif­fer­ent devel­op­ment and per­son­al­i­ty styles of the dif­fer­ent mem­bers.  I’ve come up with sev­er­al dif­fer­ent gen­er­al cat­e­gories.  Some peo­ple cross these bound­aries, depend­ing on the situation.

Continue read­ing…

Regretting a lazy design

Last night of the iter­a­tion, we are try­ing to cut a good build. This morn­ing’s scrum fea­tured a flus­tered QA engi­neer who was hav­ing trou­ble com­plet­ing her tests due to “cor­rupt­ed data”. Developers look at each oth­er and think, “rebuild the VM”, but it real­ly turned out to be faulty (and incon­sis­tent) assump­tions, on all of our parts.

The root cause of all our prob­lems tonight, though, dates back to the removal of a data­base con­straint that sup­port­ed an assump­tion that was not removed, about three iter­a­tions ago. (A ques­tion­able design deci­sion, IMHO.) This iter­a­tion, we tried to add a new con­straint that enforced our (now refined) assump­tion in a dif­fer­ent way. We failed. The assump­tion had been bro­ken in sev­er­al places, includ­ing our test data. Now, as we are try­ing to go gold, we have found that we need to revert the con­straints and deal with the messy data, just so that our test suite will con­tin­ue to work.

Now, in the­o­ry, in the field, nobody should be break­ing things. But we are still allow­ing them to, so some­body will, and it will look bad for us. We are stuck between a rock and a hard place now. Thirty min­utes spent com­ing up with a bet­ter design three iter­a­tions ago would have had us at home with our wives and a beer three or four hours ago.