Blog

Avoid wrongly scoped injections with a custom Ninject kernel

October 22, 2015, (updated on February 19, 2016), Software Development

I’m currently working on a large project where different teams implement assemblies which are consumed by other teams. We use Ninject for dependency injection: Each assembly has a Ninject module class which registers the available interface-to-implemenation bindings. At the moment, the consumers of these assemblies just know what module classes to load and what interfaces to inject into own objects like ASP.NET controllers or own service objects, but nothing about the scope of the injected objects. Recently we had some nasty runtime errors, because a consumer of one of these assemblies injected a per-request scoped object into a singleton-scoped object. […] Read more...

Tags: , , , ,

Getting started with Entity Framework Code First Migrations

October 12, 2015, (updated on October 17, 2015), Software Development

The following post describes the steps to setup and use Entity Framework Code First Migrations. For more information read this Microsoft page. The sample application shown in this article can be found on GitHub. Initial setup Listed below are the steps to setup a new project with an Entity Framework context, an entity class and an initial migration: Create a new data access project (C# library project) in your solution Open the Package Manager Console in the Visual Studio menu Tools > NuGet Package Manager > Package Manager Console. In the console window, select the previously created data access project […] Read more...

Tags: , , , , , ,

How to implement an enum with string values in TypeScript

I currently work an a single-page application (SPA), where we use TypeScript to access web services. In some places, the sent and received data structures use enums to describe the range of allowed values. I tried to use the enums from TypeScript to access the web services in a strongly typed way: export interface PersonDto { language: Language; } export enum Language { English, German, French, Itialian } When serializing a PersonDto instance to JSON, the language field is converted to a number and not a string as required by the web service API: var person = <PersonDto> { language: […] Read more...

Tags:

Bug Fix: Code Contracts compile error in Visual Studio 2015 and Windows 10

September 16, 2015, (updated on October 2, 2015), 1 comment, Software Development

Recently, I had the following compile error in a fresh Visual Studio 2015 installation on Windows 10. The project in question had Code Contracts enabled. Read more... Read more...

Tags: , , , ,

Avoiding DLL file locks when using .NET reflection in external assemblies

September 6, 2015, (updated on January 7, 2016), Software Development

For one of my projects, I had to load external assemblies and query the exported types (i.e. the public classes) using reflection. Everything worked fine, except that the .DLL files were being locked until the application was terminated. The problem is, that once you load an assembly into the main AppDomain using Assembly.LoadFrom(), it stays loaded and you cannot unload the assembly. Also, loading an assembly into the AppDomain where your other code is running might have unexpected side effects. To avoid all these problems, we need to do the work in a secondary, isolated AppDomain. An AppDomain is like […] Read more...

Tags: , , , , ,

Precompile ASP.NET MVC Razor views in Release mode to find errors at compile time

A problem we often face when developing ASP.NET MVC applications is that errors in Razor views are only detected at runtime while browsing the site. This is because the Razor views are not compiled until they are requested by the browser. To avoid this problem, you can prebuild the views by adding the MvcBuildViews tag to your .csproj file: <?xml version="1.0" encoding="utf-8"?> <Project ...> ... <PropertyGroup> ... <MvcBuildViews>true</MvcBuildViews> ... There are two problems with this approach: The build process takes much longer You cannot change the views when debugging the running application To avoid these problems and still be able […] Read more...

Tags: , , , ,

Post-process the HTML output of an action in ASP.NET MVC

May 5, 2015, (updated on December 16, 2015), 3 comments, Software Development

This article shows how to implement a post-processor for an ASP.NET MVC application, which transforms the output of an controller action before it is transmitted to the client. The obvious way to do this is by implementing a custom action filter. First, we implement the base filter class: public abstract class OutputProcessorActionFilterAttribute : ActionFilterAttribute { protected abstract string Process(string data); public override void OnResultExecuted(ResultExecutedContext filterContext) { var response = filterContext.HttpContext.Response; response.Filter = new OutputProcessorStream(response.Filter, Process); } } As you can see in the OnResultExecuted method, the response’s current Filter property is wrapped by a new stream implementation. Using this interceptor […] Read more...

Tags: , , , ,

Manually triggered computed observables in KnockoutJS

April 3, 2015, (updated on April 21, 2015), Software Development

In KnockoutJS I sometimes need to create a computed observable which does not automatically detect the dependencies and which has to be triggered manually. This may be required if the computation is very complex and should only run in certain situations. To implement this special type of computable, extend the ko object with a new manualComputed method: ko.manualComputed = function (func) { var changes = 0; var dummy = ko.observable(); function observable() { dummy(); var temp = ko.computed(func); var result = temp.peek(); temp.dispose(); return result; } ko.subscribable.call(observable); observable.update = function () { dummy(++changes); }; ko.utils.extend(observable, ko.observable["fn"]); return observable; }; After […] Read more...

Tags: ,

ASP.NET MVC: How to implement an edit form for an entity with a sortable child collection

March 30, 2015, (updated on June 10, 2015), 28 comments, Software Development

This article shows how to implement the edit form for an entity with a collection property. As a show case, we will implement the edit form for a person entity with multiple addresses. The following diagram shows the UML diagram of the two entity classes: The final edit form for a person entity should look like this: The goal is to implement this form so that the address items are sortable and validated directly on the page. The article describes how to implement the described edit form in a simple way with as little as possible lines of code. The […] Read more...

Tags: , , , ,

My most used Visual Studio and ReSharper extensions

March 12, 2015, (updated on August 25, 2015), 5 comments, Software Development

This article describes some of the Visual Studio and ReSharper extensions and development tools I’m using to enhance my daily C#/.NET development productivity. ReSharper extensions In my opinion, ReSharper is a must have extension for Visual Studio which provides lots of improvements, for example better refactorings, an improved code snippet and template engine, additional static code analysis and much more. My personal configuration: Use camel humps instead of whole word when navigating or deleting: ReSharper > Options… > Environment > Editor > Editor behavior > Use CamelHumps Copy XML documentation from interface when generating interface methods: ReSharper > Options… > […] Read more...

Tags: , , , , , ,