Permalink
Browse files

finished replacing magic strings with static reflection

started adding property-by-property tests for hbm to C# converters (Types)
  • Loading branch information...
1 parent 8dc71bf commit abf0fe94e86160147484619439f7ca452507061b @handcraftsman handcraftsman committed Aug 20, 2009
Showing with 998 additions and 31 deletions.
  1. +0 −3 Converter/ClassMapBody.cs
  2. +5 −0 Converter/CodeFileBuilder.cs
  3. +1 −0 Converter/Converter.csproj
  4. +4 −0 Converter/Extensions/NHibernate/HbmPropertyExtensions.cs
  5. +5 −0 Converter/Extensions/StringExtensions.cs
  6. +24 −2 Converter/MappingConverter.cs
  7. +35 −0 Converter/Methods/CustomType.cs
  8. +1 −1 Converter/Methods/Length.cs
  9. +16 −1 Converter/Methods/Nullability.cs
  10. +10 −1 Converter/Methods/UnsavedValue.cs
  11. +21 −3 Converter/Types/Bag.cs
  12. +22 −8 Converter/Types/Component.cs
  13. +12 −1 Converter/Types/Id.cs
  14. +14 −3 Converter/Types/Map.cs
  15. +13 −1 Converter/Types/References.cs
  16. +22 −4 Converter/Types/Set.cs
  17. +21 −0 ConverterTests/ClassFileUtilities.cs
  18. +30 −0 ConverterTests/ClassFileUtilitiesTests.cs
  19. +11 −0 ConverterTests/ConverterTests.csproj
  20. +8 −0 ConverterTests/Extensions/NHibernate/HbmPropertyExtensionsTests.cs
  21. +16 −0 ConverterTests/Extensions/StringExtensionTests.cs
  22. +81 −0 ConverterTests/Methods/CustomTypeTests.cs
  23. +50 −0 ConverterTests/Methods/IndexTests.cs
  24. +105 −0 ConverterTests/Methods/NullabilityTests.cs
  25. +13 −3 ConverterTests/Methods/UnsavedValueTests.cs
  26. +30 −0 ConverterTests/Types/BagTests.cs
  27. +30 −0 ConverterTests/Types/ComponentTests.cs
  28. +105 −0 ConverterTests/Types/IdTests.cs
  29. +138 −0 ConverterTests/Types/MapTests.cs
  30. +43 −0 ConverterTests/Types/MappingConverterTests.cs
  31. +82 −0 ConverterTests/Types/ReferencesTests.cs
  32. +30 −0 ConverterTests/Types/SetTests.cs
  33. BIN WinForm/bin/Debug/NHibernateHbmToFluent.Converter.dll
  34. BIN WinForm/bin/Debug/NHibernateHbmToFluent.Converter.pdb
  35. BIN WinForm/bin/Debug/NHibernateHbmToFluent.exe
  36. BIN WinForm/bin/Debug/NHibernateHbmToFluent.pdb
@@ -16,13 +16,10 @@ public void Add(string prefix, MappedPropertyInfo info)
{
var methodBuilders = new List<ICommonMapMethod>
{
- new Column(_builder),
- new Length(_builder),
new Nullability(_builder),
new Unique(_builder),
new Index(_builder)
};
- GeneratedBy generatedBy = new GeneratedBy(_builder);
info.Type.StartMethod(prefix, _builder, info);
{
methodBuilders.ForEach(x => x.Add(info));
@@ -126,6 +126,11 @@ public void Indent()
_indentLevel++;
}
+ public void Indent(int count)
+ {
+ _indentLevel += count;
+ }
+
public void Unindent()
{
_indentLevel--;
@@ -72,6 +72,7 @@
<Compile Include="MappedPropertyInfo.cs" />
<Compile Include="MappingConverter.cs" />
<Compile Include="FakeMap.cs" />
+ <Compile Include="Methods\CustomType.cs" />
<Compile Include="Methods\Index.cs" />
<Compile Include="Methods\UnsavedValue.cs" />
<Compile Include="ReflectionUtility.cs" />
@@ -55,6 +55,10 @@ public static string GetSqlType(this HbmProperty item)
return item.column;
}
HbmColumn column = item.Column();
+ if (column == null)
+ {
+ return null;
+ }
string sqlType = column.sqltype;
return sqlType;
}
@@ -4,6 +4,11 @@ namespace NHibernateHbmToFluent.Converter.Extensions
{
public static class StringExtensions
{
+ public static string[] SplitOnFormattingWhitespace(this string input)
+ {
+ return input.Split(new[] {'\r', '\n', '\t'}, StringSplitOptions.RemoveEmptyEntries);
+ }
+
public static string GetTypeName(this string fullTypeDescriptor)
{
int commaLoc = fullTypeDescriptor.IndexOf(',');
@@ -42,7 +42,7 @@ public void ConvertAll(string hbmDirectory, string mapDirectory, string nameSpac
_writeToConsole("done...");
}
- public string Convert(string classMapName, MappedClassInfo classInfo, string nameSpace)
+ public static string Convert(string classMapName, MappedClassInfo classInfo, string nameSpace)
{
CodeFileBuilder builder = new CodeFileBuilder();
ClassMapBody bodyBuilder = new ClassMapBody(builder);
@@ -56,7 +56,7 @@ public string Convert(string classMapName, MappedClassInfo classInfo, string nam
{
if (!String.IsNullOrEmpty(classInfo.TableName))
{
- builder.AddLine("Table(\"" + classInfo.TableName + "\");");
+ builder.AddLine(FluentNHibernateNames.Table + "(\"" + classInfo.TableName + "\");");
}
foreach (var info in classInfo.Properties)
{
@@ -70,5 +70,27 @@ public string Convert(string classMapName, MappedClassInfo classInfo, string nam
builder.EndBlock();
return builder.ToString();
}
+
+ public static class FluentNHibernateNames
+ {
+ public static string Table
+ {
+ get { return typeof (FakeMap).GetMethod("Table").Name; }
+ }
+
+ public static string ClassMap
+ {
+ get
+ {
+ string name = typeof (FakeMap).BaseType.Name;
+ int index = name.IndexOf('`');
+ if (index != -1)
+ {
+ name = name.Substring(0, index);
+ }
+ return name;
+ }
+ }
+ }
}
}
@@ -0,0 +1,35 @@
+using FluentNHibernate.Mapping;
+
+namespace NHibernateHbmToFluent.Converter.Methods
+{
+ public class CustomType
+ {
+ private readonly CodeFileBuilder _builder;
+
+ public CustomType(CodeFileBuilder builder)
+ {
+ _builder = builder;
+ }
+
+ public void Add(MappedPropertyInfo item)
+ {
+ if (item.Type != PropertyMappingType.Property)
+ {
+ return;
+ }
+
+ if (item.SqlType == "CHAR" && item.ReturnType == "YesNo")
+ {
+ _builder.AddLine(string.Format(".{0}(\"{1}\")", FluentNHibernateNames.CustomType, item.ReturnType));
+ }
+ }
+
+ public static class FluentNHibernateNames
+ {
+ public static string CustomType
+ {
+ get { return ReflectionUtility.GetMethodName((PropertyPart ip) => ip.CustomType(typeof (string))); }
+ }
+ }
+ }
+}
@@ -2,7 +2,7 @@
namespace NHibernateHbmToFluent.Converter.Methods
{
- public class Length : ICommonMapMethod
+ public class Length
{
private readonly CodeFileBuilder _builder;
@@ -1,3 +1,5 @@
+using FluentNHibernate.Mapping;
+
namespace NHibernateHbmToFluent.Converter.Methods
{
public class Nullability : ICommonMapMethod
@@ -21,9 +23,22 @@ public void Add(MappedPropertyInfo info)
{
if (!info.CanBeNull.Value)
{
- _builder.AddLine(".Not.Nullable()");
+ _builder.AddLine(string.Format(".{0}.{1}()", FluentNHibernateNames.Not, FluentNHibernateNames.Nullable));
}
}
}
+
+ public static class FluentNHibernateNames
+ {
+ public static string Not
+ {
+ get { return ReflectionUtility.GetPropertyName((PropertyPart ip) => ip.Not); }
+ }
+
+ public static string Nullable
+ {
+ get { return ReflectionUtility.GetMethodName((PropertyPart ip) => ip.Nullable()); }
+ }
+ }
}
}
@@ -1,3 +1,4 @@
+using FluentNHibernate.Mapping;
using NHibernate.Cfg.MappingSchema;
namespace NHibernateHbmToFluent.Converter.Methods
@@ -22,7 +23,15 @@ public void Add(MappedPropertyInfo info)
if (id.unsavedvalue != null)
{
var unsavedValue = id.unsavedvalue == "null" ? "String.Empty" : id.unsavedvalue;
- _builder.AddLine(".UnsavedValue(" + unsavedValue + ")");
+ _builder.AddLine(string.Format(".{0}({1})", FluentNHibernateNames.UnsavedValue, unsavedValue));
+ }
+ }
+
+ public static class FluentNHibernateNames
+ {
+ public static string UnsavedValue
+ {
+ get { return ReflectionUtility.GetMethodName((IdentityPart ip) => ip.UnsavedValue(null)); }
}
}
}
View
@@ -31,23 +31,41 @@ public void Start(string prefix, MappedPropertyInfo item)
PropertyMappingType subType = new MappedPropertyInfo(bag.Item, item.FileName).Type;
if (subType == PropertyMappingType.ManyToMany)
{
- _builder.StartMethod(prefix, "HasManyToMany<" + item.ReturnType + ">(x => x." + item.Name + ")");
+ _builder.StartMethod(prefix, string.Format("{0}<{1}>(x => x.{2})", FluentNHibernateNames.HasManyToMany, item.ReturnType, item.Name));
}
else if (subType == PropertyMappingType.OneToMany)
{
- _builder.StartMethod(prefix, "HasMany<" + item.ReturnType + ">(x => x." + item.Name + ")");
+ _builder.StartMethod(prefix, string.Format("{0}<{1}>(x => x.{2})", FluentNHibernateNames.HasMany, item.ReturnType, item.Name));
}
else
{
_builder.StartMethod(prefix, "bag?(x => x" + item.Name + ")");
}
- _builder.AddLine(".AsBag()");
+ _builder.AddLine(string.Format(".{0}()", FluentNHibernateNames.AsBag));
_keyColumn.Add(bag.inverse, item.ColumnName, subType);
_lazyLoad.Add(bag.lazySpecified, bag.lazy);
_table.Add(bag.table);
_inverse.Add(bag.inverse);
_cascade.Add(bag.cascade);
_orderBy.Add(bag.orderby);
}
+
+ public static class FluentNHibernateNames
+ {
+ public static string HasManyToMany
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.HasManyToMany<string>(x => x)); }
+ }
+
+ public static string HasMany
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.HasMany<string>(x => x)); }
+ }
+
+ public static string AsBag
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.HasMany<string>(x => x).AsBag()); }
+ }
+ }
}
}
@@ -15,11 +15,7 @@ public Component(CodeFileBuilder builder)
public void Start(string prefix, MappedPropertyInfo item)
{
CodeFileBuilder componentBuilder = new CodeFileBuilder();
- componentBuilder.Indent();
- componentBuilder.Indent();
- componentBuilder.Indent();
- componentBuilder.Indent();
- componentBuilder.Indent();
+ componentBuilder.Indent(5);
const string subPrefix = "y.";
HbmComponent component = item.HbmObject<HbmComponent>();
componentBuilder.AddLine("");
@@ -29,17 +25,35 @@ public void Start(string prefix, MappedPropertyInfo item)
{
componentBodyBuilder.Add(subPrefix, new MappedPropertyInfo(componentPart, item.FileName));
}
- _builder.StartMethod(prefix, "Component<" + item.ReturnType + ">(x => x." + item.Name + ", y=>");
+ _builder.StartMethod(prefix, string.Format("{0}<{1}>(x => x.{2}, y=>", FluentNHibernateNames.Component, item.ReturnType, item.Name));
_builder.AddLine("{");
_builder.AddLine(componentBuilder.ToString());
_builder.AddLine("})");
if (component.insert)
{
- _builder.AddLine(".Insert()");
+ _builder.AddLine(string.Format(".{0}()", FluentNHibernateNames.Insert));
}
if (component.update)
{
- _builder.AddLine(".Update()");
+ _builder.AddLine(string.Format(".{0}()", FluentNHibernateNames.Update));
+ }
+ }
+
+ public static class FluentNHibernateNames
+ {
+ public static string Component
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.Component(x => x, null)); }
+ }
+
+ public static string Insert
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.Component(x => x, null).Insert()); }
+ }
+
+ public static string Update
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.Component(x => x, null).Update()); }
}
}
}
View
@@ -8,19 +8,30 @@ public class Id : IMapStart
private readonly CodeFileBuilder _builder;
private readonly GeneratedBy _generatedBy;
private readonly UnsavedValue _unsavedValue;
+ private readonly Column _column;
public Id(CodeFileBuilder builder)
{
_builder = builder;
+ _column = new Column(_builder);
_generatedBy = new GeneratedBy(_builder);
_unsavedValue = new UnsavedValue(_builder);
}
public void Start(string prefix, MappedPropertyInfo item)
{
- _builder.StartMethod(prefix, "Id(x => x." + item.Name + ")");
+ _builder.StartMethod(prefix, string.Format("{0}(x => x.{1})", FluentNHibernateNames.Id, item.Name));
+ _column.Add(item);
_generatedBy.Add(item);
_unsavedValue.Add(item);
}
+
+ public static class FluentNHibernateNames
+ {
+ public static string Id
+ {
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.Id(null)); }
+ }
+ }
}
}
View
@@ -1,22 +1,33 @@
using NHibernateHbmToFluent.Converter.Extensions;
+using NHibernateHbmToFluent.Converter.Methods;
namespace NHibernateHbmToFluent.Converter.Types
{
public class Map : IMapStart
{
private readonly CodeFileBuilder _builder;
+ private readonly Length _length;
+ private readonly CustomType _customType;
public Map(CodeFileBuilder builder)
{
_builder = builder;
+ _length = new Length(_builder);
+ _customType = new CustomType(_builder);
}
public void Start(string prefix, MappedPropertyInfo item)
{
- _builder.StartMethod(prefix, "Map(x => x." + item.Name + ", \"" + item.ColumnName + "\")");
- if (item.SqlType == "CHAR" && item.ReturnType == "YesNo")
+ _builder.StartMethod(prefix, string.Format("{0}(x => x.{1}, \"{2}\")", FluentNHibernateNames.Map, item.Name, item.ColumnName));
+ _customType.Add(item);
+ _length.Add(item);
+ }
+
+ public static class FluentNHibernateNames
+ {
+ public static string Map
{
- _builder.AddLine(".CustomType(\"YesNo\")");
+ get { return ReflectionUtility.GetMethodName((FakeMap f) => f.Map(null)); }
}
}
}
Oops, something went wrong.

0 comments on commit abf0fe9

Please sign in to comment.