Tuesday, May 5, 2009

ASP.NET MVC scaffolding

I've been recently playing a bit with the scaffolding capability in ASP.NET MVC tooling support. Being it all nice and dandy it's still a lot of work to be done to get a simple CRUD operation working with any kind of data access technology.

As a result I've started a code/view generator that will allow for bottom-up scaffolding of all the typical layers in a web application: the entity being persisted, data access using NHibernate, a pass-through service layer, the controller with default CRUD actions and a set of views supporting the controller.

As a matter of fact the basic idea behind this tool came from the Rails framework. In there you can scaffold an entity with a page and all the default CRUD behavior from the command-line. This tool will allow for exactly that but instead of taking advantage of the field:type syntax it'll use the NHibernate hbm.xml mapping files to describe an entity and its relation to other entities.

Here's a link in case you want to monitor the progress or (if you feel up to it) to join the effort:

MVCGen

Have fun!

EDIT on 21/06/2011: since I'm no longer working with .NET (or ASP.NET MVC for that matter) the project is dead.

Tuesday, April 28, 2009

ASP.NET MVC template with Spark

What it Spark?

Spark is a view engine for Asp.Net MVC and Castle Project MonoRail frameworks. The idea is to allow the html to dominate the flow and the code to fit seamlessly.

Here's an ASP.NET MVC template that you can put into your "Visual Studio 2008\Templates\ProjectTemplates\Visual Web Developer" folder and start using Spark as view engine.

MvcWebApplicationSparkProjectTemplatev1.cs.zip

All the views as well as the user control used to display log on information has been translated!

Enjoy!

Monday, April 27, 2009

Installing Oracle XE on Ubuntu

I've faced a dilemma today. I need an instance of Oracle XE datababase. The question however is "should I install the windows version on my box?". Since this is not the first time I've been doing it the answer was quite obvious: NEVER EVER DO THAT!

VMware Player to the rescue!

You might ask why wouldn't I install Oracle XE on my Windows box. The answer is plain and simple: I use my notebook for more than just data serving and allowing the Oracle to spread throughout the available resources would be a perfect waste of hardware.
So I did as follows: I've installed Ubuntu 9.04 server (very nice linux distribution indeed!!!), installed the necessary stuff I can't live without (openssh-server, tomcat6, mc, htop), used this tutorial to install Oracle XE, used this tutorial to configure web access to Oracle database manager for external hosts and that's it! It took an hour to get it to work from scratch and the time is really well spent!

One must not forget to allocate quite a lot of swap memory during installation. Automatic disk partitioning is out of the question here. I've created my swap partition to be 1.5 GB in size and that was enough to install Oracle XE.

The final product is 3.5 GB in size, has VMware Tools installed (taken from VMware Server) and performs extraordinary well! Long live virtualization!!!

See ya!

Saturday, April 25, 2009

VirtualPathProvider example

Hi there,

did you ever wonder if you can retrieve ASP.NET MVC views from another storage than a file on disk? For example from the database? This is where the VirtualPathProvider comes into play!

VirtualPathProviderExample.zip

This simplistic example shows how to implement retrieval of data from a predefined string (ugly long string but that's not the point here). You can use some other backing store (database, ftp or even web service!) to get the content.

Enjoy!

Friday, April 24, 2009

Thursday, April 23, 2009

New NUnit template for ASP.NET MVC

Hi,

I've been doing a little bit research in the area of mocking frameworks for different platforms and picked in total 3 clear winners:

- Java: Mockito
- .Net: NUnit (it's there anyway...) and Moq

Here you'll find the new NUnit test template for ASP.NET MVC that also contains a reference to Moq for convenience. Other than that it's the same template as the previous one. However expect that to change in the near future as I'll be porting the manual mocks in AccountControllerTest to Moq.

ASP.NET-MVC-NUnitAndMoqTemplate.zip


Have fun!

Splitting an ASP.NET MVC application into modules

I've been playing a little bit with the concept of splitting an ASP.NET MVC application into some sort of modules so that the final product can be developed separately and (possibly) reused.

Splitting the code into class libraries is easy - you just reference the class library from your main project and that's it. The tricky part is to get the pages into the main project in some usable way.

To solve this riddle I've first created an example ASP.NET MVC application and a class library in the same solution. The class library has been given the following dependencies:

- System.Web.Abstractions
- System.Web.Mvc

and by doing so I was able to effectively create controllers in my class library.

As for the pages I've created a folder called Views in the same way it's done in a regular ASP.NET MVC application but this time in the class library itself. The content of this folder is pretty much the same as with the regular application (\Views\controllername\viewname.aspx).
To get the pages copied to the main project I've used the AfterBuild target as follows:

<Target Name="AfterBuild">
<Exec Command="xcopy /Y /E /R /I Views ..\MVCPluginExample\Plugins\Views" />
</Target>

Hint: to get IntelliSense to work I needed to copy the Web.config from main project to the root of the class library. It works like a charm!

Furthermore I didn't want the "plugin" (so to speak) to be directly referenced from the main project (so that I can really plug'n'run additional parts of the application as needed). To achieve that I've created additional Exec tasks in the AfterBuild target:

<Target Name="AfterBuild">
<Exec Command="xcopy /Y /I /R $(OutputPath)$(AssemblyName).dll ..\MVCPluginExample\bin" />
<Exec Command="xcopy /Y /I /R $(OutputPath)$(AssemblyName).pdb ..\MVCPluginExample\bin" />
<Exec Command="xcopy /Y /E /R /I Views ..\MVCPluginExample\Plugins\Views" />
</Target>

With that at hand I'm able to debug the code in the class library because of the presence of .pdb file and all my views are nicely packaged in separate plugin projects.

But that's not all. To be able to use the views from controllers contained in the library (and to have them in a separate location so that I can easily clean up the main project) I was forced to use constructs as follows:

public ActionResult Index() {
return View("~/Plugins/Views/Home/Index.aspx");
}

Naturally it's a mess and one should never need to specify the full path to the view. It just doesn't make any sense.

It turns out that the list of locations where the MVC engine looks for views is stored in three fields: MasterLocationFormats, ViewLocationFormats and PartialViewLocationFormats (as per WebFormViewEngine.cs lines 23, 28 and 35). Since all we want to do is to instruct the engine to look for views in one more location (~/Plugins/Views/...) all we have to do is to inherit from WebFormViewEngine class and create a constructor that will provide this information. Here's the piece of code that does just that:

public class PluginAwareWebFormViewEngine : WebFormViewEngine {
public PluginAwareWebFormViewEngine() : base() {
ViewLocationFormats = new[] {
"~/Plugins/Views/{1}/{0}.aspx",
"~/Plugins/Views/{1}/{0}.ascx",
"~/Views/{1}/{0}.aspx",
"~/Views/{1}/{0}.ascx",
"~/Views/Shared/{0}.aspx",
"~/Views/Shared/{0}.ascx"
};

PartialViewLocationFormats = ViewLocationFormats;
}
}

Now we can finally get back to providing view names instead of file names in the controller action methods:

public ActionResult Index() {
return View("Index");
}

or even

public ActionResult Index() {
return View();
}

To register this new ViewEngine add the following line to Application_Start event handler in Global.asax.cs

ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new PluginAwareWebFormViewEngine());

What this does is it makes sure that there's only one view engine that is in fact modified by us.

Here you can download the complete solution.

mvc-plugin-example.zip


Have fun!