This might be c# basics 101, but I just thought if I don’t ask I’ll never know, and be doomed to write badly structured code… (the project will be in MVC, so my controllers will need to inherit from the controller class too)
Anyhow. I’d like to structure my code something like this.
public class Global_Base_Class: System.Web.Mvc.Controller { }
public class Production_Base : Global_Base_Class { GetPoducts, AddProduct... }
public class Risk_Base : Global_Base_Class { GetRisk, AddRisk... }
public class Example_View_Controller_A : Production_Base { }
public class Example_View_Controller_B : Risk_Base { }
What if I need/would like to have (to DRY up my code)
public class Example_View_Controller_C : Production_Base, Risk_Base
I’ve read that Multiple Inheritance isn’t good practice/not allowed in c#…
What would be the best route/best practice for me to use to achieve this?
Any help/links will be appreciated
First of all: Don’t name your classes like that. Microsoft have written a set of guidelines: http://msdn.microsoft.com/en-us/library/ms229002.aspx
Your structure is not valid in any OOP language since the relationships are not
is-a. A controller is a MVC controller and not a class with your business logic. Googlefavor composition over inheritanceto know more.So the answer is
Move the common functionality to a separate class which you include in all classes that needs it. (
RiskServiceorProductionService)