Monday, May 26, 2014

WCF Gotchas: Connection Closed

Here is a quick video explaining 3 WCF gotchas which result in the same error message: the connection has been closed.

Sunday, October 6, 2013

How CallerMemberNameAttribute can help to remove magic strings

In this short video we explore how using extension methods and the CallerMemberNameAttribute can help to reduce duplicate code and remove magic strings from your code

Tuesday, August 13, 2013

Intro to MEF - Part 2

This is a short video Intro to MEF - Part 2 in which we examine the basics of using and AssemblyCatalog.

Friday, June 21, 2013

C# Gotcha: Deferred Execution & Method Groups


Here is a short video demonstrating a C# Gotcha when it comes to using Deferred Execution with Method Groups:

Friday, January 11, 2013

An Intro to MEF – Part 1


I’m a big fan of MEF (Managed Extensibility Framework). I think it is very easy to use and offers a lot of advantages when developing an application.  MEF was developed to be a framework to allow for extensibility in your applications.  Parts of Visual Studio (including the Entity Framework Designer) run on MEF as does the Extension Manager.  That being said, it also works very well as a Dependency Injection container. My goal is not to compare MEF to other containers (ie. Unity, Windsor Castle, NInject, etc) but to show you how to use MEF as a DI container.

As usual, you can view/download the code on GitHub.

For Part 1 we will create a simple console application that will log a message.  However, we will use MEF as a DI container to supply the ILogger implementation.  Remember, this is meant to be a very simple example.  It is definitely not meant to demonstrate how MEF should be used.

Monday, June 25, 2012

EF Entity Name Scrubbing - Part 1

Recently I've been working with a database that has some unusual naming conventions on its tables.  The naming convention consists of [(schema)_(tableName)] where 'schema' is used in place of an actual database schema.  An example is HR_Contact.  I'm not going to go into why it was done but rather what issues this presents when using the Entity Framework designer.

Here is the database that we will be working with:

As you can see, we have two HR_ tables and two IT_ tables.  When using the Entity Framework Designer several issues arise:

1. The Entities are imported with these names
2. Navigation Properties and References are created using these names
3. The Pluralization service can have issues figuring out what the appropriate pluralization would be

When you are dealing with such a small number of tables, this may not be a big deal.  But if you're dealing with 25, 50, even 75+ tables this can prove to be a real PITA.  This was such a problem that the owner of the database decided against using EF as it was too time consuming to rename everything.  Following you will see how to create an EF Designer Plugin that will do all of the scrubbing for you.

// NOTE : Before we get started, be sure that you have the Visual Studio SDK installed.  I'll be using Visual Studio 2012 RC so I'll be installing the SDK from here.

Let's begin by creating a new C# Project called EFEntityNameScrubberExtension:

Delete the default Class1.cs file and add the following assembly references:

  1. Microsoft.Data.Entity.Design.Extensibility
  2. System.Data.Entity.Design
  3. System.ComponentModel.Composition
Now create a new class called EFEntityNameScrubber and inherit from IModelGenerationExtension.  Additionally we will add two attributes to the class that will expose EFEntityNameScrubber as an IModelGenerationExtension plugin for MEF.  Your class should look like the following:

Next we will create a new VSIX Project called EFEntityNameVsix:

When the project is created a wizard will be displayed.  On the Metadata tab enter your name in the Author text box:

Under the "Install Targets" tab, add the Visual Studio versions that you will support.

Finally, under the "Assets" tab, add the EFEntityNameScrubberExtension project as a MefComponent:

Now that we have our VSIX project created and configured, set it as the Startup Project and add a few break points into the EntityNameScrubber class methods.  Once you hit F5 an Experimental Instance of Visual Studio will start up.  This can take a few minutes if it is the first time that you have used the Experimental Instance (EI).

Once the EI has started, create a new Console Application and add an Entity Data Model.  Choose the "Generate from Database" option and click Next.  Select the Sandbox database (or any database with the table prefix that we will try to create.  For now, simply add a single table to the model:

When you click "Finish", the break point within the OnAfterModelGenerated method should be hit:

In Part 2 we will dig into the meat of the extension!

You can download the source on GitHub here.

Saturday, June 23, 2012

GetCustomAttributes Sucks

Ok so maybe the title of this post is a little harsh.  I don't really have anything against the MemberInfo.GetCustomAttributes.  I just don't like writing code like the following:

I'd prefer to do something like this:

Here is the source for the GetAttribute extension method:

You can download the full source (with tests) on GitHub here.