Updating disconnected Entity Framework child collections

One pain point I have with Entity Framework is dealing with updating child collections on disconnected entities. The most common scenario I run into is in web APIs. I have a web page which allows a user to edit an entity which includes a child collection. New children can be added, existing children edited, and existing children deleted. All the actions are saved to the server at once. When I POST this to my API, I end up writing a lot of boilerplate to figure out what changed. To prevent that, I came up with this method. A sample usage is below. This is for EF 6.1.

Generating SQL from expression trees, Part 2

As promised, here is a much improved version of the where clause builder from my last post. This version generates parameterized queries so it isn’t vulnerable to SQL injection. Using parameters also allowed me to simply the logic since I don’t need to worry about stringifying the values or using “IS” instead of “=” for null checking.

I moved all the string concatenation into a separate class called WherePart. These objects are composable in a structure similar to the source expression tree. Extracting this class is my favorite part of the refactoring.

I’m still not happy with how I’m handling the LIKE queries. I have to pass a prefix and postfix parameter down to the next level of recursion which clutters up the method signature. It might be better to just build the string in place.

Continue reading

Generating SQL from expression trees

Is this further down the rabbit hole than IL generation? I’m not sure but I went there.

(Note: This was an experiment. It doesn’t generate safe SQL. I’ll follow up with a better version.)

An expression tree is an abstract representation of code as data. In .NET they are primarily used for LINQ-style code. In C#, lambda expressions can be decomposed into expression trees. Here is an example of a lambda and it’s expression tree:

x => x.PosId == 1 && x.Name == "Main"

ExpressionTree

There are five key types of expression tree nodes.

UnaryExpression: An operation with a single operand such as negation.

BinaryExpression: An operation with two operands such as addition or && or ||.

MemberExpression: Accessing a property, field, or method of an object or a variable. (Variable references in lambda expressions are implemented as fields on a class generated by the compiler.)

ConstantExpression: A node that is a constant value.

ParameterExpression: An input to a lambda function.

The following code recursively walks an expression tree and generates the equivalent where clause in SQL, for sufficiently simple expressions. One of the areas that was a bit tricky is SQL’s handling of NULL. I have to check the right side of a binary expression for NULL so I can generate “x IS NULL” instead of “x = NULL”. I used parentheses liberally so ease composing the expressions. Handling negation was done naively. It could be cleaned up by propagating the negation into the child node.

Continue reading

Playing with IL generation

This week I started learning how to generate CIL (Common Intermediate Language, the .NET runtime’s equivalent of assembly). The .NET classes for doing this looked a bit intimidating so I chose to try out FluentIL first. FluentIL is a helper library to simplify generating IL. I’m not sure if it helped or hurt–I had to mentally translate all the online samples I looked at into FluentIL and I also found an opcode (castclass) which wasn’t implemented in the fluent helpers.

I used the cheater method to write the example below. First I wrote a strongly typed C# method which did exactly what I wanted the IL to do and built it in release mode. Then I used dotPeek to view the generated IL and wrote an equivalent generic version.

One of the difficulties of writing IL is debugging it. If the program isn’t perfectly valid, the runtime gives you a very generic invalid program error. Also, if you have a runtime type violation in the IL you get an exception saying the operation could make the runtime unstable. There is no stack trace to help you find the error.

This code provides a way to directly set properties on an object without using reflection. It would be useful if you have an object but only know the type at runtime. This isn’t production ready code.
Continue reading

Compiling legacy TypeScript

Last week I installed Visual Studio 2015. This week I was unable to use Visual Studio 2013 to rebuild a legacy app that included TypeScript 1.0 code originally developed in Visual Studio 2012. Here’s how I sorted it out.

VS2015 installed TypeScript 1.7. I won’t be using TypeScript going forward so I uninstalled the TypeScript 1.7 compiler in Programs & Features.

This is not enough though. Now I received an “unknown option: ‘noEmitOnError'” during building. TypeScript compilers (as of present) share a common MSBuild target file which contains the 1.7 options even after you uninstall. The file is at C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\TypeScript\Microsoft.TypeScript.jsproj.targets (Adjust the v12.0 as approriate for your version of VS). I ended up overwriting the entire TypeScript folder (keep a backup!) with a copy from another developer who had not yet upgraded. However I did discover that you can copy the targets file from an older version folder as well. (The one from v11.0 worked for me too, but it is slightly different.)

If you need to compile multiple versions of TypeScript, I think you’ll have to make a copy of the TypeScript folder and edit your csproj file to point to the correct one. The relevant line is:

<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\TypeScript\Microsoft.TypeScript.targets" />

Was $30,000 a good asking price?

Yesterday the internet broke. Well at least a lot of javascript builds did. But I don’t want to focus on that part of the story.

In the published conversation, Kik asked if they could compensate Azer for transferring control of the kik project name on npm. Azer asked for $30,000. Kik walked away from the negotiation and asked npm to transfer the name since they owned the trademark to Kik.

