TEF Open Source Project Released

I’ve always liked the idea of MEF. The ability to mark a class as exportable, then very easily consume it in your application is a very nice feature. However, MEF has always been instance-based, meaning you had to have an instance of a class to register. This works OK in some instances, but not in others. For example, in some instances, an instance of a class in ASP.NET can be problematic, especially if it’s tied to a specific HTTP context.

Because I needed type-specific support, without having to instantiate a specific instance, TEF was born, a simplistic implementation for registering your types. Registering your types is as simple as adding an [ExportType] to your class, or adding an assembly attribute [ExportDependency].

[ExportType(typeof(ISomeService))]
public class MySomeService : ISomeService
{
}

OR:

[assembly:ExportDependency(typeof(ISomeService), typeof(MySomeService))]

To extract the types out of the assembly, you can target a specific app domain, assembly or collection of types. For instance, the code below targets a specific app domain.

var importer = new TEFMetadataImporter();
//Pull any assembly dependencies marked with [ExportDependency]
var types = importer.GetAssemblyTypes(AppDomain.CurrentDomain).ToList();
var assemblyTypeCount = types.Count;

//Pull any type dependencies marked with [ExportType]
types.AddRange(importer.GetClassTypes(AppDomain.CurrentDomain));

The project was built for .NET projects, MonoTouch, and MonoDroid. You can grab the source code and a sample project for TEF on BitBucket. Note: for MonoTouch and MonoDroid, the project was built with Xamarin Studio, so the System DLL’s may not match the Visual Studio System DLL’s. It’s easy enough to grab the source code and replace them.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s