Monday, May 26, 2014

Porting an MVC4 App from VisualStudio to MonoDevelop

I recently created a demo shopping cart application using VisualStudio Express 2013 and MVC4. This meant spending some time in Windows.  But, I was quite impressed with MVC4. This is a vast improvement over the old viewstate based web apps. Plus, there is asset bundling support. This was worth the time spent using Windows.

When I was done, I wanted to log back into my Linux machine and port my project to Mono. The first thing I did was upgrade to Mono 3 and MonoDevelop 5. And then I set about porting my demo. MonoDevelop only has MVC3 templates, so first, I had to find an MVC4 template, Now I was ready to get started.

With most IDE's, you can just drop files into a folder and forge ahead. But MonoDevelop uses the VisualStudio project structure that mirrors the file system. So I  fired up MD, loaded my template, renamed it, and deleted all of the boilerplate. Then I visited each project folder, right clicked, selected Add Files from Folder, and dragged in all of the source from my original project.  This worked for everything except the Model.

Since Linux doesn't support MSSQL, I decided to use MySQL, with NHibernate for my ORM. I prefer a database first design process, so the first thing I did was update the sql script that creates my database and stored procs. Then I copied in the domain objects created by EntityFramework, changing the properties to virtual. The real work was creating the xml bindings -  but I actually prefer doing it manually, it is much easier to update than the tedious VisualStudio process of deleting and rerunning the wizard. Plus, you have much more control over the final product.

Thas is it. You can see the result at http://monomart.apphb.com. And compare the projects on github. The original VS project is at https://github.com/darkoverlordofdata/minimart, and the MonoDevelop project is at https://github.com/darkoverlordofdata/monomart.

Thursday, May 22, 2014

Deploy Mono MVC4 to AppHarbor

After upgrading to MonoDevelop 5,  (see my previous blog: Upgrading to MonoDevelop 5.1 in Linux Mint 16) you'll want to create an mvc4 app. But MD5 only comes with mvc3 templates. No problem. Chris F Carroll has published just the template you'll need. Clone this github project: Asp.Net MVC4 C# Razor Template for Mono on Mac and Linux

This template worked for me right out of the box. But when I deployed it to AppHarbor, it failed. The log shows this error:

error CS0234: The type or namespace name 'Mvc' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)

I actually got the same error using the mvc3 templates that ship with MonoDevelop. This is due to some missing tags in the Web.config file. Find this section:

   <compilation debug="false" targetFramework="4.0">
      <assemblies>


You'll need to add these tags:

        <add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        <add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />


Now re-deploy your site. You'll find that these tags will disappear any time you modify your project references. I keep a copy inside a comment tag inside the web.config file, so I don't have to go looking for them every time I update my project.

Wednesday, May 21, 2014

Upgrading to MonoDevelop 5.1 in Linux Mint 16

Linux Mint, being based on Ubuntu, is stuck with an old 2.10.8.1 version of Mono, and an equally old version of MonoDevelop. So, using this posting on stackoverflow, I've upgraded to the latest versions of Mono runtime and MonoDevelop.

First, the runtime:

sudo add-apt-repository ppa:directhex/monoxide

In Software Manager, edit Software source PPA's for monoxide, changing saucy to raring to select the 13.10 Ubuntu branch. Now upgrade:

sudo apt-get update && sudo apt-get dist-upgrade

At this point, I am running MonoDevelop 5.1, and I can compile and run desktop apps.
Next,  to use ASP.NET, we meed to get the web server running:


sudo apt-get install autoconf automake libtool g++ gettext libglib2.0-dev libpng12-dev libfontconfig1-dev
cd Git
git clone git://github.com/mono/xsp.git
cd xsp
./autogen.sh --prefix=/usr
make
sudo make install
sudo mkdir /etc/mono/registry
sudo chmod -R 0777 /etc/mono/registry

I can now create and run ASP.NET apps.  Check out my demo shopping cart app - Monomart.

Monday, May 19, 2014

Why Use Stored Procedures?

It's an old debate. Google 'why use stored procedures' and you'll get a million hits and a million opinions.  Many of the answers are technical in nature, relating to performance issues. The rest are pro/con discussions about where and how you want to code and debug your business logic, or how ugly stored procedures are compared to an elegant ORM. Along those lines, here is my favorite rant Who Needs Stored Procedures, Anyways?

But most of those reasons don't really matter. because the most compelling reasons to use sprocs are not technical at all, they are administrative.

Reason 1:
Data integrity. Say for example, your app has 1,000 tables. Don't laugh - SAP has way more tables than that. Your shop has a dev pool of 6 positions with an average job turnaround of 6 months. If those developers are maintaining embedding SQL into the application code, inconsistencies will crop up. If you are using sprocs, then you can have some assurance that the DAL isn't impacted.

Reason 2:
The business. Remember them? They are why you have a database. Suppose you have a business role that audits business line items, but isn't allowed to view the cost field in the line item table. Table level security won't help. Even if your app doesn't expose the cost, a clever power user will figure out how to download the table in MS Access, and then they can view the cost. You need sprocs or views to control that.

In light of business reasons like these, does it matter that stored procedures can't pass objects? Only to coders.