Hi,
I've subscribed to the Dev List for a while and have been encouraged
to submit a new feature for Castle.ActiveRecord. nHibermnate has the
ability (via mapping files) to specify if a field is generated or not
(none|insert|always).
I've added support to my local ActiveRecord source and have the
following test to ensure it's implemented ok. As I'm not a committer I
hoped I could post it up and hope that someone could commit it for me
(with perhaps a mention). Anyway if I'm going about this all wrong
please let me know, but I think it's a handy feature to add to
ActiveRecord.....
I've provided the test and detailed some of the code I used to get it
working... but I'm sure there could be a better way,
Cheers
ian pender
test :
namespace Castle.ActiveRecord.Framework.Internal.Tests.Model
{
using Castle.ActiveRecord;
[ActiveRecord]
public class ClassWithGeneratedProperties : ActiveRecordBase
{
[PrimaryKey]
public int Id { get; set; }
[Property(GeneratedType = "insert")]
public int ColumnGeneratedInsert { get; set; }
[Property(GeneratedType = "none")]
public int ColumnGeneratedNone { get; set; }
[Property(GeneratedType = "always")]
public int ColumnGeneratedAlways { get; set; }
[Property]
public int ColumnNoGenerated { get; set; }
}
}
namespace Castle.ActiveRecord.Framework.Internal.Tests :
XmlGenerationTestCase
[Test]
public void SimpleCaseGeneratedType()
{
ActiveRecordModelBuilder builder = new
ActiveRecordModelBuilder();
ActiveRecordModel model =
builder.Create(typeof(ClassWithGeneratedProperties));
Assert.IsNotNull(model);
SemanticVerifierVisitor semanticVisitor = new
SemanticVerifierVisitor(builder.Models);
semanticVisitor.VisitNode(model);
XmlGenerationVisitor xmlVisitor = new
XmlGenerationVisitor();
xmlVisitor.CreateXml(model);
String xml = xmlVisitor.Xml;
const string expected =
"<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n" +
"<hibernate-mapping auto-import=\"true\" default-lazy=
\"false\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=
\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"urn:nhibernate-
mapping-2.2\">\r\n" +
" <class name=
\"Castle.ActiveRecord.Framework.Internal.Tests.Model.ClassWithGeneratedProperties,
Castle.ActiveRecord.Framework.Internal.Tests\" table=
\"ClassWithGeneratedProperties\">\r\n" +
" <id name=\"Id\" access=\"property\" column=\"Id\"
type=\"Int32\" unsaved-value=\"0\">\r\n" +
" <generator class=\"native\">\r\n" +
" </generator>\r\n" +
" </id>\r\n" +
" <property name=\"ColumnGeneratedInsert\" access=
\"property\" type=\"Int32\" generated=\"insert\">\r\n" +
" <column name=\"ColumnGeneratedInsert\"/>\r\n" +
" </property>\r\n" +
" <property name=\"ColumnGeneratedNone\" access=
\"property\" type=\"Int32\" generated=\"none\">\r\n" +
" <column name=\"ColumnGeneratedNone\"/>\r\n" +
" </property>\r\n" +
" <property name=\"ColumnGeneratedAlways\" access=
\"property\" type=\"Int32\" generated=\"always\">\r\n" +
" <column name=\"ColumnGeneratedAlways\"/>\r\n" +
" </property>\r\n" +
" <property name=\"ColumnNoGenerated\" access=
\"property\" type=\"Int32\">\r\n" +
" <column name=\"ColumnNoGenerated\"/>\r\n" +
" </property>\r\n" +
" </class>\r\n" +
"</hibernate-mapping>\r\n";
Assert.AreEqual(expected, xml);
}
I got this test working by adding to
namespace Castle.ActiveRecord.PropertyAttribute
/// <summary>
/// Set to never|insert|always to specify how the property is
generated. If blank not included in mapping
/// </summary>
public string GeneratedType
{
get { return generatedType; }
set { generatedType = value; }
}
and amending
namespace Castle.ActiveRecord.Framework.Internal
public class XmlGenerationVisitor : AbstractDepthFirstVisitor
private void WriteProperty(String name, Type propType, String
accessString, String columnType,
bool insert, bool update, String
formula,
String column, int length, bool
notNull,
bool unique,
String uniqueKey, String sqlType,
String
index, String check, String @default)
{
WriteProperty(name, propType, accessString, columnType,
insert,
update, formula, column,
length, notNull, unique, uniqueKey, sqlType, index,
check, @default, null);
}
private void WriteProperty(String name, Type propType,
String accessString, String columnType,
bool insert, bool update, String
formula,
String column, int length, bool
notNull,
bool unique,
String uniqueKey, String sqlType,
String
index, String check, String @default,
string generatedFlag)
{
BeginWriteProperty(accessString, columnType, formula,
insert, name, propertyType, update, generatedFlag);
Ident();
WriteColumn(check, column, index, length, notNull,
sqlType, unique, uniqueKey, @default);
Dedent();
EndWriteProperty();
}
and finally
private void BeginWriteProperty(string accessString,
string columnType,
string formula,
bool insert,
string name,
Type propertyType,
bool update,
string generatedValue)
{
AppendF("<property{0}{1}{2}{3}{4}{5}{6}>",
MakeAtt("name", name),
WriteIfNonNull("access", accessString),
MakeTypeAtt(propertyType, columnType),
WriteIfFalse("insert", insert),
WriteIfFalse("update", update),
WriteIfNonNull("formula", formula),
WriteIfNonNull("generated", generatedValue));
}
--
You received this message because you are subscribed to the Google Groups
"Castle Project Development List" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/castle-project-devel?hl=en.