C#

Mutexes, Semaphores, Monitors, ReaderWriterLocks, AutoResetEvents & ManualResets oh my!

Mutexes, Semaphores, Monitors, ReaderWriterLocks, AutoResetEvents & ManualResets oh my! Up until a few years ago, mainstream programming for the most part did not involve asynchrony and/or parallelism except for the most performance demanding applications. In the multi core world we live in today async and parallel programming have become common place. Libraries such as TPL which provide first class framework level support and keywords like async/await built on TPL provide language support for ......

MEF 101 - Part2

This is part 2 of a 2 part series exploring the MEF.We covered some of the basics in Part 1 In this part we'll cover the following: Catalogs Recomposition Export Providers Catalogs: Catalogs provide one way for MEF to discover components that it can compose. In it's most basic form it contains a registration of types. A container by itself is just an empty repository. The catalog is the one that collects and returns ComposablePartDefinitions i.e (objects of types that you have registered with MEF) ......

MEF 101 - Part1

The Managed Extensibility framework or MEF is a framework released as part of .NET 4 and Silverlight that provides a mechanism to dynamically compose an application at runtime out of loosely coupled parts and provides a variety of mechanisms to discovering these parts. You can think of this as an implementation of a plugin framework that enables plugging in functionality into a container, based on information available at runtime. The most notable example of a MEF client is the Visual Studio 2010 ......

A simple Dynamic Proxy

Frameworks such as EF4 and MOQ do what most developers consider "dark magic". For instance in EF4, when you use a POCO for an entity you can opt-in to get behaviors such as "lazy-loading" and "change tracking" at runtime merely by ensuring that your type has the following characteristics: The class must be public and not sealed. The class must have a public or protected parameter-less constructor. The class must have public or protected properties Adhere to this and your type is magically endowed ......

T4 template consuming a WCF service

It’s been a while since I’ve blogged since things have been crazy busy. I finally decided to kick myself and do a post so without further ado, the problem at hand: A co-worker of mine asked me whether its possible to dynamically create a type from some data he is receiving from a WCF service (over HTTP). My natural response was, why would you need to do that since Visual Studio creates a proxy from the metadata exposed by the service and the return types would typically be DataContracts. Well, it ......

Why no Anonymous Iterators in C#

Yes I’m a language geek and yes the awesome Eric Lippert is my idol when it comes to anything C# and the CLR, I’ve been following his blog for a long time and recently Eric wrote an awesome series on the how and why of Iterator Blocks. The entire series is very well worth a read, but then again I’d read almost anything Eric writes from why the sky is blue to why the Falkirk Wheel has horns. I had posed a question on this post about why there are no anonymous iterators in C#. Eric provided a very ......

Creating Higher Level APIs with TPL

The BCL has a bunch of APIs which provide asynchronous versions of operations in the form of either Beginxxx/Endxxx or XyzCompleted, the former known as Asynchronous Programming Model(APM) and the latter known as Event Based Asynchronous Pattern (EAP). TPL provides a nice abstraction for performing asynchronous operations as Tasks using delegates, but what can you do for working with these existing BCL classes which already have an async version? Here is an example. Let's say we need to download ......

CLR via C# 3rd Edition

Some book news…Jeff Richter is one of my favorite authors and I’ll read just about anything he writes. His last book CLR via C#, 2nd Edition left me wanting more, luckily Jeff has signed into a contract for writing a 3rd edition of the book updated for C#3.0/4.0 and .NET 4.0 ......

Retrieving Added Entities from the ObjectStateManager to avoid duplication

Let's say we neede to process a bunch of entities coming from an XML file, create EDM entites for each XML element and eventually persist them. Very trivial to accomplish using a little bit of LINQ. <Roles> <Role name="Admin"/> <Role name="Reader"/> <Role name="Writer"/> </Roles> var doc = XDocument.Load("Roles.xml"); var roles = doc.Element("Roles").Elemen... => new Role { RoleName = (string)x.Attribute("name") }); PermissionsAPIContext context = ......

Inside TPL video

No code today. Instead, here is a link to a great video from Channel9 I found, which takes a deep dive into the underlying mechanics of how TPL does what it does and some of the enhancements that the thread pool has undergone. Albeit a year old, this video goes into details of the work stealing algorithm and local thread queues that helps increase throughput. From the perspective of being a consumer of TPL, you can be blissfully ignorant of what happens behind the scenes, but understanding the mechanics ......

Full C# Archive