Showing posts with label ActiveRecord. Show all posts
Showing posts with label ActiveRecord. Show all posts

Thursday, January 17, 2008

Let do Projection

This would be a continue part from my previous post, what I tried to do is find out how to use ActiveRecord "NHiberate to be clear" in my reporting scenario (not a CRUD) after a quick search I come up with a few blog posts which give me some light:
It will take another post for me to wrap up how I come up with .... stay tune.

Edit: I shouldn't have missed, exploring test case
Castle.ActiveRecord.Tests\ActiveRecordGenericsTestCase.cs

Friday, November 02, 2007

Once upon a time there was a ^GENERIC^

Man ! how many times did you hear me say "I'm love the Generic so MUCH". Not only it elegant but it really got it gold purpose of code reduction. Let look back at my previous solution on ActiveRecord Type Hierarchy and see how generic could help me:
[ActiveRecord("List_Lookups",
DiscriminatorColumn = "Type",
DiscriminatorType = "String",
DiscriminatorValue = "NA")]
public class Lookup: ActiveRecordBase where T: class
{
.... some code ...
}
and now for derived class DeleteAll(), FindAll() etc are just ready to be use isn't that cool?
[ActiveRecord(DiscriminatorValue = "PTL")]
public class ProjectTypeList: Lookup
{
.. zero code ;)
}
Now I am ready to focus on another matter ;)

By the way x1r0k3wl's comment come to rescue.

Thursday, November 01, 2007

Table Lookup & ActiveRecord Type Hierarchy

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.