.net

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…

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.