Chorn Sokun's Weblog

Conquer inner fear, push it to the limit!

Table Lookup & ActiveRecord Type Hierarchy

with 2 comments

In any database application I can say. We need to have a read-only list for user to pick instead of manually key in. I knew that this can be done easily using Castle.ActiveRecord so I started out like this:

Design a lookup table

“Lookup” Table Schema:

  • ID – int primary key
  • Name – varchar(50)
  • Type – Discreminator Column

Very simple.

Generate ActiveRecord classes

[ActiveRecord("List_Lookups", DiscriminatorColumn = "Type",
  DiscriminatorType = "String", DiscriminatorValue = "NA")]
public class Lookup: ActiveRecordBase{.... some code ...}

sub-class code like this

[ActiveRecord(DiscriminatorValue = "PTL")]
public class ProjectTypeList: Lookup
{
}

Testing
Lucky that I start with this: ProjectTypeList.DeleteAll();
Which cause all the records in the Lookup table deleted. WEIRD !
It suppose to delete all records in Lookup table with column Type = “PTL”

Okay, I keep testing with FindAll() method and the result was not satisfied. I start lookup for the solution in the forum finally I got the answer from Hammett’s replied.

The answer is: “Implement our own DeleteAll(), FindAll() and other method by in the derived classes.”

using Castle.ActiveRecord;

namespace PID.Net.Models{
  [ActiveRecord(DiscriminatorValue = "PTL")]
  public class ProjectTypeList: Lookup
  {     

    public new static void DeleteAll()
    {
         DeleteAll( typeof(ProjectTypeList) );
    }     

    public new static ProjectTypeList[] FindAll()
    {
         return (ProjectTypeList[]) FindAll( typeof(ProjectTypeList) );
     }

 }
}

I hope I won’t waste my time next time I tried to deal with this scenario.

Advertisements

Written by Chorn Sokun

November 1, 2007 at 4:21 pm

2 Responses

Subscribe to comments with RSS.

  1. Another solution that doesn’t require the new:ing of the static methods for each subclass is making the base-class generic.

    Example:
    public class Lookup : ActiveRecordBase

    public class ProjectTypeList : Lookup

    Martin R-L

    April 26, 2010 at 5:53 pm

  2. Update: trying to use html entity codes instead of pure text for the angle brackets.

    Another solution that doesn’t require the new:ing of the static methods for each subclass is making the base-class generic.

    Example:
    public class Lookup<T> : ActiveRecordBase<T>

    public class ProjectTypeList : Lookup<ProjectTypeList>

    Martin R-L

    April 26, 2010 at 5:55 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: