C# – Plugin Strategy For MVC 5 Site

asp.net-mvc5c

I am working on setting up a multi-tenant site, where users can select a theme. Each of these themes have different settings, so I would like someone to be able to select a theme and then when they edit their site settings it would bring up a form to allow that. I am sure I could do this through hard coding data, but it would seem that I would be better off using plugins to allow new theme plugins to be added and remove the need to magic strings or having to create a dynamic settings page that reads what fields to display from a database. There is plenty of documentation on things like MEF, but I need some help on figuring out how to display a unique view for each theme and then store the results. Any help or direction would be appreciated.

Best Answer

You do not need MEF or plugabble stuff. Using IoC container will be enough for MVC5 I have used Windsor Castle with great success and lately also Autofac.

Then you have all your controllers made by factory and you can have service for selecting layout in your controller. With this service you query database for user settings and set your model/viewbag with needed info. I did something like it for silent release where only pilot users got new look and new features aka feature switch.

Also you can switch your layout by using "_ViewStart.cshtml"

The _ViewStart file can be used to define common view code that you want to execute at the start of each View’s rendering. For example, we could write code within our _ViewStart.cshtml file to programmatically set the Layout property for each View to be the SiteLayout.cshtml file by default.

https://www.codeproject.com/articles/808894/ioc-in-asp-net-mvc-using-autofac

http://www.c-sharpcorner.com/UploadFile/dacca2/implement-ioc-using-unity-in-mvc-5/

On finishing note you can also do it with static class that has config from database but then you have to do a lot of ifs.