views:

289

answers:

3

Is it possible to load external "web-parts" in an ASP.NET website? I would like to make stand-alone dll's containing a User-Control or Page to include in a main site. Like a plug-in architecture for websites.

The only examples I can seem to find bind the user control to the main project, you need to have a "hard" reference to the control.

What I would like is to define a shared interface which the plugins can implement, and the website can load and display.

+1  A: 

There is a discussion over here about compiling UserControls into dlls

http://stackoverflow.com/questions/1765867/asp-net-usercontrol-class-library/1765900#1765900

For something like what you are describing, I would highly recommend creating custom server controls. You could provide an abstract base class for all plug ins.

public abstract class PluginBase : WebControl {

}

Then you could search a directory for assemblies and search each assembly for anything deriving from that type.

    DirectoryInfo di = new DirectoryInfo(path);
    FileInfo[] assemblies = di.GetFiles("*.dll");
    foreach (var assembly in assemblies) {
        Assembly a = Assembly.LoadFrom(assembly.FullName);
        foreach (Type type in a.GetTypes()) {
            if (type.BaseType == typeof(PluginBase)) {                     
                //load plugin
            }
        } 
    }

You can also register your plug ins in your web.config by providing a config section like the example here

Bob
That seems like a nice solution. Would you have to build up the WebControl completely in code, or is there the possibility to use the templating engine?
Robert Massa
+1  A: 

Plugins can implement your interface. Interface may define one method. This method takes necessary general inputs to plugin's context. plugin's responsibility will be to return html markup which can be placed on the final output page. This way you can widegtize this but compromise Page lifecycle and postback features becuase the page has no idea what is returned by the plugin.

this. __curious_geek
Would I also lose the postback-features if the interface implements the WebControl like Bob suggests?
Robert Massa
Yes, you will lose postback features. Just to give you an idea UserControls is inbuilt plugin mechanism provided by asp.net. UserControl derives from System.Web.UI.Page which makes it postback capable. I recommend rather than reinventing the wheel you should try to specialize UserControls by making your own UserControl base class and make it available to plugin developers. It can be so special that it may expose methods and props that are coupled to your application architecture and core business logic.
this. __curious_geek
+1  A: 

Hi Robert I think you want to have an Infrastructure which capable to load decoupled Modules.(Inversion of control, or IoC )

read more about WCSF Web Client Software Factory www.codeplex.com/websf

if you want nice sample code about design web application with modular approach please download Cuyahoga

Cuyahoga is an open source .NET web site framework. It provides content management capabilities and has a modular approach

after all read the ASP.NET 3.5 Social Networking very nice book.

Create a full-featured, enterprise-grade social network using ASP.NET 3.5 Learn key new ASP.NET topics in a practical, hands-on way: LINQ, AJAX, C# 3.0, n-tier architectures, and MVC Build friends lists, messaging systems, user profiles, blogs, message boards, groups, and more Rich with example code, clear explanations, interesting examples, and practical advice – a truly hands-on book for ASP.NET developers

www.packtpub.com/expert-guide-for-social-networking-with-asp-.net-3.5/book

the author show us how to apply design patterns to real world web applications. all the modules that highlighted with Bold Font are developed with IoC Pattern with use of StructureMap

This Book Also has very nice sample code that you can download from Packt Publishing website.

Navid