Subscribe!

Don't forget to...

 Subscribe

Monday, June 27, 2011

Today I populated an html div element asynchronously using jQuery and JSON in my ASP.NET MVC2 project. Now, when you were 10 years old, did you think you'd one day find a sentence like that above interesting? I bet not. But here's why it is (lol) :

In MVC you're allowed to mix javascript code (client side) with C# code (server side), but you have to understand where each part of it is being run. Here's the code (the important parts of it anyway):

Client side (mostly):

<script type="text/javascript">
$(document).ready(function() {
$.getJSON('<%=Url.Action("GetJsonDriveList", "DiskSpace") %>', function(jsonData) {

for (i = 0; i < jsonData.length; i++) { $.get('<%=Url.Action("LoadDiskSpace", "DiskSpace") %>?serviceAddress=' + jsonData[i].serviceName, function(data) {
$("#mainDiv").append(data);
});
}
});
});
</script>

Server side :
public class DiskSpaceController : Controller {
public JsonResult GetJsonDriveList()
{
return Json(GetDriveList(), JsonRequestBehavior.AllowGet);
}

public List GetDriveList() {
// yada yada
}

public PartialViewResult LoadDiskSpace(string serviceAddress)
{
var drives = GetDiskSpace(serviceAddress);
var aDriveModel = new DiskDriveModel(drives[0]);

return PartialView("_Drive", aDriveModel);
}
//........
}

jQuery : javascript library that makes it easy to do asynchronous server requests and select elements on a webpage.
JSON: JavaScript Object Notation. Makes it easy to convert C# objects to Javascript objects and vice versa.(e.g. list converts to javascript Array()....etc.)

OKAY!

So first off the client side stuff. That's a script tag you can put on a view (MVC term) and it'll run when the page loads. (OH make sure you've referenced jQuery.js, microsoftAjax.js and microsoftMvcAjax.js first, btw). Everything inside the <% %> tags runs SERVER side, and the other stuff runs client side. And the Url.Action call might seem wierd, but all it does is construct a url that looks like "DiskSpace/LoadDiskSpace" and the "?serviceAddress=..." is the querystring that will be sent with the jQuery GET request.

So the $(document).ready part only runs when the DOM (document object model) is ready. This is the first thing you do ANYtime you run jQuery, as far as I know. Then $.getJSON sends a request to the server and expects a result in the form of a JSON object. When the result is returned, the callback is run, and in the callback we send another request, this time for a PartialView to be inserted into our div element with id "mainDiv".

Now the server side stuff. There's some stuff in MVC that maps the request (e.g. "DiskSpace/GetJsonDriveList") to an action method in a controller. "DiskSpace" is the controller (or the class, to put it simply) and "GetJsonDriveList" is the name of the action method to run and get the result from. So the method is run, and returned as a JSON object using Json(ObjectToReturn, JsonRequestBehavior.AllowGet). The "AllowGet" cannot be left out or else you won't get anything returned and will stare at the screen for hours in a state of frustration!! And then, when the client side receives it, it can be interpreted easily. The next action method that's called has the same idea, but without the json : it creates a View with PartialView("ViewName", modelObject) and then returns it to the client side.

MVC makes web development easy...but jQuery and JSON make it easier! If you wanna make a webpage ever (and you don't know ruby on rails), d/l the plug-in from pluralsight and watch the free screencasts, they're SUPER easy to follow and get you started quickly.

Hope this was...entertaining? ... lol

Tuesday, June 14, 2011

Mono for Android



I'm excited.

I've recently been looking for a way to motivate myself to learn better software development practices, and stumbled on something called mono. No, I didn't get it from kissing someone. Mono for Android is sort of a Visual Studio plug-in which allows one to develop Android applications in C# ! I'm SOOOO happy about this.

Personally, I learned Java in school and tried out Android development for a couple weeks, and liked it. Unfortunately, Java is going the way of the dinosaur, and it seems that .NET is taking it's place. That's why I decided to pursue learning C# instead of Android.

Now that mono has arrived and (hopefully - I'm updating the API's as I type) works, I can pursue BOTH Android development AND .NET ! Basically this means I can get good at working for the man, and developing for myself.

I'm super excited.

For a brief history of mono's inception, check out this blog:

http://www.koushikdutta.com/search/label/Mono

AND if you're a .NET dev and wanna try it out, the trial version is FREE :

http://mono-android.net/DownloadTrial

Sunday, June 12, 2011

App.Config files

Dear all,

when you use a config file for a Visual Studio application, make sure to do a few things:

1.) Make sure to create an identical app.config file for your test project. Otherwise, when running tests, the config file will not be found.

2.) If you're going to use ConfigurationManager, you have to add a reference to it under the references tab in Solution Explorer.

Cheers!

Tuesday, June 7, 2011

NAnt for automating builds in C# works fine....

I've been learning NAnt the past few days, and it's really quite simple.  Unfortunately, using it with NUnit isn't.  I'm supposed to be able to use NAnt to build my projects automatically and test them too, all with one quick call to a batch file:

./build.bat test

where build.bat just calls nant.exe and passes it a buildfile, which is an xml file that includes "targets" for nant to run.  NAnt compiles my project and my test project just fine, but then when NUnit runs, I get some messed up errors.  I can run my tests properly in Visual Studio using ReSharper, and they behave properly, but it doesnt work on the command line using NAnt...what the crap is up?!

A couple notes for you:
my folder structure is as follows :
src : contains application projects in one folder (app) and test projects in another (test)
build : should be deleted and rebuilt every time NAnt runs
config : contains config files for the project
tools : contains the NAnt folder and the NUnit folder including the exe's and dll's
lib : 3rd party libraries
base : contains the above folders, as well as the .sln, the batch file and the buildfile for NAnt.

There were a few solutions that seemed to work for various scenarios, but I think the one that will work most consistently (and unfortunately is a bit cumbersome) is this:

Use NAnt to clean and rebuild your projects and tests into a single folder (/tmp).  Then, place all the NUnit dll's directly into that same folder.  Any references to NUnit should point to the copy in the tools folder.  Then, when everything is built and the NUnit dll's are placed in the tmp folder, run the tests from the base directory through NAnt by creating a new "target" in the buildfile that runs nunit-console.exe.  The target should have no dependencies. You may also need to add this to the nunit-console.exe.config file:

underneath the configuration opening tag put:
<startup><supportedruntime version="v4.0.30319"/>
and under the runtime opening tag put:

<loadfromremotesources enabled="true" />
I'll have to do this everytime I build, until I figure it out...lame.

P.S. I learned to use NAnt from my bro, and from here:

http://blog.developwithpassion.com/2006/04/automating-your-builds-with-nant-part-1