Trail:

NHibernate.DataBinding

Introduction

NHibernate Extensions is meant as a repository for tools that make life easier when developing NHibernate based applications with Visual Studio .NET. For now I provide a tool called NHibernate.DataBinding?.

NHibernate.DataBinding? is a tiny NHibernate Extension that can be used to make the VS.NET GUI Builder aware of NHibernate mapped collections and thus eases Application development a lot. NHibernate mapped objects can be used as object datasources in the GUI Builder and hence the Visual Studio .NET's databinding features can be leveraged to their full extend.

A more detailed description will follow.

Licence

All work presented on http://nhibernate.fluffnstuff.org/ is made available under the Apache Licence, Version 2.0.

Screenshots

See the Screenshots section for examples.

Download

The sources are located in a subversion repository at https://secure.nme.at/svn/NHibernate/ and the .NET 2.0 binaries are downloadable at http://nhibernate.fluffnstuff.org/downloads.

Synopsis

using System.Collections.Generic;
using NHibernate.DataBinding;
using NHibernate.Mapping.Attributes;

namespace MyNamespace.Entities {
    [Class]
    public class ParentEntity {
        private int? id = null;
        private DateTime lastModified = DateTime.Now;
        private BindingSet<ChildEntity> childEntities = BindingSet.GetInstance<ChildEntity>();

        public ParentEntity() {
            childEntities.AddingNew += new AddingNewEventHandler(
                delegate(object sender, AddingNewEventArgs args) {
                    ChildEntity child = new ChildEntity();
                    child.Parent = this;
                    args.NewObject = child;
                });
        }

        [Id(0, Name = "Id")]
        [Generator(1, Class = "native")]
        public virtual int Id {
            get { return id.HasValue ? id.Value : -1; }
        }

        [Timestamp]
        public virtual DateTime LastModified {
            get { return lastModified; }
        }

        [Set(0, AccessType = typeof(BindingAccessor.CamelCase))]
        [Key(1, Column = "ParentEntity")]
        [OneToMany(2, ClassType = typeof (ChildEntity))]
        public virtual BindingSet<ChildEntity> ChildEntities {
            get { return childEntities; }
        }

        public override bool Equals(object obj) {
            Entity other = obj as Entity;
            if (other == null || id.HasValue != other.id.HasValue) return false;
            if (id.HasValue) return id == other.id;
            return this == other;
        }

        public override int GetHashCode() {
            return 0;
        }

        public virtual bool IsNew {
            get { return !id.HasValue; }
        }
    }
}