Implementing Model View Controller with C# (or any programming language)

koeks525

Executive Member
Joined
Jul 14, 2012
Messages
6,011
Reaction score
1,197
Location
Canada
Good Morning guys,

This week, I had decided I will be writing my C# Practicals in Model View Controller style (which probably is a bit overkill because the programs they require us to write are not too complex or big enough). My issue is with implementing, or rather, coding MVC.

From what I understand, the Model contains your database class(es), as well your "data" classes like Student, or Employee, or User, etc. The View contains all the screens (user interfaces) that the program will use. The Controller is the "brains" behind the program, which will use and process the needed models and push data to the needed view(s) and display to the user.

With this, I thought of creating a Controller Class, A DatabaseOperations Class, My Product Class, and My Category Class. My problem is with the controller class, I thought the controller must be accessible throughout the runtime of the program... my first thought would be in using static methods but that is bad (I heard). Another method I thought of is making each of my views have a Controller object instatiated in them - but then this would mean each form will have a different instance of the Controller class.

How would you guys implement the Model View Controller pattern in programming? I really thought it would be great if I get started with MVC and eventually get comfortable using it.

Thanks.
 
I.M.O. It is generally a good idea to have a static class to hold at least the most important system-wide variables (I use 'Program'). If you do not want to use a static class for the 'brains' of the application, then access an instance of the 'brains' class via the static class (I use Application).

i.e.

Code:
public static class Program 
{
      public static Application = new Application();
}

Then anywhere in your code you can access Program.Application, note that although Application is statically declared, it is still an instance of a class and as you will have only one instance of Application it also brings in the Singleton design pattern which perhaps may score you some points.

I'm sure the others will give a better implementation though.
 
Last edited:
Nothing wrong with static as long as you remember to not keep state in it.

I'll split the data access layer entity classes from the models of the business layer and the UI layer. Reason being is that when your data source changes you won't have adverse effects or changes on the UI etc (especially when binding and the compiler just gracefully ignores the blatantly obvious mistakes).

If these practicals are web based you can use the MVC framework.

If you are going to do this from scratch and not use an existing MVC framework you'll most likely have to pass the controller into the view as an argument and call it from the events etc.
 
I.M.O. It is generally a good idea to have a static class to hold at least the most important system-wide variables (I use 'Program'). If you do not want to use a static class for the 'brains' of the application, then access an instance of the 'brains' class via the static class (I use Application).

i.e.

Code:
public static class Program 
{
      public static Application = new Application();
}

Then anywhere in your code you can access Program.Application, note that although Application is statically declared, it is still an instance of a class and as you will have only one instance of Application it also brings in the Singleton design pattern which perhaps may score you some points.

I'm sure the others will give a better implementation though.

This is what I thought of doing as well, with the Controller class I had created with my application. I wanted to do this but I was reluctant to do it after hearing a third year lecturer mention making use of static methods isn't a good idea because you can't control its life span.

I think let me put my Controller object in some static class, then my next practical I will try other ways
 
Top
Sign up to the MyBroadband newsletter
X