{"id":695,"date":"2009-10-06T22:28:55","date_gmt":"2009-10-06T21:28:55","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=695"},"modified":"2021-03-11T09:44:57","modified_gmt":"2021-03-11T08:44:57","slug":"db4o-persistent-classes-tips","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2009\/10\/db4o-persistent-classes-tips\/","title":{"rendered":"db4o: Persistent Classes Tips"},"content":{"rendered":"<p>In the last four posts I\u2019ve mostly talked about db4o itself and how to used it. \u00a0All this is actually just the necessary evil for our goal, to persist our data. So here are some very basic tips how to model your persistent classes for db4o (well it applies also to other technologies).<\/p>\n<p>(All posts of this series: <a href=\"?p=620\">the basics<\/a>, <a href=\"?p=637\">activation<\/a>, <a href=\"?p=654\">object-identity<\/a>, <a href=\"?p=671\">transactions<\/a>, <a href=\"?p=695\">persistent classes<\/a>, <a href=\"?p=733\">single container concurrency<\/a>, <a href=\"?p=744\">Queries in Java, C# 2.0<\/a>, <a href=\"?p=779\">client-server concurrency<\/a>, <a href=\"?p=840\">transparent persistence<\/a>, <a href=\"?p=949\">adhoc query tools<\/a>)<\/p>\n<h3>Don\u2019t Go Berserk With Your Data-Model a.k.a KIS<\/h3>\n<p>My first advice is extremely general and applies to nearly all engineering areas: Keep It Simple (KIS). Keep your data-model as simple as possible to do the job. Well it\u2019s might fun and challenging to show your great intellect by designing the most awesome data-model ever. However it will almost certainly bite you later when you have to maintain the application.<\/p>\n<p>Why do I tell this here? Well db4o makes it quite easy to just create a ton of classes and store a complex object graph. But only because its easy to store and retrieve objects\u00a0it doesn\u2019t solve your data-modeling challenges by magic. You still need to evaluate, test and manage your data-model very careful. Otherwise it will end in a mess.<\/p>\n<div id=\"attachment_696\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-architects.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-696\" class=\"size-medium wp-image-696\" title=\"db4o-architects\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-architects-300x96.png\" alt=\"db4o data-model keep it simple\" width=\"300\" height=\"96\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-architects-300x96.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-architects-1024x328.png 1024w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-architects.png 1200w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-696\" class=\"wp-caption-text\">db4o data-model keep it simple<\/p><\/div>\n<p>Ok, now to more concrete tips.<\/p>\n<h3>Be Careful With Framework Classes<\/h3>\n<p>Let\u2019s take a look at this innocent looking example. It\u2019s just a very simple class which holds a color. Since\u00a0it is a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Windows_Presentation_Foundation\">WPF<\/a>-Application why not using the existing Color-class? Here\u2019s the code:<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">using<\/span> System.Windows.Media;\r\n\r\n<span class=\"kwrd\">namespace<\/span> Db4OSeries\r\n{\r\n    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> SimpleObject\r\n    {\r\n        <span class=\"kwrd\">public<\/span> Color BackGround { get; set; }\r\n    }\r\n}<\/pre>\n<p>So you store some colors. It may works fine for a while. However when you take look with the ObjectManager (a tool to view the database) at your database, you notice that there are lots of classes, like Uri, UriParser, Win32.SafeHandles etc. How the hell got those classes into your database? You just stored some SimpleObject-instance with a Color. The answer is within the Color-Class. The Color class may look like it is simple class. However it can hold a reference to a Color-Context, which holds information about how to render colors down to references into the Operation-System.\u00a0<\/p>\n<p>So what\u2019s the conclusion? Be VERY, VERY careful when you pull in\u00a0framework classes. Normally you don\u2019t have any clue what such a\u00a0class actually stores. That\u2019s information-hiding at work, which is a core-principal of object-oriented programming.<\/p>\n<p>Furthermore since you don\u2019t have the control of such classes, they might change from version to version. So your application might suddenly breaks.<\/p>\n<div id=\"attachment_697\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-framework-classes.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-697\" class=\"size-medium wp-image-697\" title=\"db4o-framework-classes\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-framework-classes-300x126.png\" alt=\"db4o carful with framework classes\" width=\"300\" height=\"126\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-framework-classes-300x126.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-framework-classes.png 951w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-697\" class=\"wp-caption-text\">db4o carful with framework classes<\/p><\/div>\n<p>You could resolve this problem above by writing your own Color-class. (Which I\u2019ve done in my application). Another approach could be to write a <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/implementation_strategies\/translators.html\">custom object-translator<\/a> which only stores the relevant information and doesn\u2019t store references to system-resources.<\/p>\n<h3>Design Your Data-Model Traversing Friendly<\/h3>\n<p>To illustrate this point I go back for a moment to Hibernate &amp; Co. With a object-relational-mapper following many relations is normally a very expensive operation. So I often didn\u2019t actually use the relations and preferred queries instead. So when I designed my persisted classes for Hibernate I often didn\u2019t put every possible relation in there.<\/p>\n<p>Now object-databases are damn good at traversing along the object-references\/-relations. So when you&#8217;re designing your persisted classes you should keep in mind how you traverse them later. Really focus on the relations between objects and how they interact.<\/p>\n<p><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-navigate.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-699\" title=\"db4o-navigate\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-navigate.png\" alt=\"db4o-navigate\" width=\"600\" height=\"361\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-navigate.png 600w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-navigate-300x180.png 300w\" sizes=\"(max-width: 600px) 100vw, 600px\" \/><\/a><\/p>\n<p>So my approach is this. I develop first a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Domain_model\">domain-model<\/a> of the problem I try to solve. As soon as I\u2019m happy with my domain-model I begin to draw the directions I need to navigate through the model.<\/p>\n<p>An example. I\u2019ve a color, a car and a driver as domain-objects. Do I ever want to know the cars color? Almost certainly. So I draw a navigation-arrow from the car to the color. Do I ever want to know the colors cars? Probably never. So no navigation-arrow back. With the approach I complete the model step by step\u00a0with navigation-arrows. After that I\u2019ve a really good idea what I need to persist and which references I need to store.<\/p>\n<p><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/domain-model.gif\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-698\" title=\"domain-model\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/domain-model.gif\" alt=\"domain-model\" width=\"515\" height=\"326\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/domain-model.gif 515w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/domain-model-300x189.gif 300w\" sizes=\"(max-width: 515px) 100vw, 515px\" \/><\/a><\/p>\n<h3>Know Your Compiler \/ Language<\/h3>\n<p>Ok, this is only important in certain scenarios. Again a simple example. I\u2019ve a class which I represents a person.<\/p>\n<pre class=\"csharpcode\">    <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">class<\/span> Person\r\n    {\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">string<\/span> Name { get; set; }\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">int<\/span> Age { get; set; }\r\n    }<\/pre>\n<pre class=\"csharpcode\">\u00a0<\/pre>\n<p>Now you&#8217;ve decided to index the Person.Name-property. Now you have to know two things. First is that db4o works with the actual fields of the class. Therefore you\u00a0declare the indexes at the field-level.\u00a0(There&#8217;s an attribute\/annotation for that).\u00a0But how the hell do you know what fields this class has? Here you really need to know some details of your compiler \/ language. In this case it the field for the .Name-property is \u2018&lt;Name&gt;k__BackingField\u2019. Other .NET-languages might show an other behavior.<\/p>\n<div id=\"attachment_700\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-compiler.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-700\" class=\"size-medium wp-image-700\" title=\"db4o-compiler\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-compiler-300x116.png\" alt=\"db4o compiler, language, platform\" width=\"300\" height=\"116\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-compiler-300x116.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/db4o-compiler.png 1000w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-700\" class=\"wp-caption-text\">db4o compiler, language, platform<\/p><\/div>\n<p>Why the hell is this so ugly? As said db4o runs for nearly every Java and .NET version. So the core of db4o is build for the least common features. On top of this are some platform-specific additions like LINQ for .NET. However sometimes some stuff isn\u2019t yet pretty for your platform. Like this auto-property example. So when you hit such a case you might need to know a little more about your language, compiler etc.<\/p>\n<p>To solve the problem above I use a small utility:<\/p>\n<pre class=\"csharpcode\"><span class=\"kwrd\">    public<\/span> <span class=\"kwrd\">class<\/span> Person\r\n    {\r\n        [Indexed]\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">string<\/span> Name { get; set; }\r\n        <span class=\"kwrd\">public<\/span> <span class=\"kwrd\">int<\/span> Age { get; set; }\r\n    }<\/pre>\n<p>And then use a extension-method which takes a type. The extension-method adds the indexes according to the attributes:<\/p>\n<pre class=\"csharpcode\">var cfg = Db4oEmbedded.NewConfiguration();\r\ncfg.Common.IndexClass(<span class=\"kwrd\">typeof<\/span>(Person));\r\nvar db = Db4oEmbedded.OpenFile(cfg, <span class=\"str\">\"database.db4o\"<\/span>);<\/pre>\n<p>The source-code for the utility is at the end of this post.<\/p>\n<h2>Read\/Try\/Ask<\/h2>\n<p>This is it for today =). Anyway there are lots of tips and advices out there. In the db4o documentation are some advices how to <a href=\"http:\/\/developer.db4o.com\/Documentation\/Reference\/db4o-7.12\/net35\/reference\/html\/reference\/implementation_strategies\/type_handling.html\">store some types like Blobs, Collections etc<\/a>. On this site are some <a href=\"http:\/\/www.odbms.org\/downloads.aspx#odbms_pp\">patterns for persistence<\/a>. However those patterns are mostly for very specific problem which I haven\u2019t encountered yet.<\/p>\n<h3>Next Time<\/h3>\n<p>Concurrency, oh no, concurrency. =( Next time I give a brief introduction about concurrent operation on a db4o database.<\/p>\n<p>Index-Utility: <a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/DBSchemaUtility.cs\">DBSchemaUtility.cs<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the last four posts I\u2019ve mostly talked about db4o itself and how to used it. \u00a0All this is actually just the necessary evil for our goal, to persist our&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[150],"tags":[21,100,295],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/695"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=695"}],"version-history":[{"count":19,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/695\/revisions"}],"predecessor-version":[{"id":3833,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/695\/revisions\/3833"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}