Showing posts with label Castle. Show all posts
Showing posts with label Castle. Show all posts

Monday, December 31, 2007

Castle.MonoRail.Framework.IFilter - Part I

Today I would like to share some technique in using Castle.MonoRail.Framework.IFilter . I am a bit sad because it takes too much time for me to realize how useful, until I read between the line over and over again
Filters are executed before and|or after your actions. It is useful for security, dynamic content and to keep away repetitive code.
Keep away repetitive code is what I am tracing, let the hear the story shall we?

This happen with one of the real world project that I am working on Data Bridge like any other web application it need authentication and passing some user preference along each action (inherit from SecureController) to the view such as List, Add, Edit & Delete action & view.

This can be done using the following technique:

Create Filters\AuthenticationFilter.cs
public class AuthenticationFilter: IFilter
{
bool IFilter.Perform(ExecuteEnum exec,
IRailsEngineContext context,
IController controller){
if(!UserNotLogin())
{
Redirect("Login", "Index");
}
else
{
controller.PropertyBag["maxRow"] = GetUserMaxRowPreference();
... set more preferences as needed
}
}
}
Create SecureController
[Layout("default"), Rescue("generalerror")]
[Filter(ExecuteEnum.BeforeAction, typeof(AuthenticationFilter))]
public abstract class SecureController : ARSmartDispatcherController
{
}
SecureController is marked as abstract, prevent from direct use.

Using SecureController
public abstract class ProductController: SecureController
{
public void List()
{
}
}
Now view part: View\Product\list.vm
When render table I want to display miximum $maxRow rows.
See there is no code need to set preference in each action method, nice.

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.

Wednesday, August 15, 2007

Life without intellisence

Without an intellisence feature my programming life become doom :( I just feel I need to get additional eyes to read between the line :))

Can you tell the different between these lines of code?

${Url.Link('Main page', {@controller='Home', @action='Index'})}
${Url.Link('Main page', {@controller:'Home', @action:'Index'})}

and these two:

<% component captureFor, {@Id: "head"}: %>
<% component CaptureFor, {@Id: "head"}: %>

At the end of the day it just make me laugh @ myself :))

Now that should be the first trick to get into the castle.
The next step is to keep searching for the Excalibor

Come with me @
http://www.castleproject.org ; http://using.castleproject.org

Monday, August 13, 2007

Excalibor Quest inside the Castle


Do you want to join my quest? here is the Castle and the map ...

Man I love this project, I hope you will love it too.
If you find that not enough to buy you in check my ideal bogger's Ayande.