ASP.NET MVC 3 Thoughts

17. August 2010

I've been playing with ASP.NET MVC 3 some days now and I have some thoughts about it. First of all, it looks cool, using the Razor syntax in views is already a step in the good direction. But, why on earth do we need Dependency Injection in an MVC framework? DI is cool in enterprise wide applications that need flexible interfaces and thorough testing, but use it in MVC? I think we forget the essence of what a MVC framework should do: display information. No calculations, no fancy techy obscure coding, no, just fetch the data for your view model, do some simple logic for display and input handling and create views for that. Want calculations? -> Use services. Want heavy techy stuff? -> Use services. That is the reason we came up with SOA no?

 

I my opinion a website should have a very small footprint in your business process. It should be easily maintainable and very transparent to non developers like content managers, designers and marketeers. You could say that a good website should have at least some kind of CMS right? Maybe that is true, but still, transparency is the key to success. It helps you tune you site once it slows down, scale your site if it grows, ...

 

If you start using DI you don't know where it ends. MVC is not only a cool framework because you have control over how you would like your views to look, it also offers you a very testable base that allows you to test controllers and views in a simple transparent way.

 

The same goes for the new Razor syntax which is nice and well thought, we all work qwerty right, where the @ is better reachable then our worshipped %. But that is another story. Let’s take a small example of some old MVC code and the new Razor syntax

 

ASP.NET MVC syntax

   1: <ul>
   2:     <%
   1:  foreach(var p in Model.Products) { 
%>
   3:         <li><%
   1: = p.Name 
%> ($<%
   1: = p.Price 
%>)</li>
   4:     <%
   1:  } 
%>
   5: </ul>

Razor syntax

   1: <ul>  
   2:     @foreach(var p in Model.Products) {  
   3:      <li>@p.Name ($@p.Price)</li>  
   4:     }  
   5: </ul> 

 

In both cases if we open these files in an editor that is not Visual Studio, we end up with some nasty code in our design that isn’t supposed to be there. Why not surround all logic tags by <!---->, yes, the HTML comment tags.

 

Suggestion

   1: <ul>  
   2:     <!--@foreach(var p in Model.Products) {-->
   3:     <li>@p.Name ($@p.Price)</li>  
   4:     <!--}-->
   5: </ul> 

 

It solves some issues with our designers I think.

 

Maybe I will be back with some more thoughts about it. My credo still remains the same keep it simple ;)

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Featured, Development

Macbook Pro + eSata expresscard

30. April 2010

After running Windows 7 in VM Fusion on my internal SSD I wanted to use the ex-internal 7200 RPM disk I replaced with the SSD to run my VMs. But to use the full capacity of the 7200 RPMs as an external disk, I couldn't rely on USB 2.0. So I went on the lookout for a good eSata ExpressCard.

I purchased a Sweex PU211 eSata II ExpressCard together with a SilverStone TS02B external eSata enclosure.The latter allows you to use eSata without having to connect your drive to an external power source. You just plugin the USB cable and eSata cable and you're good to go.

 I'm now running all my VMs from the external drive using eSata speeds until a larger 'good' ssd disk becomes affordable. The Macbook Pro supports the Sweex card natively and allows you to hot swap different eSata drives.

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Grouping objects with LINQ in C#

15. October 2009

A collegue of mine needed an algorithm to filter out duplicate objects in a List and count the total occurence of each distinct object. He used this query to get his results, it may be usefull to some of you.

myRoles = from c in allRoles
          group c.RoleId
          by new { c.PersonId, c.RoleTypeId }
          into g
          orderby g.Count() descending
          select g;

Credits go to Roel Dieltjens

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Vici Core Tip #1 - XML file based settings

13. October 2009

.NET offers quite an extensive configuration framework in which you can use XML files to configure your applications. The drawback is that most of these files need to be located in the applications directory or sub-directories. Thus excluding reuse throughout different applications. An example of this is using an app.config or web.config file to store all your config information. If you have different applications that use the same base assemblies you can either place them in the GAC or place them in the same application directory as you main applications. Which imply that you need to keep you config settings in sync across multiple files and apps.

You can off course design your own settings-framework to resolve the above issue or use one of the fancy features of Vici Core. Let's get started on how we implement this.

What you need to obtain first is a copy of the latest Vici.Core dll which you can download at http://viciproject.com/wiki/Projects/Mvc/Download (the core has not yet received its own download, so it comes together with the mvc binaries for now, just get it from that download).

After you downoaded Vici.Core.dll, reference it in your project and then let the fun begin. I will include some sample code to show you  how simple it is to load your settings in your application

Step 1 - Create your XML file

As you can see there is a version attribute in the XML file to tell the config framework that we updated the XML file and that a new version needs to be updated in the cache.

<?xml version="1.0" encoding="utf-8" ?><Config version="1">    <FileLocation>c:\files\</FileLocation>    <TempPath>c:\temp\</TempPath></Config>

Step 2 - Create a POCO to hold your settings

Property names map to the XML nodes in your config file.

 

public class ConfigSet{  public string FileLocation;  public string TempPath;}

 

Step 3 - Register your POCO with the Vici Core config framework

 

