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:

[csharp]
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;
}

}
[/csharp]

Here is the comment class:

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

}
[/csharp]

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

[csharp]
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("########");
}
}
[/csharp]

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!