Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7613889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T02:15:49+00:00 2026-05-31T02:15:49+00:00

Our architecture uses the Repository pattern extensively. We have an abstract base class for

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T02:15:50+00:00Added an answer on May 31, 2026 at 2:15 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We've got some code that uses LoadLibrary and GetProcAddress to implement a plugin architecture
We plan to redesign, improve our (C#) application architecture. Anyone has some framework, hompage
I have a couple of design/architectural questions that always come up in our shop.
We have some search functionality that can return tens of thousands of results from
In a recent project I have nearly completed we used an architecture that as
I’m trying to reevaluate our n-layer architecture and would love to get some suggestions
In our application we use the repository pattern to retrieve and persist data from
The architecture of our system is such that there is a set of functionally
We have the standard three tier architecture with our middle tier hosted in IIS
I have an architecture where we are passing our data nodes as IEnumerable<BaseNode> .

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.