20081030

NHibernate for the Lazies

I don't know about you, but sometimes if you want to learn a new framework, you want to see results right away. Most of the time, it never happens that way. Let's take nHibernate as an example. For somebody that is very new, you go to tutorials, how to create one and hope it works. One thing that pissed me off when I was learning NH back in the days, was writing those .hbm files.

Don't you wish there was an easy way to learn nHibernate, learn how to use sessions, transactions, unit of work, and all that good stuff? Ofcourse we have fluent-nhibernate as a good way to start up. But what if (1)you're using .net 2.0? (2)you don't like how integrated the mapping is with your application?

One day, I decided to spike a generator that generates your .hbm on the fly based on your domain objects.

Word of caution. I just used this as a spike, not a solution - More like a fast way of developing a small app without the pain of .hbm.xml files. Also, this exercise shows that you can have a domain model, totally ignorant how stuff is being saved to some kind of a persistance layer - hence mapping files. And finally it's totally doable to generate a mapping file on the fly based on your domain object.

Now let's talk code. The way this exercise was done, I was taking advantage of the Chain of responsibilty pattern to build the mapping files.

Before we go with any code, lets follow the basic rules:

  1. Every .net data types are property elements
  2. Every custom objects are many-to-one elements
  3. Every IList<> are bag items


The Model


The Test


The IHandler Interface


The Mapping File Builder


The Handlers



The Implementation

20080714

SproutCore & WCF

Before you start ctrl+c & ctrl+v'ing some source code, make sure you have tried the hello world sample and read the Rest API sample on the sproutcore wiki. Also, make sure you have played with FireBug to call some of the sproutcore commands shown in the sproutcore tutorials.

Ever since WWDC 2008 announced SproutCore, I got curius with how this javascript mvc framework communicates with a server backend. Since I have very limited knowledge of RoR rest, I decided to stick to what I know - WCF.

For my sample, I am using the contacts example that is posted on the sproutcore googlecode website.


JSON & DTO's
Let's start with how the Contacts JSON object should look like:

Notice how SproutCore collections has a defined format: {"records":[], "ids":[]}, also take notice on how the properties are defined: "first_name", "last_name" - Looks like sproutcore does some nyahh and puts underscores.


Now let's look @ the ContactDTO & SproutCoreContactDTO DataContracts in C# and let's make this work for sproutcore:



This is how we build the SproutCoreContactDTO object:


WCF Service & Web.Config

Now we've built out dto and have an idea how we build our JSON object, let's make WCF do some crazyness and send and take in DTO's as JSON from something that is not a .Net proxy!

Here's the service:


Notice how my methods are List, Create, Update, Show? That's taking advantage of sproutcore's built-in Record functionality.


Note that complex types like ContactDTO being consumed as a parameter must be a WebInvoke and an HTTP-POST method - you will have pain if u dont!

The key words to return your objects as JSON in WCF is "ResponseFormat = WebMessageFormat.Json"

Web.config (system.servicemodel configuration)


Things to remember about your wcf service:
1) must be webHttpBinding to talk to any non-microsoft clients
2) must have a webHttp endpoint behavior

NUnit Tests

We could create a proxy to verify if our services work, but since we're going to be using sproutcore as our UI, we're going to do it the hard way. We're going to bust some serious System.Net skillz... soo peep this awesomeness:



Now that's some awesome shizzle! all works, and it's all good in the hood!


Apache, Mongrel, IIS - Fo Shizzle my Nizzle!

In order for your sc-server (mongrel) to have the proper resourceUrl for your WCF web service, we need to use a middleman as our reverse proxy - apache. Soo, we need to do the following:
1) download apache web server
2) make sure your port is something other than port 80 (I was running IIS and Apache on the same computer)
3) uncomment the following on httpd.conf:
- proxy_module
- proxy_ajp_module
- proxy_balancer_module
- proxy_connect_module
- proxy_http_module
4) add the following in the very bottom of httpd.conf:
#SproutCore Stuff
ProxyPass /contacts http://localhost:4020/contacts
ProxyPassReverse /contacts http://localhost:4020/contacts
ProxyPass /static http://localhost:4020/static
ProxyPassReverse /static http://localhost:4020/static
#IIS Stuff - Where my ContactService.svc is at
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/

With ReverProxy set-up, you can still go to localhost:4020, but your reference to your wcf service won't work. Depending on how you configured you apache server, you now have to access your sproutcore site via apache.


Finally! some sproutcore action!!
I'm assuming you have already exported the contacts sample from google code, and read "Using the built-in REST API".

We need to modify a couple of things so we can talk to our wcf service.

Model\contact.js

contacts\main.js




The Finished Product

With all your sc-gen, iisreset, and all that good stuff, run your site to see the final product.


