private CodeTypeDeclaration CreateUpdateClass(
IMap map,
IDictionary<Type, IMap> maps,
CodeGeneratorConfig codeGeneratorConfig) {
// public class Post_Update
var updateClass =
new CodeTypeDeclaration(map.Type.Name + codeGeneratorConfig.UpdateClassSuffix) {
IsClass = true,
TypeAttributes = TypeAttributes.Public
};
updateClass.BaseTypes.Add(map.Type);
updateClass.BaseTypes.Add(typeof(IUpdateClass));
// public IList<string> UpdatedProperties { get; set; }
this.GenerateGetSetProperty(
updateClass,
"UpdatedProperties",
typeof(IList<>).MakeGenericType(typeof(string)),
FinalPublic);
// public bool IsConstructed { get; set; }
this.GenerateGetSetProperty(updateClass, "IsConstructed", typeof(bool), FinalPublic);
// public Post_Update() {
// this.IsConstructed = true;
// this.backingUpdatedProperties = new List<string>();
// }
var constructor = new CodeConstructor { Attributes = MemberAttributes.Public };
constructor.Statements.Add(new CodeAssignStatement(CodeHelpers.ThisProperty("IsConstructed"), new CodePrimitiveExpression(true)));
constructor.Statements.Add(new CodeAssignStatement(CodeHelpers.ThisProperty("UpdatedProperties"), new CodeObjectCreateExpression(typeof(List<>).MakeGenericType(typeof(string)))));
updateClass.Members.Add(constructor);
// public override IList<Comment> Comments {
// get {
// return this.backingComments;
// }
// set {
// if (this.IsConstructed) {
// this.UpdatedProperties.Add("Comments");
// }
// this.backingComments = value;
// }
// }
foreach (var column in map.Columns.Where(c => !c.Value.IsIgnored)) {
var prop = this.GenerateGetSetProperty(
updateClass,
column.Key,
column.Value.Type,
MemberAttributes.Public | MemberAttributes.Override,
true);
var ifIsConstructed = CodeHelpers.ThisPropertyIsTrue("IsConstructed");
var addColumnToUpdatedProperties =
new CodeExpressionStatement(
new CodeMethodInvokeExpression(
CodeHelpers.ThisProperty("UpdatedProperties"),
"Add",
new CodePrimitiveExpression(column.Key)));
prop.SetStatements.Insert(0, new CodeConditionStatement(ifIsConstructed, addColumnToUpdatedProperties));
}
return updateClass;
}