I am attempting to implement a type hierarchy using the JoinedBase/JoinedKey
mechanism described in the documentation.  I need the classes to be lazy
load, however, which seems to be causing a problem, and I'm stuck trying to
resolve it.

In my sample, I have two classes: Thing and Person.  Person inherits from
Thing.  Without lazy load, I can create a person, do a find, and it returns
a Person object:

Person p1 = new Person { Id = "P1", Name = "Fred Flintstone" };
ActiveRecordMediator<Person>.Save(p1);
Thing t = ActiveRecordMediator<Thing>.FindByPrimaryKey("P1");

Without lazy load, t.GetType().Name is "Person", as I would expect.  When I
switch to lazy load, however, t.GetType().Name is
ThingProxydd0bacfc680c497ebb30e90d08faa7f1.  That is fine by itself, but
when I attempt to cast it to a Person, the cast fails.  I understand why it
generates the proxy class, but shouldn't it return a PersonProxy?

I'm not wed to the JoinedBase/JoinedKey technique - anything that will give
me a lazy type hierarchy would be acceptable.  I am using ActiveRecord 2.0.

I've included a full sample console application that illustrates the problem
at the end of this message.  This application prints out the following:

t.GetType().Name: ThingProxydd0bacfc680c497ebb30e90d08faa7f1
Name: NULL!

Thanks in advance for any assistance!

Cheers,
-Doug


======================

using System;
using System.Collections.Generic;

using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework.Config;


namespace LazyHierarchy
{
    [ActiveRecord(Lazy = true), JoinedBase]
    public class Thing
    {
        protected string id;

        [PrimaryKey(PrimaryKeyType.Assigned)]
        public virtual string Id
        {
            get { return id; }
            set { id = value; }
        }
    }


    [ActiveRecord(Lazy = true)]
    public class Person : Thing
    {
        [JoinedKey]
        public override string Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public virtual string Name { get; set; }
    }



    class Program
    {
        static void Main()
        {
            try
            {
                Dictionary<string, string> properties = new
Dictionary<string, string>();

                properties.Add("connection.driver_class",
"NHibernate.Driver.SqlClientDriver");
                properties.Add("dialect",
"NHibernate.Dialect.MsSql2005Dialect");
                properties.Add("connection.provider",
"NHibernate.Connection.DriverConnectionProvider");
                properties.Add("connection.connection_string", @"Data
Source=.\SQLEXPRESS;Initial Catalog=MyFamily;Integrated Security=SSPI");
                properties.Add("proxyfactory.factory_class",
"NHibernate.ByteCode.Castle.ProxyFactoryFactory,
NHibernate.ByteCode.Castle");

                InPlaceConfigurationSource source = new
InPlaceConfigurationSource();

                source.Add(typeof(ActiveRecordBase), properties);

                ActiveRecordStarter.Initialize(source, typeof (Thing),
typeof (Person));

                ActiveRecordStarter.CreateSchema();

                using (new SessionScope())
                {
                    Person p1 = new Person { Id = "P1", Name = "Fred
Flintstone" };

                    ActiveRecordMediator<Person>.Save(p1);
                }

                Thing t =
ActiveRecordMediator<Thing>.FindByPrimaryKey("P1");

                Console.WriteLine("t.GetType().Name: {0}",
t.GetType().Name);

                Person p2 = t as Person;

                Console.WriteLine("Name: {0}", p2 == null ? "NULL!" :
p2.Name);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to castle-project-users@googlegroups.com
To unsubscribe from this group, send email to 
castle-project-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to