Paul Hinett
Paul Hinett

Reputation: 1981

.Net Core - Multi Tenant Controller Overrides

I am looking to add multi-tenancy to an .net core MVC application i am developing.

My aim is to have a core project which contains the base features, including controllers, views, assets etc.

Then I would like to have a separate project per tenant which can override the defaults from the core project. At the moment i am trying to tackle the task of overriding controllers, not just at the controller level but the ability to override individual actions within a controller.

What are should i be looking to to do this, I was thinking about the IControllerFactory but not sure if this is the correct place in .net core?

I'm assuming there may be a away to achieve this with IoC but need pointing in the right direction.

Anyone have any thoughts on this?

Upvotes: 0

Views: 372

Answers (1)

Macilquham
Macilquham

Reputation: 282

This is the way I designed it on my current project, my requirement is slightly different to yours in that its multi-tenated but a tenant can operate in mutiple countries, the functionality is the same per tenant but different per country, the design below will hopefully give you a few ideas to help you on your way.

I treat the controllers as nothing more than a navigation mechanism, keep them thin and light. I have an area per country (in your scenario this could be an area per tenant)

enter image description here

Each areas controllers simply reference a base controller

enter image description here

The base controller is very light because we want be able to implement custom logic per country using our DI container. Lets run through the Edit scenario for a permit.

enter image description here Intellisense playing up view shouldnt be red :)

Edit permit calls _editPermitManager which is of type IEditPermitManager injected into our controller. I have two implementations of this in the system one for New Zealand because its a special case and one all other countries use (but you could easily have an implementation per country).

enter image description here

enter image description here

I manage this injection with ninject enter image description here

The flow is user navigates to /blah/NewZealand/Permit/Edit/4 clicks submit this is directed to Permit controller in NewZealand therefore ninjects injects the New Zealand edit manager and this is invoked on the edit flow, I also inject in a INavigationService (_navigationService.Route) enabling me to potentially direct to different views on a per country basis.

Hopefully that gives you a starting point.

Upvotes: 1

Related Questions