开发者

Multi Tenant MVC 3 App with View Model Problem

I am writing an app that utilizes multi-tenancy as outlined by Zack Owens.

In Zack's example he has separate c# class libraries to store the controllers and views for each tenant. Since I am not using the Spark View engine which has compiled views this is not an option. Instead I have created a separate MVC3 App Project for each tenant and in there I am storing the content, controllers, views, etc.

I have a main MVC App Project (which is the startup app) with a custom controller factory which scans the assemblies similar to Zacks example and sets up dependency injection using structure map. I have a view engine that uses the controller context to grab the correct view. If it can't find a view or dependency in the tenants app it will use the view or dependency from our main MVC App Project. Everything is working awesome except when it comes to viewmodels.

Most (if not all) of our views are passed a view model. I would like the View Model class to reside in the Tenant App Project and not in the Main App Project so tenants can have custom view models to go with their custom views.

It seems like it would work but I keep getting Errors.

Scenario #1 - The default View Models reside in the main MVC App Project. The Tenant App has a reference to the Main A开发者_JS百科pp and therefore can use the default View Models. No Problems. Now when I want to add a View Model or Override a View Model, I create the class and put it in the Tenant App. The problem is when you call the page, the Main MVC App can't find the new View Model because there is no reference from the Main App to the Tenant App and their can't be due to circular reference issues.

Scenario #2 - I remove the reference from the Tenant App to the Main App. I then copy the default view models from the Main App to the Tenant App, change the namespaces, and create a reference from the Main App to the Tenant App so the view model classes are accessible to the Main App.

I then run it and get the following error:

The model item passed into the dictionary is of type 'Sample.Models.Login', but this dictionary requires a model item of type 'Sample.Models.Login'.

The error makes sense, but not really. I only have one class named Sample.Models.Login

I have some assumptions on why this might be doing this but I can't get it figured out. Any tips or advice would be greatly appreciated.

Thank You


Not sure I understand but I'll have a go anyway.

First of all, if you need to have different view models, the chances are you should have a different controller/action. Maybe you just want to add some extra information though - in that case, the customised view model should be based off the original view model. You can get round circular reference issues by moving the main view models into a separate assembly. You can also manage reference issues by using a plugin framework such as the Managed Extensibility Framework.

However, for any given controller/action, the data requirements of the main part of the view should be fairly constant regardless of the tenant. If it needs different data it's probably not doing quite the same thing and should be in a different action.

That said, one tenant could have a mini basket at the top of every page for example while the other tenants don't, so you would think they need custom view models to include the basket data. However, a better way to do this would be just to alter the master page for that tenant to include a call to @Html.RenderAction("minibasket") for example (in Razor), and that action will be responsible for fetching the additional basket data.

And as an aside, if you want to compile your views like Adam suggested, an additional option would be to take a look at RazorGenerator.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