using Vici.Core.Config;  class Program  {    public static ConfigSet Config;    static void Main(string[] args)    {      // The path to the location of your config file      string configFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config.xml");      // Register the provider with the xml file      ConfigManager.RegisterProvider(new ConfigurationProviderXmlConfig(configFilePath));      // Create a new instance of our POCO      Config = new ConfigSet();      // Register our POCO with the framework      ConfigManager.Register(Config);      // Update our manager to tell it that we registered a new config file      ConfigManager.Update();    }  }

 

 

Step 4 - Use it

/*  Your code here*/string filePath = Program.Config.FileLocation;/*  More of your code here*/

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Remove SVN files and folders by a click

9. June 2009

So your using SVN as we are? And you need to remove all the SVN files and folders to relocate or just detach your project? Then this trick will save you some time.

It adds a 'Delete SVN Folders' entry to your context menu in Windows Explorer.

 

How does it work? Create a new file with a .reg extension and add the following lines to the file.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN]
@="Delete SVN Folders"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shell\DeleteSVN\command]
@="cmd.exe /c \"TITLE Deleting SVN Folders in %1 && COLOR 9A && FOR /r \"%1\" %%f IN (.svn) DO RD /s /q \"%%f\" \""

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Vici MVC 2.0 released

4. June 2009

Last weekend a new 2.0 stable version of Vici MVC was released. So if you're really into web development have a break and visit http://www.viciproject.com/

 

More Tips & Tricks to come ...

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,

Vici MVC Tip #2 - ActionResult you say?

20. May 2009

Vici MVC Tip 2The latest Vici MVC releases from 2080 up contain a new ActionResult object. This allows your controllers to return an action instead of the default void. So what should I use it for? Let's say you would like to stream xml or the contents of a file to the browser. This could be done the old way by using the response object from the WebAppContext or by using the new way using ActionResults.

Also for some ajax requests you expect JSON to be returned by the server in order to make your scripts work. You could do the conversion of your data to JSON by yourself or use anonymous types and the new JSONActionResult to convert your data to JSON automatically.

ActionResults currently available

  • JSONActionResult: Output your data as a JSON formatted string for AJAX or Javascript use.
  • RedirectActionResult: Redirect to another URL.
  • RenderViewActionResult: In fact this is the same as you would use the ChangeLayout method. You define a different view from the default for your controller. As an extra you can provide a complete new ViewData collection to be used by the new view.
  • SendFileActionResult: Send a file through the browser to the user
  • XmlActionResult: Stream an XML string to the browser

In theory this is all very nice, but how do I use this in code? Well here we go.

Stream a file to the browser

  1. using Vici.Mvc;  
  2.  
  3. namespace BVirtual.ViciDemo  
  4. {  
  5.   [Url("/Download/{DownloadUid}")]  
  6.   public class Download : BaseController  
  7.   {  
  8.     /// <summary>  
  9.     /// Default action for the download controller  
  10.     /// </summary>  
  11.     /// <param name="DownloadUid">Paramter passed in by Vici MVC during URL parsing</param>  
  12.     public ActionResult Run(string DownloadUid)  
  13.     {  
  14.       // Get the object describing the file we would like to stream from disk  
  15.       DownloadFile dFile = DownloadService.GetFileLocationForUid(DownloadUid);  
  16.  
  17.       // Check if we found the file in our system  
  18.       if (dFile != null)  
  19.       {  
  20.         // Now start streaming the contents of the file from disk to the browser using   
  21.         // - The location on disk  
  22.         // - The mime type of the file to get the correct program to handle it on the client computer  
  23.         // - The filename of the file you would like to stream  
  24.         return new SendFileActionResult(dfile.PhysicalLocationOndisk, dFile.MimeType, dFile.Filename);  
  25.       }  
  26.       else 
  27.       {  
  28.         // No file found by the given UID, redirect to the error page  
  29.         return _RedirectToErrorPage();  
  30.       }  
  31.  
  32.     }  
  33.  
  34.     /// <summary>  
  35.     /// Example method to show that you can return ActionResult objects from another method or class  
  36.     /// </summary>  
  37.     /// <returns>The action result to be returned by the view</returns>  
  38.     private ActionResult _RedirectToErrorPage()  
  39.     {  
  40.       // Return a redirect action in case of error  
  41.       return new RedirectActionResult("/Messages/DownloadFailed");  
  42.     }  
  43.   }  

In this example we would like to stream a file from the server to the client browser. In case we couldn't find we redirect to user to an error message page.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Vici MVC Tip #1 - foreach range

18. May 2009

Vici Project Logo

As you all might know, or not know, I'm a hardcore user of the Vici Project. So I will try to post some tips and tricks on Vici projects here.

This piece of code helps those who would like to loop over a range of items or just use a counter to loop over.

 

 

{{foreach i in [1...20]}}
  Insert your HTML template part here
{{endfor}}

Ok, this is easy, it just loops over the numbers 1 to 20 for i. But what if I would like to do it the other way around. Just do.

{{foreach i in [20...1]}}
  Insert your HTML template part here
{{endfor}}

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,

Pixagogo introduces new album icons

26. February 2009

Online photo album service Pixagogo introduced new album icons to help you identify the settings of your albums. This way it will be clearer for users to see if their albums are publically available, password protected or private. So no need to go to the album settings to check out whether your precious photos are available for all your friends or just for you.

 

For more information or to check it out, go to www.pixagogo.com

 

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development

Aiming high

25. February 2009

It's been a while since I posted my last development blog. I've been kind of busy walking the thing line between development en family life, where development took a great part. But I've found some cool new and existing technologies to put my mind on. The Azure Services Platform is one of them. I've been using "the cloud" for some time now, but not realy from a Microsoft perspective. I admit not being a great Linux supporter but for some cost effective non crucial parts, it's quite ok :)

 

The things I remember from my TechEd visit was, that it requires a bit of a mind switch to develop against the MS cloud. I'm going to dive deeper into this matter to see what it offers and if it can be any use for me in the future.

 

Project Velocity on the other hand is one that I'm eager to test, once I have the time for it. Caching is sacred in web architectures. And Velocity looks nice on paper. I hope they can make it happen and are smart enough not to put it under a paying license. There are nice free alternatives available.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Development ,