A plugin framework with ASP.NET MVC3 and embedded Razor views

前端 未结 6 523
小鲜肉
小鲜肉 2020-12-12 17:32

I\'m designing a plugin framework for ASP.NET MVC3 using Razor views, and I\'m having an issue getting the embedded views to work correctly.

The plugin frame

相关标签:
6条回答
  • 2020-12-12 18:08

    You might find this helpful http://www.adverseconditionals.com/2011/07/portable-aspnet-code-using.html

    I have started creating two projects

    MyLibrary MyLibrary.Templates

    and have the views as Content in .Templates, and added as links and set to EmbeddedResource in MyLibrary. People that want to override the views can install the .Templates project.

    0 讨论(0)
  • 2020-12-12 18:13

    Check out MEF

    You can also do this with Windsor Installers - Mike Hadlow ha a good stab at this: http://mikehadlow.blogspot.com/2010/10/experimental-aspnet-mvc-add-ins-updated.html

    0 讨论(0)
  • 2020-12-12 18:14

    Ok, the solution was found using this article.

    First I create a class with a PreApplicationStartMethod. This method scans the plugin folder and copies the dlls to the AppDomain.DynamicDirectory.

    Then each of these dll's are loaded using BuildManager.AddReferencedAssembly.

    And voilà, the strongly-typed Razor views compile beautifully. See the code here:

    [assembly: PreApplicationStartMethod(typeof(MySolution.PluginHandler.PluginActivator), "Initialize")]
    namespace MySolution.PluginHandler
    {
        public class PluginActivator
        {
            private static readonly DirectoryInfo PluginFolderInfo;
    
            static PluginActivator() {
                PluginFolderInfo = new DirectoryInfo(HostingEnvironment.MapPath("~/plugins"));
            }
    
            public static void Initialize() {
                CopyPluginDlls(PluginFolderInfo, AppDomain.CurrentDomain.DynamicDirectory);
                LoadPluginAssemblies(AppDomain.CurrentDomain.DynamicDirectory);
            }
    
            private static void CopyPluginDlls(DirectoryInfo sourceFolder, string destinationFolder)
            {
                foreach (var plug in sourceFolder.GetFiles("*.dll", SearchOption.AllDirectories)) {
                    if (!File.Exists(Path.Combine(destinationFolder, plug.Name))) {
                        File.Copy(plug.FullName, Path.Combine(destinationFolder, plug.Name), false);
                    }
                }
            }
    
            private static void LoadPluginAssemblies(string dynamicDirectory)
            {
                foreach (var plug in Directory.GetFiles(dynamicDirectory, "*.dll", SearchOption.AllDirectories)) {
                    Assembly assembly = Assembly.Load(AssemblyName.GetAssemblyName(plug));
                    BuildManager.AddReferencedAssembly(assembly);
                }
            }
        }
    }
    

    I hope this can help other programmers that want to create a clean plugin framework using these new technologies.

    0 讨论(0)
  • 2020-12-12 18:20

    David Ebbo recently blogged about precompiling Razor views into assemblies. You can view the post here.

    You should be able to avoid registering the assemblies directly by dynamically loading the assemblies (I would typically use my IoC container for this) and then calling BuildManager.AddReferencedAssembly for each plugin assembly.

    0 讨论(0)
  • 2020-12-12 18:25

    Please take a look at NOPCommerce Source code. It has nice plugin framework based on work by Shannon.

    0 讨论(0)
  • 2020-12-12 18:28

    You can also go for "Area" feature in MVC3 and 4 , you can create a nice plugin system. It gives separation of handling plugin's model,view and controller in it's own assembly.

    0 讨论(0)
提交回复
热议问题