Raba - Defend your code RSS 2.0
# Wednesday, January 16, 2008

I am not going to explain here nor about MVC neither about the ASP.NET 3.6 (yes it is 3.6, they didn't tell you that?!) I just gonna write a little bit about a sample application I wrote to myself.
Those of you who want to learn more about MVC (the pattern itself) - you can read in Martin Fowler's post or maybe here at wiki anyway there is even a better place to learn this in the Head First - Design Patterns book.
Also you might read about the new asp.net MVC Framework (CTP), you can choose whether to read this (long but very important article) or you can see the intro webcast (damn you lazy programmers)

Microsoft are doing well to cause us work harder just to learn the technology, this asp.net MVC implementation is awesome but It is a huge change for all of you ASP.NET people, I hope I find the time to write more about it, but here is one sample I wrote for myself.


                                       From Request to Response through the MVC Flow

On each request there is a place that the Controller has its constructor calls and this will initiate your logic, I found it very useful to manage this controller creation. In this example I create my own Factory and there I manage to create the controller using another constructor (not the default one), moreover I can also add my own logic such as Security Check (in our sample) or something else useful.

Here is my own Controller Factory logic:

1 public class ShaniControllerFactory : IControllerFactory 2 { 3 #region IControllerFactory Members 4 public bool IsUserExists 5 { 6 get 7 { //Put here your great Logic 8 return false; 9 } 10 } 11 public bool IsUserAuthorizedForEdit 12 { 13 get 14 { //Again Special Logic for Checking Authorization 15 return false; 16 } 17 } 18 19 public IController CreateController(RequestContext context, Type controllerType) 20 { 21 IController controller = null; 22 if (!IsUserExists) 23 { 24 controller = new NotAuthorizedController(); 25 return controller; 26 } 27 28 //Distinct between those two 29 if (!IsUserAuthorizedForEdit) 30 { 31 controller = Activator.CreateInstance(controllerType,(object) false) as IController; 32 } 33 else 34 { 35 controller = Activator.CreateInstance(controllerType, (object)true) as IController; 36 } 37 return controller; 38 } 39 40 #endregion 41 }

You can simply see the CreateController method (lines: 19-38) where I put my logic for the Controls initiation, and I simply prefer creating them using my constructor (the one with the bool parameter) and not using the default one.
But, How will we have this Controller Factory running instead of the generic factory? It is very simple:

1 protected void Application_Start(object sender, EventArgs e) 2 { 3 ControllerBuilder.Current.SetDefaultControllerFactory(typeof(ShaniControllerFactory)); 4 5 RouteTable.Routes.Add(new Route 6 { 7 Url = "[controller]/[action]/[id]", 8 Defaults = new { action = "Index", id = (string)null }, 9 RouteHandler = typeof(ShaniRouteHandler) 10 }); 11 12 RouteTable.Routes.Add(new Route 13 { 14 Url = "Default.aspx", 15 Defaults = new { controller = "Home", action = "Index", id = (string)null }, 16 RouteHandler = typeof(ShaniRouteHandler) 17 }); 18 } 19

You can see at line 3 (within the Global.asax), this is all I had to do for setting my controller factory as the default factory.

You can also think about another good examples on your own, I've found a great post about TDD and Dependency injection with asp.net MVC by Phil Haack - Read the whole post it is a great one, at the end he is creating his own factory for managing the Control creation using Dependency Injection.

Enjoy.

Wednesday, January 16, 2008 12:04:15 AM (GMT Standard Time, UTC+00:00)  #    Comments [0] - Trackback
Asp.net MVC

Comments are closed.
Archive
<February 2012>
SunMonTueWedThuFriSat
2930311234
567891011
12131415161718
19202122232425
26272829123
45678910
Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2012
Shani Raba
Sign In
Statistics
Total Posts: 145
This Year: 0
This Month: 0
This Week: 0
Comments: 97
Cool Stuff
Add to Technorati Favorites
Themes
Pick a theme:
All Content © 2012, Shani Raba
DasBlog theme 'Business' created by Christoph De Baene (delarou)