/source/FireStarter.CodeGeneration/CodeDomBoClass.cs

http://FireStarterModeller.codeplex.com · C# · 196 lines · 171 code · 17 blank · 8 comment · 16 complexity · 06ec8866c3c7fd8fc7ab60aa5fd11684 MD5 · raw file

  1. using System;
  2. using System.CodeDom;
  3. using System.CodeDom.Compiler;
  4. using FireStarter.Base;
  5. using FireStarterModeller.BO;
  6. using Microsoft.CSharp;
  7. namespace FireStarter.CodeGeneration
  8. {
  9. internal class CodeDomBoClass : CodeDomBoClassStub
  10. {
  11. public CodeDomBoClass(CodeDomProvider codeDomProvider, IModelClass dmClass)
  12. : base(codeDomProvider, dmClass)
  13. {
  14. }
  15. protected override CodeCompileUnit GetCodeCompileUnit()
  16. {
  17. return GetClassDefPartialCompileUnit();
  18. }
  19. private CodeCompileUnit GetClassDefPartialCompileUnit()
  20. {
  21. CodeNamespace classNamespace;
  22. CodeTypeDeclaration classType;
  23. CodeCompileUnit classCode = GetClassStubPartialCompileUnit(
  24. " NB Custom code should be placed in the provided stub class." +
  25. Environment.NewLine + " Please do not modify this class directly!",
  26. true, out classNamespace, out classType);
  27. //Properties
  28. CodeTypeMember firstMember = null;
  29. CodeTypeMember member = null;
  30. foreach (IModelProperty property in _modelClass.Properties)
  31. {
  32. if (property.GenerateCode)
  33. {
  34. member = GetPropertyDefMemberProperty(classNamespace, property);
  35. if (firstMember == null)
  36. {
  37. firstMember = member;
  38. }
  39. classType.Members.Add(member);
  40. }
  41. }
  42. CreateRegion(firstMember, member, "Properties");
  43. //Relationships
  44. firstMember = null;
  45. member = null;
  46. foreach (IModelRelationship relationship in _modelClass.Relationships)
  47. {
  48. if (relationship.GenerateCode)
  49. {
  50. member = GetRelationshipDefMemberProperty(classNamespace, relationship);
  51. if (firstMember == null)
  52. {
  53. firstMember = member;
  54. }
  55. classType.Members.Add(member);
  56. }
  57. }
  58. CreateRegion(firstMember, member, "Relationships");
  59. ////Find by Properties
  60. //AddFindByClass(classNamespace, classType, defClass);
  61. return classCode;
  62. }
  63. private CodeMemberProperty GetPropertyDefMemberProperty(CodeNamespace classNamespace, IModelProperty property)
  64. {
  65. CodeMemberProperty memberProperty = new CodeMemberProperty();
  66. memberProperty.Name = property.PropertyName;
  67. IModelPropertyType modelPropertyType = property.PropertyType;
  68. CodeTypeReference reference;
  69. if (modelPropertyType != null)
  70. {
  71. string typeName = modelPropertyType.PropertyTypeName;
  72. string assemblyName = modelPropertyType.AssemblyName ?? "";
  73. EnsureAssemblyImported(classNamespace, assemblyName);
  74. reference = new CodeTypeReference(typeName);
  75. if (modelPropertyType.IsValueType)
  76. {
  77. if (_codeDomProvider is CSharpCodeProvider)
  78. {
  79. reference = new CodeTypeReference(typeName + "?");
  80. }
  81. else
  82. {
  83. reference = new CodeTypeReference("Nullable", reference);
  84. }
  85. }
  86. }
  87. else
  88. {
  89. reference = new CodeTypeReference(typeof(object));
  90. }
  91. memberProperty.Type = reference;
  92. memberProperty.HasGet = true;
  93. memberProperty.Attributes = MemberAttributes.Public;
  94. CodeMethodReturnStatement returnStatement = new CodeMethodReturnStatement(
  95. new CodeCastExpression(reference, new CodeMethodInvokeExpression(
  96. new CodeBaseReferenceExpression(), "GetPropertyValue", new CodePrimitiveExpression(property.PropertyName))));
  97. memberProperty.GetStatements.Add(returnStatement);
  98. if (property.HasSetter)
  99. {
  100. memberProperty.HasSet = true;
  101. memberProperty.SetStatements.Add(new CodeMethodInvokeExpression(
  102. new CodeBaseReferenceExpression(), "SetPropertyValue", new CodePrimitiveExpression(property.PropertyName), new CodePropertySetValueReferenceExpression()));
  103. }
  104. else
  105. {
  106. memberProperty.HasSet = false;
  107. }
  108. return memberProperty;
  109. }
  110. private static CodeMemberProperty GetRelationshipDefMemberProperty(CodeNamespace classNamespace, IModelRelationship relationship)
  111. {
  112. CodeMemberProperty memberProperty = new CodeMemberProperty();
  113. memberProperty.Name = relationship.RelationshipName;
  114. //DefClass relatedClass = (DefClass)relationship.ClassName.MyRelatedClass.CurrentClass;
  115. string typeName = relationship.RelatedClass;
  116. string assemblyName = relationship.RelatedAssembly;
  117. if (String.IsNullOrEmpty(typeName))
  118. {
  119. typeName = "BusinessObject";
  120. assemblyName = "Habanero.BO";
  121. }
  122. if (!string.IsNullOrEmpty(assemblyName))
  123. {
  124. EnsureAssemblyImported(classNamespace, assemblyName);
  125. }
  126. CodeTypeReference reference = new CodeTypeReference(typeName);
  127. CodeTypeReference returnType;
  128. if (relationship.IsMultiple)
  129. {
  130. returnType = new CodeTypeReference("BusinessObjectCollection", reference);
  131. }
  132. else
  133. {
  134. returnType = reference;
  135. }
  136. memberProperty.Type = returnType;
  137. memberProperty.HasGet = true;
  138. memberProperty.Attributes = MemberAttributes.Public;
  139. CodeMethodReturnStatement returnStatement;
  140. if (relationship.IsMultiple)
  141. {
  142. returnStatement = new CodeMethodReturnStatement(
  143. new CodeMethodInvokeExpression(
  144. new CodeMethodReferenceExpression(
  145. new CodeVariableReferenceExpression("Relationships"),
  146. "GetRelatedCollection", reference),
  147. new CodePrimitiveExpression(relationship.RelationshipName)));
  148. //Generated Code sample: return Relationships.GetRelatedCollection<Contact>("Contacts");
  149. }
  150. else
  151. {
  152. returnStatement = new CodeMethodReturnStatement(
  153. new CodeMethodInvokeExpression(
  154. new CodeMethodReferenceExpression(
  155. new CodeVariableReferenceExpression("Relationships"),
  156. "GetRelatedObject", reference),
  157. new CodePrimitiveExpression(relationship.RelationshipName)));
  158. //Generated Code sample: return Relationships.GetRelatedObject<Contact>("Contact");
  159. }
  160. memberProperty.GetStatements.Add(returnStatement);
  161. if (relationship.IsMultiple)
  162. {
  163. memberProperty.HasSet = false;
  164. }
  165. else
  166. {
  167. memberProperty.HasSet = true;
  168. CodeMethodInvokeExpression setStatement = new CodeMethodInvokeExpression(
  169. new CodeMethodReferenceExpression(
  170. new CodeVariableReferenceExpression("Relationships"), "SetRelatedObject"),
  171. new CodePrimitiveExpression(relationship.RelationshipName),
  172. new CodePropertySetValueReferenceExpression());
  173. memberProperty.SetStatements.Add(setStatement);
  174. //Generated Code sample: Relationships.SetRelatedObject("Contact", value);
  175. }
  176. return memberProperty;
  177. }
  178. }
  179. }