Inheritance with Transfer
February 03, 2009
In my current project I've got various types of User who can login to the system. For example a Developer and a Designer. With OO this is a "IS-A" relationship, so we use inheritence, because a Developer "IS-A" User and a Designer "IS-A" User.
The problem is how to map these to a relational database using Transfer ORM (why didn't I start using it sooner!). Bob Silverberg and Paul Marcotte have come up with a solution to this, but I felt that my needs were different so went down a different root. Now before I go any further I should point out that I'm quite new to Transfer and not in the same league as Bob and Paul, but, disclaimer over, this is what I came up with.
Here is a snippet from my Transfer config (using Bob's example of a Analyst, Developer and Designer):
<package name="employee">
<object name="User" table="USERS" decorator="model.employee.User">
<id name="UserID" type="GUID" />
<property name="Firstname" type="string" />
<property name="Lastname" type="string" />
<property name="Username" type="string" />
<property name="Password" type="string" />
<property name="Active" type="boolean" />
<manytoone name="Role" lazy="false">
<link to="employee.Role" column="role_id" />
</manytoone>
</object>
<object name="Analyst">
<id name="AnalystID" type="GUID" />
<property name="levelOfAttentionToDetail" type="string" />
<property name="totalPagesOfRequirementsProduced" type="numeric" />
<manytoone name="User">
<link column="user_id" to="employee.User" />
</manytoone>
</object>
<object name="Developer">
<id name="DeveloperID" type="GUID" />
<property name="FavouriteLanguage" type="string" />
<property name="enjoysDeathMetal" type="boolean" />
<manytoone name="User">
<link column="user_id" to="employee.User" />
</manytoone>
</object>
<object name="Designer">
<id name="DesignerID" type="GUID" />
<property name="FavouriteColour" type="string" />
<property name="toleratesDevelopers" type="boolean" />
<manytoone name="User">
<link column="user_id" to="employee.User" />
</manytoone>
</object>
<object name="Role">
<id name="RoleID" type="numeric" />
<property name="Name" type="string" />
</object>
</package>
Although I've set up a manytoone relationship between the Developer and the User, in reality it is a onetoone relationship, but this isn't supported by Transfer. My Role database table has the following values for the Name column: Designer, Developer & Analyst.
Transfer let's me get the User properties for a Developer, by simply calling DeveloperObject.getUser().getPropertyName(). However, I still need to be able to access the Developer properties from a User object. To do this, in my Transfer User decorator I've got a method that dynamically calls the correct SubType for the User.
User.cfc (Transfer Decorator)
<cfcomponent name="user"
displayname="model.employee.User"
hint="I am the User transfer decorator"
output="false">
<cffunction name="getSubType"
access="public"
output="false"
returntype="any"
hint="I return the usersubtype object">
<cfif hasRole()>
<cfreturn getTransfer().get( ListFirst( getTransferObject().getClassName(), "." ) & "." & getTransferObject().getRole().getName(), getUserID() ) />
<cfelse>
<cfthrow message="User does not have a SubType" detail="This is usually because the user object is not persisted" />
</cfif>
</cffunction>
</cfcomponent>
I'm pretty new to Transfer and I haven't tested this approach in the wild, but in my tests so far it seems to work well. I'd like to thank Paul Marcotte for letting me bounce the concepts off him and Bob Silverberg for helping me track down a real n00b error I had with Transfer.
I'd really like to hear if anyone has any thoughts or different approaches to this problem, thanks!
- Posted in:
- ColdFusion
- OOP
- Transfer
No comments
Leave a comment
If you found this post useful, interesting or just plain wrong, let me know - I like feedback :)