And ofcourse, what's this long post without the sourcecode? download it from here or if you have svn, type "svn co http://ezed.googlecode.com/svn/spikes/SproutCore&WCF"

20080709

Boo & System.Collections.Generic

i know there is the boo.lang.hash type, but i needed to use the IDictionary for this one.

Soo....

if you're busting some C#:


and if you're freestylin' with boo...

20080619

ClickOnce and Multiple Environments...

This is dedicated to the peeps that have been googlin' how to handle multiple environments and using ClickOnce.  When I mean multiple environments, you are deploying an application on multiple servers and in different environments, and the only difference are miniscule settings on the app.config file...


If you have:

- Multiple Instances of your ClickOnce app on Server 1
http://Server1/Development/MyApp.application
http://Server1/Test/MyApp.application
http://Server1/Production/MyApp.application

- Multiple Instances of your ClickOnce app on Server 1
http://Server2/Development/MyApp.application
http://Server2/Test/MyApp.application
http://Server2/Production/MyApp.application

With the setup above, all environments also have a manifest with the same name but just signed differently.

What's whacked is even though they are different applications when they download. What happens is the first one you download, will be the default app.config. This may sound crazy, but it's true... 

After some googlin' and spiking up some shizzle, I decided to new InitializeKISSMethod() (keep it simple stupid!). And ended coming up with a naming convention of [server].[environment].application and [server].[environment].manifest.  So it would looks like:

- Multiple Instances of your ClickOnce app on Server 1
http://Server1/Development/Server1.Development.application
http://Server1/Test/Server1.Test.application
http://Server1/Production/Server1.Production.application

- Multiple Instances of your ClickOnce app on Server 1
http://Server2/Development/Server2.Development.application
http://Server2/Test/Server2.Test.application
http://Server2/Production/Server2.Production.application


--------

So if you're a developer that is very depended on VS2005 (or 2008), this may be a lot of work! I would suggest automating this - will make your life ezed!

And remember, start with mageui.exe, then go all out with mage.exe.  You will get a better understanding what's happenin' when you are bustin' some serious ClickOnce nyahhhh.

20080529

LinqToYourBrain Recap

Thanks to Edmug for organizing tonights event and thanks to all the people that came and checked out my presentation - I enjoyed presenting!


For those who are interested playing with the source code, I have posted it on googlecode. So if you have subversion type:


20080513

LinqToYourBrain

Some of you may have heard, some have not. But I will be presenting an introduction to LinqToSql on May 29th.

Thanks to Edmug for hooking me up with a spot, and finally presenting to a public and devoted audience of developers.

See you there!

20080222

My MacBook Pro is F***ing Alive!!!!

Well I was supposed to sell my macbook, but then when I had some interested buyers I found an ebay store that sold an 8x Dual-Layer Superdrive for $159USD - So I bought it!!!!

Then I decided to buy a 160GB 7200RPM hard drive, so my shit can compile faster when i'm coding.

All in all, the total cost of fixing my MBP was $314.23 which is awesome! I didn't go with buying a bottom case... just used a hammer, and a wooden train track from my kid's thomas the tank engine set to remove the dent.


And viola! It's installing Leopard as I type this!!

Notes:
- Go to ifixit.com and check out their detailed instructions how to open your MBP.
- Remember to hold the "C" button after you hear the "bong" sound - that makes your MBP boot up on you DVD drive.



Youporn! Here I come! I missed 1 week of your awesome content!!!

20080213

For Sale - Busted MacBook Pro (with Busted SuperDrive)

Once upon a time... ahhh forget it. It's for sale for $500 for parts or if you want to spend some more money, you can buy a new SuperDrive and you have a cheap macbook pro. Hard drive not included.

Here are the specs:

- MBP 1st gen.
- ATI 256MB Video Card
- 2GB Ram
- new MagSafe power adapter
- OSX Leopard DVD
- 2 Macbook Pro batteries (Holy Sh*t!)
- Some awesome white decals


Long story short, I was updating my MBP with a software update, then something messed up so I needed to re-install.

But what I noticed was that there was a dent on the bottom case, so I couldn't fit my Leopard DVD (Damn pop-can).

So I opened my MBP, fixed the case, and then found that the SuperDrive (DVD) was busted.

As much as I wanna fix it, I don't have time.

"But Jonas, what about you brining it to the shop and getting it repaired?" - Nahhhhhh... I'm due for another laptop, and have been eye'ing on a macbook (black).


So if you have skill and the $500 to buy an awesome laptop that gave me my coding skills, this is the laptop for you! All you need is to buy a new hard drive, and a new SuperDrive. And you will have an uber facebook browser.


holla at me if you wants it... Oh yea, Canada only.


Below are some pics of the MBP.