Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create type at runtime that inherits an abstract class and implements an interface

Tags:

c#

Our architecture uses the Repository pattern extensively. We have an abstract base class for most of the repositories that implements some common functionality (e.g. get, load, list etc). There is a corresponding interface for this base class, IRepository, which defines the public methods of the abstract class. Most entities have a corresponding interface for the repository, e.g. the Foo entity has an IFooRepository, which in turn implements IRepository.

What I have just described is fairly typical, although I know it is not without problems. But anyway, it is what we have and we have to live with it.

One of my pet-hates with this type of architecture is having to define empty classes that simply inherit the base Repository class and do nothing else, e.g:

public class FooRepository : Repository, IFooRepository
{
}

One way of getting around this redundant code, is to allow our IOC framework to dynamically create these classes at runtime, so that I don't have to write them myself. If I can work out how to create these classes dynamically, then I already know where to plug them into NInject.

Does anyone know of a some code that can create such a class? Perhaps this can be done with a Proxy framework such as Castle?

like image 921
cbp Avatar asked Jan 29 '12 13:01

cbp


1 Answers

I was intrigued by the possibilities raised by your question so I did some investigation into how you would generate the repository proxies using either Castle’s DynamicProxy or the Reflection.Emit classes.

Using the following repository and domain classes (I expanded the scenario to allow repositories to return strongly type collections):

public interface IRepository<T>
{
    IEnumerable<T> All { get; }
}

public abstract class Repository<T> : IRepository<T>
{
    public IEnumerable<T> All
    {
        get
        {
            return new T[0];
        }
    }
}

public interface IFooRepository : IRepository<Foo>
{
}

public class Foo
{
}

To generate a proxy equivalent to

public class FooRepository : Repository<Foo>, IFooRepository
{
}

when using DynamicProxy:

DefaultProxyBuilder proxyBuilder = new DefaultProxyBuilder();

Type baseType = typeof(Repository<>).MakeGenericType(typeof(Foo));

Type[] intefacesImplemented =  new Type[] { typeof(IFooRepository) };

Type proxy = proxyBuilder.CreateClassProxyType(baseType, intefacesImplemented,  ProxyGenerationOptions.Default);

When using Reflection.Emit:

Type baseType = typeof(Repository<>).MakeGenericType(typeof(Foo));
Type repositoryInteface = typeof(IFooRepository);

AssemblyName asmName = new AssemblyName(
    string.Format("{0}_{1}", "tmpAsm", Guid.NewGuid().ToString("N"))
);

// create in memory assembly only
AssemblyBuilder asmBuilder =
    AppDomain.CurrentDomain.DefineDynamicAssembly(asmName, AssemblyBuilderAccess.Run);

ModuleBuilder moduleBuilder =
    asmBuilder.DefineDynamicModule("core");

string proxyTypeName = string.Format("{0}_{1}", repositoryInteface.Name, Guid.NewGuid().ToString("N"));

TypeBuilder typeBuilder = 
    moduleBuilder.DefineType(proxyTypeName);

typeBuilder.AddInterfaceImplementation(repositoryInteface);
typeBuilder.SetParent(baseType);

Type proxy = typeBuilder.CreateType();

You can then register them with your IOC container and use them as usual: (in this case Windsor):

WindsorContainer container = new WindsorContainer();

container.Register(Component.For<IFooRepository>().Forward(proxy));

IFooRepository repository = container.Resolve<IFooRepository>();

IEnumerable<Foo> allFoos = repository.All;

Both Reflection.Emit and DynamicProxy can be setup up allow for the use of non-default constructors.

If you’re interested there’s an excellent tutorial on DynamicProxy, while the documentation for the Reflection.Emit classes can be found here.

like image 101
Luke Forder Avatar answered Sep 23 '22 06:09

Luke Forder