CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Peter's Gekko

public Blog MyNotepad : Imho { }

May 2008 - Posts

  • nHibernate many-to-many collections. (OR mapping is not one table one class)

    Mapping collections in nHibernate is at first sight quite confusing. There are loads and loads of possibilities. The official documentation is somewhat academic. This is good because, once you have got it, it is a clear reference. But it is bad because the style does not always help to get it; especially when the docs start saying The previous sections are pretty confusing and try to clarify things with some short examples. Thank goodness there are more resources. Especially parts of an article nHibernate made simple by David Veeneman gave me some "aha-erlebnisse". Here I will recap parts of what I have learned and used for one of my own projects. I have picked this part as well because it does a great job in showing that OR mapping in nHibernate is more than just writing an hbm.xml mapping for every database table.

    Mapping collections

    There are several ways to describe a collection

    • A set contains unique items, the items have no order
    • A bag can contain the same item more than once, the items have no order
    • A list contains ordered items. Each item has an index in the list.

    An nHibernate mapping of a collection can even be in four different forms. Besides these three ways there is a map. A list has an integer index; a map is a list where the index has a complex type.

    These collection types map not that well on the .NET types. There is no set type in .NET and the idea of ordering is implicit. Included in the nHibernate api is Iesi.Collections which does have a set type. But nHibernate can map on just the standard .NET types. For now I will do just that. A set can be mapped to an IEnumerable the others can be mapped to an Ilist. Note that the latter implicitly introduces an order to the items in a bag.

    Mapping a collection in a domain

    I have a website which contains a listing of publications. These publications are categorized in a number of subjects. Every publication can be on several subjects. A clear domain model in code will look like this

    namespace Gekko.Website.Domain
    {
        public class Subject
        {
            public override string ToString()
            {
                return Name;
            }
    
            public virtual int Id { get; set; }
            public virtual string Name { get; set; }
        }
    }
    
    namespace Gekko.Website.Domain
    {
        public class Publication
        {
            public override string ToString()
            {
                return Title;
            }
    
            public virtual int Id { get; set; }
            public virtual bool Hidden { get; set; }
            public virtual string Title { get; set; }
            public virtual string Description { get; set; }
            public virtual string Url { get; set; }
            public virtual bool InDutch { get; set; }
            public virtual Publisher PublishedBy { get; set; }
            public virtual int PublishedInYear { get; set; }
            private IList onSubjects = new List();
            public virtual IList OnSubjects
            {
                get { return onSubjects; }
                set { onSubjects = value; }
            }
        }
    }

    This is a model I can communicate in a clear language with my customer. As stated a publication has a list of subjects.

    When it comes to storing this in a relational database things are more complicated. This is a many to many relation, a publication has several subjects, a subject has several publications. To store this we need three tables. The database will look like this

    The good thing about using nHibernate as an O-R mapper is that I don't need a class for the PublicationOnSubject table. The relation between publication and subject, which is so clear in the model, can be mapped to the underlying database as a collection. The mapping of the subject is straightforward

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Gekko.Website.Domain" namespace="Gekko.Website.Domain">
      <class name="Subject" table="Subjects" proxy="Subject">
        <id name ="Id" type="Int32" column="idSubject">
          <generator class="identity"></generator>
        </id>
        <property name="Name" type="string" length="50" column="Subject"></property>
      </class>
    </hibernate-mapping>

    In the mapping of the publication the subjects are mapped in a many-to-many collection

    <?xml version="1.0" encoding="utf-8" ?>
    <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Gekko.Website.Domain" namespace="Gekko.Website.Domain">
      <class name ="Publication" table="Publications" proxy="Publication">
        <id name="Id" type="Int32" column="idPublication">
          <generator class="identity"></generator>
        </id>
        <property name="Hidden" type="boolean" column="Hidden"></property>
        <property name ="Title" type="string" length="80" column="Title"></property>
        <property name="Description" type="string" length="150" column="Description"></property>
        <property name="PublishedInYear" type="Int32" column="PublishedInYear"></property>
        <property name ="Url" type="string" length="120" column="Url"></property>
        <property name="InDutch" type="boolean" column="inDutch"></property>
        <many-to-one name="PublishedBy" class="Publisher" column="idPublisher"></many-to-one>
        <bag name="OnSubjects" table="PublicationOnSubject" lazy="false" >
          <key column="idPublication"></key>
          <many-to-many class="Subject" column="idSubject"></many-to-many>
        </bag>
      </class>
    </hibernate-mapping>

    OnSubjects is a bag which uses the table PublicationOnSubject. The key to this linking table is idPublication. The many to many class is the Subject class we just mapped.

    Using the domain objects on a winform

    The Publication and Subject domain objects are easy and clear to use in code. The OnSubjects bag of a publication maps to an Ilist and anything which implements Ilist can be used for databinding. In both classes I have overridden the ToString method to a descriptive property. This will make databinding a snap as the default display of an object is ToString().

    This is some winforms code which combines controller and view. It uses the repositorymanger I presented in an earlier post. When loading the form a list of publications and a list of subjects are read from the repositories and bound directly to the controls. The listbox will display the Names of Subjects.

    repository = new HibernateBasedRepository();
    dataGridView1.DataSource = repository.Publications.ListAll("Title");
    listBoxPotentialCategories.DataSource = repository.Subjects.ListAll("Name");

    Likewise I can bind the OnSubjects property of the selected publication to another listbox

    listBoxAssignedCategories.DataSource = pub.OnSubjects;

    Things get even better when updating the OnSubjects property. Which is a matter of picking a Subject domain object, adding that to the OnSubjects collection of the publication and persisting the publication to the repository.

    pub.OnSubjects.Add(listBoxPotentialCategories.SelectedItem as Subject);
    repository.Publications.Save(pub);
    repository.Commit();

    Deleting a subject is a matter of removing the subject form the OnSubjects collection

    pub.OnSubjects.Remove(listBoxAssignedCategories.SelectedItem as Subject);
    repository.Publications.Save(pub);
    repository.Commit();
    The hard thing is updating the form by notifying the datasource has changed. For the sake of completeness:
    ((CurrencyManager) listBoxAssignedCategories.BindingContext[listBoxAssignedCategories.DataSource]).Refresh();

    I can't make that easier, that's just winforms.

    Winding down

    That's about it. We have been working with a clear model which an end user also understands. In my experience most people do understand how data in information systems is organized in tabular form. But when I start talking about normalized tables we lose each other. Here nHibernate's O-R mapping pushes the technical details of normalization out of the model, even completely out of the code. And also for the developer writing the code work has become clearer and easier. Would you like to write out all sql needed by hand ? Not me.

    Posted May 29 2008, 02:23 PM by pvanooijen with 5 comment(s)
    Filed under:
  • Getting started with subversion

    Good tools are half the job. Being a one person shop I believed VSS was good enough for my source control needs. Almost everybody says Subversion is a far better alternative. The main advantage mentioned is (beside stability and all that) the different and better way subversion handles multiple users working on the same file. As a one person shop that's not that important. The main downside mentioned is a somewhat steep learning curve. But recently, after another frustrating struggle with VSS, I had enough and spent my energy on an exploration of Subversion.

    It was a delight. The learning curve is by far not as steep as I feared. Besides that the documentation included is quite good. My only problem was to find a good starting point. I found this in TortoiseSVN, which presents itself as just a client but does contain everything I need. The help files tell all you want to know about Subversion and more. Here I'll just summarize the things you need for a jumpstart.

    TortoiseSVN presents itself as a shell extension. So it is available in all explorer windows. Subversions stores the sources (or any other file) of a project in a repository. A repository is a folder in the file system. The procedure to create a repository and a working copy is different than VSS, there is no such thing as "Add to source control". You have to create and fill the repository first and create a new working copy after that. These are the steps:

    • Install TortoiseSVN
    • Create a folder to store your repositories. After setting up a repository you will not work with this folder yourself. All interaction runs through the TortoiseSVN shell extensions
    • Setup a template folder. This folder describes the structure of new repositories. Recommended is the trunk/branches/tag tree you meet everywhere in subversion docs.
    • Create new empty folder for the project.
    • Right click it and pick create repository here in the TortoiseSVN context menu. This will fail when the folder is not empty.
    • Select the template folder and pick Import in the TortoiseSVN context menu. You will be asked for a repository. Select the repository folder for the project you just created.
    • Select the folder of the project you want to add to the repository. Again pick Import from the context menu. Select the repository and pick the trunk folder.
    • Create a new empty working folder for the project.
    • In the context menu pick SVN checkout and select the repository. TortoiseSVN will warn when the folder is not empty.

    And now source control is fully integrated in the windows explorer, including all dialogs. An update from or a commit to the repository are just a context click away. TortoiseSVN presents the status of the folder in icon overlays.

    You don't want all files to be under control. Because they muddle up your repository, but also because they spoil the visible status. Just building the project would turn the icon of the root folder red. TortoiseSVN has loads off ways to describe excluded files. I'm still wrestling somewhat with the list. So far I've come up with

    *.dll* *.pdb *.CACHE  _ReSharper.* *resharper* Iesi.Collections.xml Castle.DynamicProxy.xml log4net.xml NHibernate.xml

    But there will be a better way without any doubt. There is also a Visual Studio add-in for SVN, the only Subversion commercial product I know of. (Yes, also subversion is an open source project) But I haven't looked at that yet.

    I have only talked about this client tool. For the moment this does fulfill all my needs. I can reach repositories over the network using a file url like  file://HARAHARA/Users/Peter/SubversionRepositories/Gekko\Website/trunk (mind the (back-)slashes). In case I need more, there is whole load of subversion servers available for any platform.

    That's about it. Another great tool added to my box.

  • Nhibernate and medium trust

    Being sick of all the hassles it took to keep my own server up and running I've moved it to a public hoster. Which does offer asp.net hosting but like many others code is only given medium trust. My site was using nHibernate. That does have serious problems under this trust level. I've been investigating and experimenting to get it up and running. Alas without success. Nevertheless a summary of what I've done. As most web references on this subject are either vague or not specific enough this can help get a better overview. Or perhaps somebody can point me to something I have overlooked.

    Medium trust restricts the permissions given to your code. When googling a lot of the links mention the error message That assembly does not allow partially trusted callers. You can get it  when you sign the assemblies of your site. It depends on your hoster. You used to get that when hibernate tries to load a signed assembly with your domain objects. This can be fixed by setting the AllowPartiallyTrustedCallersAttribute on the loading assembly. This has been fixed on the recent production build of nHibernate; a lot of the posts still around deal with a previous version of nHibernate and are a dead end.

    The main problem with medium trust is that it inhibits reflection. nHibernate depends heavily on reflection. For lazy load to work nHibernate generates proxies to the domain objects. These proxies are built by reflecting on the domain objects. You can configure nHibernate to work without lazy loading. I gave this path a full try and tried everything I could find on the web and could think of myself.

    This is what I did to the configuration

    • Apply the requirepermission="false" in the web.config section of nHibernate
    • Set the hibernate.use_reflection_optimizer in the web.config to false
    • Set the hibernate.bytecode.provider in the web.config to null.

    This is what I did in the mapping files

    • Set the lazy attribute of all classes to "false"
    • Set the lazy attribute of all collections to "false"
    • Remove the proxy attribute of all classes

    In the domain classes I even made all public members non-virtual. In a normal scenario nHibernate will now completely trip over this, complaining it cannot create a proxy.

    On my local machine the site still worked. But on the medium trust host it immediately crashed again on a security exception. The stack trace pointed to the initialization of the reflection optimizer. Huh ? This is where I gave up.

    There is an approach on the web which promises a working lazy load even in medium trust. It is supposed to work by replacing the proxyfactory by one which provides pre generated proxies instead of generating them on the fly via reflection. Alas providing you own proxyfactory takes nHibernate 2.0. Which is still in alpha phase. From a maintainability point of view beyond consideration.

    Googling around getting nHibernate to work under medium trust is considered important but still takes a lot of effort, also in 2.0. I really hope it is going to work but for me it's back to basics for this project. Not all work has to be done again, the domain model is clear, all I have to do is rewrite the implementation of the repositories. To the UI it's not important how I gets the data as long as it gets them.

    The main reason for needing persistence on my site is an index on a selection of my publications. Reading through the results it sometimes does make my toes curl. A lot of the work is pretty old by now and my techniques have really grown over the years. Time for a good update of the contents of the underlying db.

    Posted May 09 2008, 03:35 PM by pvanooijen with 3 comment(s)
    Filed under:
  • Domain-Driven Design: Reading toward deeper insight

    On almost every family holiday I take a good book. Not being distracted by the continuous flow of the Internet and with enough time to let stuff sink in, holiday time makes a good time to see things in perspective again and come home with a fresh mind full off ideas. Last time this worked very well to give myself a real boost on building enterprise applications domain-driven style. Jimmy Nilsson's Applying Domain-Driven Design and Patterns was a quite pragmatic coding based approach which really accelerated my way of building applications. So this time I considered it time to finally do a thorough read of Eric Evans's bible of DDD

    There are plenty of reviews on the web, on Amazon alone you will find loads and loads of information on it's content and value. Besides that it has a website. Fragments of it's content is found in so many posts on Codebetter that it almost seems implicit knowledge over here. But as I enjoyed it that much I would like to draw some more attention to this modern classic itself.

    Inside you will not find a lot of code. Besides that all code is Java code; the .net platform is not even mentioned once. The book is about the essentials of code. All revolves around one central object oriented domain model. A model which serves the developer to write the implementation but it is also a model which speaks the language of the domain expert who understands the required functionality of the software. The model is described in an ubiquitous language whose meaning is understood in the same way by all team members and so serves good communication. By refactoring the developer as well as the domain expert work in cooperation on the same model. This may sound somewhat vague but chapter by chapter all aspects, from database persistence to a vision statement come by including very clear examples. Reading gave me many points of recognition as well as some aha-moments. A very pleasant moment was the conclusion of part 3, Crisis as an opportunity, where Eric mentions "punctuated equilibria", the (r)evolutionary model of a recent rant of mine. How a domain model can evolve over time also fits perfectly in that story.

    At first I only felt sorry I had waited that long to start reading (or going on a holiday :)). The book is written very clear, does give a great overview of the subject and is very systematic in making its point. It is absolutely a classic that every developer, architect (is there a real difference ?) or anyone else who is involved in building software should read. Not everybody does agree on this, between all the five star reviews on Amazon you will find some less enthusiastic ones. So, on second thought, I would not always recommend this book as a starting point for DDD. In case you have troubles reading it start Jimmy Nilsson's Applying DDD and patterns and make sure you really start doing some hands-on DDD stuff yourself. And sooner or later you will really appreciate this book as it will structure all you've read and done back into one clear and flexible model. And that's what DDD is about.

    And now you should start reading it yourself.

More Posts

Our Sponsors

Free Tech Publications

This Blog

Syndication

News