(Lviv community of .NET developers)

Activator for App_Code assemblies

December 14, 2007 17:38 by alexk
I want to implement "proxy" pattern in my ASP.NET application is it possible?

why not?!

First question:
which pattern you want to use for instance of proxy class creation? if it looks like written below, then you are looking on right article.

 protected void Page_Load( object sender, EventArgs e )
{
    Assembly appCodeAssembly = FindAppCodeAssembly();
    string proxyType = "Artfulbits.Proxy.LocalProxyAdapter";
    Type type = appCodeAssembly.GetType( proxyType );
    IProxyAdapter proxy = ( IProxyAdapter )Activator.CreateInstance( type, null );

    // TODO: use proxy as you want here
}


Where to place proxy classes implementation?

I recommend to use App_Code folder in ASP.NET web site. One from the many greates pluses is that you can change code at runtime. ASP.NET application recompile itself on any changes. And this is very good :)

How to find App_Code assembly in ASP.NET AppDomain?


try to use this simple method written below. It's not very performance optimized, but it works fine for me.

  private static readonly Regex s_expression = new Regex( "App_Code*", RegexOptions.Compiled | RegexOptions.IgnoreCase );

  public static Assembly FindAppCodeAssembly()
  {
    Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();

    foreach( Assembly asm in assemblies )
    {
      if( s_expression.IsMatch( asm.FullName ) )
      {
        return asm;
      }
    }

    return null;
  }
 

Here we use specifics of ASP.NET implementation and assemblies names creation. I can not guaranty that this code will fork fine for every scenario, but in 80% of asp.net application development it's will work without any troubles. Enjoy

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Add comment


(Will show your Gravatar icon)