-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: How can I loosely couple database columns?
PostPosted: Fri Jan 09, 2009 9:27 am 
Newbie

Joined: Fri Jan 09, 2009 9:13 am
Posts: 4
Hi,

I'm new to NHibernate so may be trying to do the impossible. Any advice as to how to accomplish my goal with any solution would be very much appreciated.

I have two versions of an almost identical database. Below I have created an Example table to demonstrate the basic differences, namely the ID column has changed from an Integer Identity to a GUID and various properties have been updated, in the Example archived has been replaced with readOnly and hidden:

Legacy version:
Code:
        CREATE TABLE Example
        (
            --Data Identity (maps to DbId in the example code)
            Id int IDENTITY PRIMARY KEY,

            --Example columns
            SomeValue varchar(50),
            AnotherValue int,

            --Data Properties
            Archived bit
        )

New version:
Code:
        CREATE TABLE Example
        (
            --Data Identity (maps to DbId in the example code)
            Id uniqueidentifier PRIMARY KEY,
   
            --Example columns
            SomeValue varchar(50),
            AnotherValue int,

            --Data Properties
            ReadOnly bit,
            Hidden bit
        )


I want to use NHibernate to connect to one or other of these database versions. I would like to be able to tell the application which version to use through settings in a configuration file.

My initial plan was to create a common interface for the business logic and use an IoC container such as Unity to swap between the relevant concrete classes in the configuration file.

Can anyone advise if this is possible and if so how to create the relevant NHibernate mapping files?

Alternatively, can anyone suggest any solution to the problem using any other methods or tools (commercial or open source)?

Here is an example of the business logic I created to test the Unity approach:
Code:
    public interface IDataIdentity
    {
        object Id { get; }
    }

    public class LegacyDataIdentity : IDataIdentity
    {
        public virtual long DbId { get; set; }

        public object Id
        {
            get { return DbId; }
        }
    }

    public class NewDataIdentity : IDataIdentity
    {
        public virtual Guid DbId { get; set; }

        public object Id
        {
            get { return DbId; }
        }
    }


    public interface IDataProperties
    {
        bool ReadOnly { get; set; }
        bool Hidden { get; set; }
    }

    public class LegacyDataProperties : IDataProperties
    {
        public virtual bool Archived { get; set; }

        public bool ReadOnly
        {
            get { return Archived; }
            set { Archived = value; }
        }

        public bool Hidden
        {
            get { return Archived; }
            set { Archived = value; }
        }
    }

    public class NewDataProperties : IDataProperties
    {
        public virtual bool ReadOnly { get; set; }
        public virtual bool Hidden { get; set; }
    }


    public class DataItem
    {
        public DataItem(IDataIdentity dataIdentity, IDataProperties dataProperties)
        {
            DataIdentity = dataIdentity;
            DataProperties = dataProperties;
        }

        public IDataIdentity DataIdentity { get; set; }
        public IDataProperties DataProperties { get; set; }
    }

    public class Example : DataItem
    {
        public Example(IDataIdentity dataIdentity, IDataProperties dataProperties)
            : base(dataIdentity, dataProperties)
        {
        }

        public virtual string SomeValue { get; set; }
        public virtual int AnotherValue { get; set; }
    }


Many thanks,

Paul


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jan 14, 2009 8:13 am 
Newbie

Joined: Fri Jan 09, 2009 9:13 am
Posts: 4
I've looked at the possibility of using Fluent to do the mappings. Although I would prefer to have all the configurations stored in (xml) config files I'm more than happy to use Fluent if it provides a workable solution.

The main problem I found with Fluent was that I couldn't find how to tell it to map one table to separate concrete classes. So in the above example code I would want to (for the legacy version) map the database ID column to the DbId property of the LegacyDataIdentity class and the Archived database column to the Archived property of the LegacyDataProperties class. If I could do this I could possibly BuildUp the Example class with unity and then map the remaining properties.

Can anyone see a workable solution here using Fluent?

Thanks,

Paul


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.