I am looking for a solution to modularize my simple commerce https://github.com/simplcommerce/SimplCommerce. I have been researching for a few days. Some instructions have been found for example:
- aspnet/Mvc#4572
- https://github.com/ExtCore/ExtCore
- http://shazwazza.com/post/custom-assembly-loading-with-aspnet-core/
- https://github.com/OrchardCMS/Orchard2
They seem not completed yet or too complicated for me. Struggling for a while, finally I can make it work. To be honest, I not sure this is best solution and I am looking for comments
There are few things we need to address to make our application modularized:
- How can MVC know about our controllers when they are in other class libraries, in other folder and not being referenced by the host
- How can the ViewEngine pick up the right location for the Views
- How to register services used in modules
- How to serve static file: js, css, image for modules
Below is general folder structure I have come up with
The Modular.WebHost is the ASP.NET Core project and it will act as the host. It will bootstrap the app and load all the modules it found in the Modules folder.
Each module contains all the stuff for itself to run including Controllers, Services, Views and event static files.
For easy development, in the visual studio solution I create a "Modules" solution items and add module projects in Modular.WebHost/Modules phycial folder. To prevent Modular.WebHost to compile stuff in Modules folder, we need to exclude them in the project.json.
- Fist, in the Startup.cs, we scan all the *.dll in each module and load them up
varmoduleRootFolder=_hostingEnvironment.ContentRootFileProvider.GetDirectoryContents("/Modules");foreach(varmoduleFolderinmoduleRootFolder.Where(x =>x.IsDirectory)){varbinFolder=newDirectoryInfo(Path.Combine(moduleFolder.PhysicalPath,"bin"));if(!binFolder.Exists){continue;}foreach(varfileinbinFolder.GetFileSystemInfos("*.dll",SearchOption.AllDirectories)){Assemblyassembly;try{assembly=AssemblyLoadContext.Default.LoadFromAssemblyPath(file.FullName);}catch(FileLoadExceptionex){if(ex.Message=="Assembly with same name is already loaded"){continue;}throw;}if(assembly.FullName.Contains(moduleFolder.Name)){modules.Add(newModuleInfo{Name=moduleFolder.Name,Assembly=assembly,Path=moduleFolder.PhysicalPath});}}}}Then module assemblies will be added to MVC by ApplicationPart
varmvcBuilder=services.AddMvc();foreach(varmoduleinmodules){// Register controller from modulesmvcBuilder.AddApplicationPart(module.Assembly);}ModuleViewLocationExpander is used to help the view engine lookup up the right module folder the views
services.Configure<RazorViewEngineOptions>(options =>{options.ViewLocationExpanders.Add(newModuleViewLocationExpander());});
Each module contains an ModuleInitializer.cs where services for that module is registered.
// Register dependency in modulesvarmoduleInitializerInterface=typeof(IModuleInitializer);foreach(varmoduleinmodules){// Register dependency in modulesvarmoduleInitializerType=module.Assembly.GetTypes().Where(x =>typeof(IModuleInitializer).IsAssignableFrom(x)).FirstOrDefault();if(moduleInitializerType!=null&&moduleInitializerType!=typeof(IModuleInitializer)){varmoduleInitializer=(IModuleInitializer)Activator.CreateInstance(moduleInitializerType);moduleInitializer.Init(services);}}- And this is how I serve static files for modules
// Serving static file for modulesforeach(varmoduleinmodules){varwwwrootDir=newDirectoryInfo(Path.Combine(module.Path,"wwwroot"));if(!wwwrootDir.Exists){continue;}app.UseStaticFiles(newStaticFileOptions(){FileProvider=newPhysicalFileProvider(wwwrootDir.FullName),RequestPath=newPathString("/"+module.SortName)});}