First, I don’t believe Azer was violating Kik’s trademark. Kik’s trademark is for mobile software, instant messaging, and a website. Azer’s kik project is a software bootstrapping tool. I’m not a lawyer, but I don’t see how Azer’s use of the name is trademark infringement. There are many other companies that use the Kik name. The USPTO’s TESS system has 15 records for Kik. On this basis, it’s fair to say that Kik should compensate Azer for the name.

Now, was Azer’s asking price of $30,000 fair? Kik obviously did not think so. To an individual developer this is probably a sizable amount of money. But what is the value to Kik? While they don’t exactly say what software tool they are releasing, since it’s being released on npm it’s likely a tool for extending their service. The more extensions there are to Kik, the more reasons users will find to become engaged with it. Growing the user base has a huge return in terms of valuation potential for a VC-backed startup. Kik’s argument was that if they had to compete with an unrelated kik package, it could confuse developers (likely reducing adoption). So my question is: what is it worth to reduce that confusion? I don’t know Kik’s financials (they’ve publicly raised $120M on a $1B valuation), but I do know that software development projects aren’t cheap. At a company Kik’s size, anything worth releasing is probably costing tens, if not hundreds, of thousands in developer time, plus management, marketing, and overhead. If reducing developer confusion could lead to higher user numbers and thus lead to higher valuation, I could probably be convinced $30,000 was a fair price to pay. And if it isn’t, they could always counter.

Finally, if Kik’s goal was to get this project name for cheap, it was critical for them to set an anchor price early. By asking Azer to give the first offer they lost control of the negotiation.

You should… A lesson from mentoring

When I started mentoring junior developers, I had a bad habit of saying things like “you should…<my opinion>”. When giving directions, it’s direct to the point, however I don’t think phrasing directions this way helps new developers learn as quickly as guiding them in a way that requires them to make decisions themselves. Today I find myself replacing those “you should” moments with phrases more like “Have you considered…?” or “In my experience…” or pointing them towards an article or specific MSDN page. Sometimes they’ll be able to explain what they want to do but don’t know the technical term for it. By supplying one it’s much easier for them to google for more resources. Once they have some good context on the problem, I’ve found that simply following a Socratic line of questioning about the details of the problem is very effective. Usually they interrupt with me with “Oh I have an idea now!”

Searching for a word in Visual Studio

I was trying to find all usages of a variable in a SQL script today, however the script contained many similarly named variables. Simply searching for @Foo would also find @FooBar and @FooCat which was cluttering up the search results. To get around this I used the regex search feature in Visual Studio. In the find usages window, check the “Use Regular Expressions” checkbox and then include the word boundary escape code (\b) in your search. I searched for @Foo\b which will only return @Foo and even find it in cases like @Foo<space>, @Foo) and @Foo=.

SQL INSERT vs UPDATE

I’m curious as to why the insert and update commands in SQL have different syntax. So far I have been unable to find any satisfactory explanation. I personally have a gripe with the insert syntax in that the column names are separated from the inserted value. In wide tables this makes it difficult to alter existing stored procedures since you have to manually count fields or come up with a commenting scheme. Even then, you aren’t guaranteed you’ll find a bug at runtime.

I propose that in addition to the normal syntax

INSERT INTO Foo (ColumnA, ColumnB, ColumnC)
VALUES ('First', 'Second', 'Third')

SQL also support an alternate syntax such as:

INSERT INTO Foo
SET
ColumnA = 'First',
ColumnB = 'Second',
ColumnC = 'Third'

This syntax is easily readable and much more maintainable than the existing syntax.

MySQL has already adopted this syntax but unfortunately SQL Server has not. In the meantime I’ll continue commenting my large inserts like this:

INSERT INTO Foo (ColumnA, ColumnB, ColumnC)
VALUES (
'First',  -- ColumnA
'Second', -- ColumnB
'Third'   -- ColumnC
)

I wish C# had this Java feature

In Java there is a feature called the Catch or Specify Requirement. This feature requires that each method specify what exceptions it may throw. Any caller to that method must either include a catch block for the exceptions or specify that it can pass the exceptions up the call chain. A Java application will not compile if all exceptions are not handled.

C# does not have any similar feature. When working with unfamiliar system classes, I typically refer to MSDN to learn what exceptions they may raise. Unfortunately if I miss something, the C# compiler does not provide any warning.

C# does provide the ability to document the exceptions using the <exception> tag, but this is only used to generate documentation and provide intellisense in Visual Studio. There is a ReSharper add-in called Exceptional which warns you about uncaught exceptions. I have not tried it, but according to feedback on Stack Overflow, it generates too much noise to be useful.

My general practice is to use generic exception handling blocks at the highest level of my code so errors can at least be caught, logged, and feedback given to the user.