Browsing:

Month: August 2010

Back to basics: POCO and One to Many relationships (and db4o)

Here is another back to basics post. Howto 'do' one-to-many when you are not having a RDBM and using POCO?

Here is the (very simple) model:

For persistance I use Db4o in this example. Please see this blogpost.

One blogentry has many comments. So, we'll put comments in a List as a attribute of blogentry:

 public class BlogEntry
        {
            public string blogtitle { get; set; }
            public string blogstory { get; set; }
            public DateTime postdate { get; set; }
            public List<Comment> comments { get; set; }

            public BlogEntry()
            {
                this.postdate = DateTime.Now;
            }

        }

Here is the comment class:

 public class Comment
        {
            public string commenttext { get; set; }
            public string email { get; set; }
            public BlogEntry blogentry { get; set; }
           
        }

Now we are ready to build our console based blog application:

static void Main(string[] args)
 {
   using (var s = SessionFactory.Current)
    {
       //DELETE
       //s.DeleteAll<Comment>();
       //s.DeleteAll<BlogEntry>();

       //ADD
       var b = new BlogEntry { blogtitle = "Lady Gaga FTW",
       blogstory = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book"
       };

       var c = new Comment { blogentry = b, commenttext = "inderdaad", email =  "info@test.com" };  
       var d = new Comment { blogentry = b, commenttext = "niet mee eens", email =  "info@krap.com" };


       List<Comment> commentlist = new List<Comment>();
       commentlist.Add(c);
       commentlist.Add(d);
       b.comments = commentlist;

       s.Save(b);
       s.CommitChanges();
                
       //RETRIEVE (2 foreach loops)
       var blog =  s.All<BlogEntry>();
         foreach (BlogEntry ble in blog) {
                    Console.WriteLine(ble.blogtitle);
                    Console.WriteLine(ble.blogstory);
                    Console.WriteLine(ble.postdate);
                    foreach (Comment cmt in ble.comments)
                    {
                        Console.WriteLine("Comment: ");
                        Console.WriteLine(cmt.commenttext);
                        Console.WriteLine(cmt.email);
                        Console.WriteLine("---");
                    }
                    Console.WriteLine("########");
                }
            }

So, when I execute this application a few times, the db40 file gets a bit stuffed and I get the following output, which is a full blown console blog application:

Hmm.
Oh well, I said it was a back to basics post, didn't I. 🙂


Tooltip for this month SSMA for MySQL

Since Sun MicroSystems has been aquired by Oracle, Some people have had their doubts about the future of MySQL, ofcourse there is MariaDB, a fairly new MySQL solution founded by the masterbrain behind MySQL, Monthy. But it just doesn't do it 'yet' for the bigger audience, i never heard a client ask for a MariaDB database solution, the questions are always the same, Are we going to use Oracle or Microsoft and what are the organization costs to implement and support?

SQL 2008 Express

If you are a personal or a non heavy user of databases Microsoft released the MSSQL Express edition, a free SQL database version that supports databases up to 10 GB. Read more about the free Microsoft SQL 2008 R2 Express here. And to make it even easier for you, Microsoft has now released the MySQL migration Assistent (SSMA) at the 12th of August. The SSMA tool is available for servers SQL2005, 2008, 2008 R2 and SQL Azure and is compatible with MySQL version 4.2 and higher, also suitable for 64 bit platforms. So what's keeping you up? For everyone who still has doubts, Microsoft will now convince you. After a SQL migration assistant for Oracle, Access and SyBase, MySQl has been added to this list. A pretty smart move for Microsoft to generate even more new clients, who still had cold feet, thinking about costs and downtime of the servers, there is no longer an excuse. Microsoft is listening, improving and on the move!


Ruby on Rails envy part 2 – The first webapplication

Fire up Putty and log on to our newish ROR virtual appliance!

Oh wait. First I have to think of something we are going to create. So how about a Marketplace app, where supply and demand meets? And since we're so into a free society, let's make it an app where no money is allowed. So we're going to exchange goods and services. What do we need?

  • List all open requests
  • Create a new request
  • Read and display a request
  • Update the request
  • Delete the request

That will be enough for now. So, what are the features of the request?

  • name - name of the requester (string)
  • contents - the textbody of the request (text)
  • title - the title of the request (string)

Now start up Putty and log on to the BitNami server.

cd /opt/bitnami/projects
rails fleemarket

This is something like File -> New Project -> ASP.NET MVC 2 application in Visual Studio. With this command, you'll create the complete web application in a folder 'fleemarket'. If I cd into this new folder I get to see:

root@linux:/opt/bitnami/projects/fleemarket# ls
app     db   lib  public    README  test  vendor
config  doc  log  Rakefile  script  tmp

Right, now let's run it. After all, in VS I can also start debugging right after the creation of my project:

ruby script/server

Anyhow, we'll get the following output as the server is starting. So we need to open up another instance of Putty for editing.

root@linux:/opt/bitnami/projects/fleemarket# ruby script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

So, let's create the Request class first.

root@linux:/opt/bitnami/projects/fleemarket# ruby script/generate scaffold request requester:string title:string contents:string
      exists  app/models/
      exists  app/controllers/
      exists  app/helpers/
      create  app/views/requests
      exists  app/views/layouts/
      exists  test/functional/
      exists  test/unit/
      create  test/unit/helpers/
# etc 

So, Rails created a controller and a view apparently. And now for the database.

root@linux:/opt/bitnami/projects/fleemarket# rake db:migrate
(in /opt/bitnami/projects/fleemarket)
==  CreateRequests: migrating =================================================
-- create_table(:requests)
   -> 0.0040s
==  CreateRequests: migrated (0.0042s) ========================================

I am done. And then check out what I've just created. A complete CRUD thing with a database.

Next time, let's check out what just happened under da hood.


Ruby On Rails Envy – how to set up a ROR environment (with BitNami)

From the moment I read about ScottGu sitting in a plane and setting the outline for ASP.NET MVC, I couldn't help thinking: "This is a serious case of Ruby On Rails Envy." I know, the MVC pattern is very old indeed, but I thought it is more than coincidental that, with the success of ROR, Microsoft comes up with an alternative. Oh well. Just my humble opinion.

I was writing webapplications with the Ruby on Railsframework back then (I think it is 2008) but as ASP.NET MVC matured I switched. Because the companies I work for do have a Windows environment ready for me, so practically I had to. And I never looked back at ROR. Which is a shame, because Ruby.Is.Beatiful.

So let's get our hands dirty and get started with Ruby On Rails!

But wait. What? Where? How?

Setting up the Rails environment

If you are on a Mac or on Linux it's relatively easy to set up a Rails environment. If you're on Windows it is less comfortable, because it's slow. And as for editing, when you're on a Mac, the TextMate editor is the absolute bomb.
My MacBook is in the living room playing Spotify songs at the moment, so let's take the Linux road today in a virtual environment. Let's use vim as a texteditor.
This is going to be lots of fun!

Yeah, okay, but I'm a bit lazy and after all, it's Sunday evening.

Right. Let's download a virtual machine that runs Ubuntu Server with a complete Ruby environment. Please do so and we'll start the tutorial from there.

After booting the virtual machine, this what you'll see:


Login with username 'bitnami' and password 'bitnami'.
You''ll be prompted to change the password.
Next, change to root. To do so type 'sudo -s' :

Basic Linux housekeeping for starters:

sudo -s
apt-get update
apt-get upgrade
# install the vim editor
apt-get install vim
# set the network interface:
vim /etc/network/interface

Now the interfaces file opens. The IP address is set to dhcp by default, which we'll change to static.
Hit the INSERT key to edit the file:

# The primary network interface
auto eth0
iface eth0 inet static
        address 192.168.201.6
        netmask 255.255.255.0
        network 192.168.201.0
        broadcast 192.168.201.255
        gateway 192.168.201.1

Now hit ESCAPE followed by :w and :q (for writing and quitting vim).
Set the DNS Server in /etc/resolv.conf:

vim /etc/resolv.conf
nameserver 62.45.191.241
nameserver 62.45.191.242

Now it's time to restart the network:

/etc/init.d/networking restart

Now check with ifconfig if eth0 has the correct IP address. Because if this is the case, we can leave the VMWare console for what it is and connect to the virtual machine using Putty.
Oh. Apparently ssh is disabled on the virtual machine. Go:

mv /etc/init/ssh.conf.back /etc/init/ssh.conf
start ssh

Now we can connect to the appliance using Putty.
Accept the fingerprint securitywarning and login with bitnami and your password. Switch to root immediately with sudo -s (I always do so).

Eeuw, this is so boring. Where are those fancy dircolors? (Note: in some versions the dircolors are already enabled)

cd /home/bitnami
vim .bashrc
#paste this in the file:
if [ "$TERM" != "dumb" ]; then
    [ -e "$HOME/.dircolors" ] && DIR_COLORS="$HOME/.dircolors"
    [ -e "$DIR_COLORS" ] || DIR_COLORS=""
    eval "`dircolors -b $DIR_COLORS`"
    alias ls='ls --color=auto'
    #alias dir='ls --color=auto --format=vertical'
    #alias vdir='ls --color=auto --format=long'
fi

that's much better.

So, how about Ruby On Rails then?
The Bitnami Ruby Stack came with an example project. Let's start that up and check it out:

cd /opt/bitnami/projects/rubystack-2.1-0
#start the app:
ruby script/server
-bash: ruby: command not found

Hmm, that's not working. We should add Ruby to the path.

vim /etc/environment
#hit INSERT to edit:
PATH="/opt/bitnami/ruby/bin/:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
#hit ESCAPE followed by :w and :q

Now log off, log on and try again:

root@linux:/opt/bitnami/projects/rubystack-2.1-0# ruby script/server
=> Booting Mongrel
=> Rails 2.3.5 application starting on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server

And now we can access the application at http://your-vm-ip:3000.

So, everything works apparently. Next time we are going to create our first ROR project.