1

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

1
  • 2
    "isn't allowed", and "prefer composition over inheritance". That do? Commented Mar 5, 2012 at 6:52

1 Answer 1

3

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. Google favor composition over inheritance to know more.

So the answer is

Move the common functionality to a separate class which you include in all classes that needs it. (RiskService or ProductionService)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! :) as a side note before I come across as a total n00b, i just wrote down the class names as an example, don't normally use my example as a naming convention... then again, why am i defending myself on the internet :P.. anyhow, thanks again :D
@Yenros: It doesn't matter how you did name your classes or how you structured them before. What matters is how they look in the future. We all come here to learn. I come across as a noob too, just look at all my questions ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.