Fredrik Normén
ASP.NET, AJAX, Silverlight, RIA, Architecture, Clean Code
-
ASP.Net MVC Framework 2 - The Binding helper class (UI-mapper)
-
ASP.Net MVC Framework 2 - Interception and creating a Role Action Filter
With the ASP.Net MVC Framework Preview 2, we can use Action Filters. An Action Filter can be used to intercept actions before and after an Action method of a Controller is executed. By using Action Filters you can for example add code that will check if a user has the right permission to access a action method, or you can use it to log messaged before and action is executed, and also after. An Action Filter is easy to apply to a Controller or its Action method. Just add the ActionFilter as an attribute to the class or specific method of a Controller.
-
ASP.Net MVC Framework 2 - Define Routes in Web.config
Something I like with ASP.Net MVC Framework Preview 2 is the RouteValueDictionary used by the Route class’s Defaults, Constraints and DataTokens property, before they use an anonymous type. Because the properties are now a specific type it’s much easier to define the routes in web.config. Before it was quite easy but some reflection was needed. I have now created a ConfigSection and a helper method to easily register routes which can be defined in the web.config for the Preview 2 version of the ASP.Net MVC Framework. I decided to use the following XML structure to define routes in web.config:
-
ASP.Net MVC Framework - Using Forms Authentication
NOTE: THIS Post was written when ASP.NET MVC Framework was in a early Preview stage.
-
ASP.Net MVC Framework - Assign a ViewFactory to a Controller
I have seen several posts about how to assign a ViewFactory to a Controller, most of the case the code looks like this:
public class HomeController : Controller
{
public HomeController()
{
this.ViewFactory = new MyViewFactory();
}
} -
Remove code smell with AOP
During the last years you have probably heard about Aspect Oriented Programming (AOP). Different people like or don’t like AOP. I’m one of them who like AOP. As you may now VB.Net and C# are object orientated languages and with object oriented programming (OOP) you want to reduce code duplication, code duplications smells badly. If you need to do code duplication you are probably doing something wrong with the implementation of your domain model. With OOP you can generally eliminating code duplication, but there are some cases where we can’t avoid it, for example take a look at this code:
-
Multiple return values, I want this in C#
I so badly want to have support for multiple return values in C# instead of using out parameters.
Note: The following is only an example, we can use a struct or an object to create a data structure which will hold the value, but I only use this code for demonstration purpose.
int x,y,z;
bool isHidden;
GetCords("MyElement", out x, out y, out z, out isHidden);
Instead of the code above, I want to do something like this:
Return 10,11,10,true;
var x,y,z,isHidden = GetCords("MyElement");
If I don’t care about the y and z return values I can instead write:
var x,,,isHidden = GetCords("MyElement");
Isn’t this beautiful?
hmm, when I’m thinking of beauty, I start to think about "The beauty is in the eye of the beholder" ;) -
Defensive programming and Design by Contract on a routine level
“When quality is pursued, productivity follows” – K. Fujino, Vice President of NEC Cooperation’s C&C Software Development Group.
-
MVC - Expression Language and custom tags
I have seen several developers that want and have no problem by using server-side code blocks in a View when they are working with the MVC pattern. But I think the server-side code block should be used only in some advanced and special occasions and instead use an Expression Language. By using a lot of server-side code block can make the View messy and the solution can be difficult to implement, form the perspective of code maintainability and extensibility. If we instead use tags similar to HTML/XML we can make the code blend into HTML in a nice way.
Take a look at the following code:
<% if (ViewData != null && ViewData.HasError) {%>
...
<% } %> -
ASP.NET MVC Framework - Ajax
Take look at Nikhil's blog about how AJAX can be used with the ASP.Net MVC Framework: