Managing DbContext the right way with Entity Framework 6: an in-depth guide

UPDATE: the source code for DbContextScope is now available on GitHub: DbContextScope on GitHub. A bit of context This isn't the first post that has been written about managing the DbContext lifetime in Entity Framework-based applications. In fact, there is no shortage of articles discussing this topic. For many applications, the solutions presented in those articles (which generally revolve around using a DI container to inject DbContext instances with a PerWebRequest lifetime) will work just fine. They also have the merit of being very simple - at least at first sight. For certain types of applications however, the inherent limitations of these approaches pose problems. To the point that certain features become impossible to implement or require to resort to increasingly complex structures or increasingly ugly hacks to work around the way the DbContext instances are created and managed. Here is for example an overview of the real-world application that … [+]
Read more

From NHibernate to Entity Framework 6 - Part 1: the mapping story

This is the first part of an n-part series on using Entity Framework 6 coming from an NHibernate background . Although this series is primarily aimed at NHibernate veterans making the transition to Entity Framework 6, any developer new to EF will probably find most of this information helpful too. Introduction After having used NHibernate quite extensively for the past few years, I've just completed my first 6 months in the Entity Framework world. I had actually already given Entity Framework a try back in the EF4 days. I quickly abandonned it as it became clear very quickly that it wasn't ready for prime time. But I have to say that Entity Framework has come a long way since then. EF6 has become not just a usable but a very capable ORM. It's definitely now a very credible alternative to NHibernate. The transition from NH to EF6 was fairly smooth as … [+]
Read more

Generating HTML emails with RazorEngine - Part 04 - Taking a step back: behind the scenes of Razor and RazorEngine

This is the fourth part of a 10-part blog series. You'll find a list of all the posts in this series in the introductory post. Make sure to review the Before we start section in the introductory post. All the source code is on GitHub. Comment? Bug? Open an issue on GitHub. Before diving any deeper into RazorEngine, it's worth spending a few minutes getting a good grasp of what Razor and RazorEngine really are and of how they work under the hood. It will save a lot of time and frustration when trying to customize or understand RazorEngine's behaviour. This is a very quick and deliberately simplified overview of Razor. What is Razor? Razor is a language that lets you create document templates mixing static markup and code. Typically, the static markup is HTML and the code is C# or VB.NET. But it doesn't have to be. Razor … [+]
Read more

A tale of debugging - the LinkedIn API, .NET and HTTP protocol violations

In my last project, I was in charge of integrating the LinkedIn API into .NET 4 application. A few weeks after launch and during a client demo (of course), a request to the LinkedIn API failed with the following cryptic WebException: The server committed a protocol violation. Section=ResponseHeader Detail=CR must be followed by LF We had thouroughly tested the LinkedIn API over several months before launch, had a dedicated team of testers and had a full suite of automated integration tests against the API, yet had never seens this error before. In fact, I'd never seen this error before in my life - and I've implemented my fair share of third-party REST API integrations. Since we were investigating another issue at the time, we put that one down on a temporary network glitch and moved on... ...until both our QA team and our automated tests were able to … [+]
Read more

Generating HTML emails with RazorEngine - Part 03 - Caching, VS integration & namespace config

This is the third part of a 10-part blog series. You'll find a list of all the posts in this series in the introductory post. Make sure to review the Before we start section in the introductory post. All the source code is on GitHub. Comment? Bug? Open an issue on GitHub. Caching templates with RazorEngine When generating our email in the previous post, we happily ignored the cacheName parameter of the TemplateService.Parse() method. This was wrong. Not using RazorEngine's cache will result in both dreadful performances and memory leaks. Generating a document from a Razor template involves some very expensive operations. We'll cover this in more details in the next post but in a nutshell, TemplateService.Parse() will: Parse your template and generate the source code of a class that can generate the final document. Compile that class on the fly into its own assembly. Load that newly … [+]
Read more