Tuesday, September 25, 2007
To DDD or not to DDD
Would there be any complexity left to deal with in your software? If the answer is no, you're lucky (in a way). If the answer to this highly hypothetical question is still yes, then domain driven design (DDD) might be the thing for you.
Subclasses with ActiveHibernate
class Payment
include Hibernate
hattr_accessor :amount,:double
...
end
class CreditCardPayment < Payment
join_with_table do
hattr_accessor :creditCardType,:string
...
end
end
class ChequePayment < Payment
hattr_accessor :chequeNumber,:string
..
end
generates the mapping
<class name="Payment" table="PAYMENT">
<id name="id" type="long" column="PAYMENT_ID">
<generator class="native"/>
</id>
<discriminator column="PAYMENT_TYPE" type="string"/>
<property name="amount" type="double"/>
...
<subclass name="CreditCardPayment" >
<join table="CREDIT_CARD_PAYMENT">
<key column="PAYMENT_ID"/>
<property name="creditCardType" type="string" />
...
</join>
</subclass>
<subclass name="ChequePayment" >
<property name="chequeNumber" type="string" />
...
</subclass>
</class>
The <join/> construct in the CreditCardPayment mapping allows to combine the table-per-class-hierarchy and the table-per-subclass mapping strategies in one inheritance hierarchy. In the Ruby mapping DSL it can be nicely expressed with a class method (table name and key are optional arguments) and a block. The fact that a Ruby class definition is just ordinary Ruby code (with self referring to a Class object) opens a whole new world to the simple Java developer that I am.
In Subversion soon.
Tuesday, September 11, 2007
Deep Cloning an Object Graph in .NET
public abstract class Entity
{
private long _id = -1;
public virtual long Id
{
get { return _id; }
}
public virtual T Clone()
{
MemoryStream memoryStream = new MemoryStream();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(memoryStream, this);
memoryStream.Position = 0;
return (T)binaryFormatter.Deserialize(memoryStream);
}
}
It is used like so:
public class Order : Entity<Order>
{
//...
}
The Order class derives from a template class that uses the Order class itself as parameter. I had never seen this before, and, for example, it gives all your entities a Clone() method that returns the proper type.
What's even more interesting is the way the Clone() method has been implemented by just serializing to a memory stream, immediately followed by a deserialization. It gives you a deep clone of your entity, together with all child entities it references. This technique is robust, simple, clean and generic (requiring no extra development effort).
I can imagine some people will object to this because of performance, but that only makes me think of all the crimes that are committed daily in the name of performance and (premature) optimization.
Monday, August 27, 2007
ActiveHibernate follow-up
Wednesday, August 15, 2007
Towards a mapping DSL for AH
class Project
include Hibernate
with_table_name "PROJECTS" #optional
primary_key_accessor :id,:long,:PROJECT_ID
hattr_accessor :date,:timestamp,:START_DATE
#column name is optional
hattr_accessor :name,:string
hattr_accessor :complexity,:double
hattr_accessor :size,:long
hattr_accessor :on_time,:boolean
end
By including the Hibernate module (as opposed to inheriting from a framework base class), the Hibernate mapping is generated and configured automatically. Utility class methods (like "save" and"find") and accessor methods for the mapped properties are also added.
It's all in the file dsl_test.rb (see the link in the top right corner for the code repository). It is far from complete, but thanks to Ola I can start filling the gaps (like inheritance, relations, components, ...). Let me know what you think!
Monday, August 6, 2007
First ActiveHibernate code on Google Code
All feedback is more than welcome, as I'm new to (J)Ruby. I'm also very interested in ideas for a Ruby-like interface to Hibernate. The first thing that should be done is replacing the hbm.xml mapping files by a kind of annotations or a DSL (inspired by Rails or GORM, the OR/M in Grails).
Friday, July 27, 2007
ActiveHibernate: time to package
Good news. Hibernate collection mapping is up and running. Well, one example that is: Hibernate's bag type now works with Ruby Array's, lazy loading included. To get started I've done a shameless copy/paste of the Hibernate code for the bag collection type. That went fairly easy because the Java class RubyArray implements the java.util.List interface. So now the mapping files look like
<hibernate-mapping>
<class table="BLOGS" lazy="false" entity-name="Blog">
<tuplizer entity-mode="dynamic-map"
class="activehibernate.RubyTuplizer"/>
<id name="id" column="BLOG_ID" type="long">
<generator class="identity"/>
</id>
<property name="title" type="string" />
<bag name="items"
collection-type="activehibernate.RubyArrayType">
<key column="blog_id" />
<one-to-many entity-name="Item"/>
</bag>
</class>
</hibernate-mapping>
where Blog and Item are genuine Ruby classes. The nice part is that I haven't touched any existing Hibernate or JRuby code.
The plan is to package the things I already have (still need to write more tests to show what is working) and put it on SourceForge or Google Code to get some feedback from JRuby and/or Hibernate people. (I can very well imagine I've made things to complex where simpler solutions exist out-of-the-box.) After that it's time to start looking at ways to get rid of the XML mapping files by writing a friendly Ruby interface around the Hibernate details.
I hope that after some time this project will be stable/useful enough to be part of JRuby Extras at RubyForge.