improve OOP modeling code generation

This commit is contained in:
Michael Becker 2025-09-26 00:11:59 -04:00
parent 70f5c03435
commit 40dd9b4b76
3 changed files with 121 additions and 7 deletions

View File

@ -121,7 +121,46 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
}
else if (item is Method m)
{
sb.Append(GenerateCode(m.AccessModifier));
if (m.AccessModifier != AccessModifier.None)
{
sb.Append(' ');
}
if (m.IsOverride)
{
sb.Append("override ");
}
if (m.ReturnType != null)
{
sb.Append(m.ReturnType);
sb.Append(' ');
}
else
{
sb.Append("void ");
}
sb.Append(m.Name);
sb.Append('(');
for (int i = 0; i < m.Parameters.Count; i++)
{
MethodParameter parm = m.Parameters[i];
sb.Append(BuildObjectReference(parm.DataType.ObjectNames));
sb.Append(' ');
sb.Append(parm.Name);
if (i < m.Parameters.Count - 1)
{
sb.Append(',');
}
}
sb.Append(')');
sb.Append('{');
foreach (IMethodMember member in m.Items)
{
sb.Append(GenerateCode(member));
sb.Append(';');
}
sb.Append('}');
}
else if (item is CreateInstance ci)
{
@ -151,6 +190,19 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
sb.Append(BuildParameterList(mc.Parameters));
sb.Append(')');
}
else if (item is PropertyAssignment pa)
{
sb.Append(BuildObjectReference(pa.ObjectReference.ObjectNames));
sb.Append(" = ");
if (pa.Value != null)
{
sb.Append(pa.Value);
}
else
{
sb.Append("null");
}
}
return sb.ToString();
}
@ -239,6 +291,57 @@ public class CSharpCodeGenerator : DotNetCodeGenerator
pp.SetMethod = new Method();
}
}
else if (pi.PropertyType == typeof(int))
{
pp.GetMethod = new Method(new IMethodMember[]
{
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
{
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
}) { GenericParameters = [ new ObjectReference(["int"]) ]})
});
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
if (pi.GetSetMethod() != null)
{
pp.SetMethod = new Method();
}
}
else if (pi.PropertyType == typeof(decimal))
{
pp.GetMethod = new Method(new IMethodMember[]
{
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
{
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
}) { GenericParameters = [ new ObjectReference(["decimal"]) ]})
});
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
if (pi.GetSetMethod() != null)
{
pp.SetMethod = new Method();
}
}
else if (pi.PropertyType == typeof(DateTime))
{
pp.GetMethod = new Method(new IMethodMember[]
{
new Return(new MethodCall(new string[] { "this", "Oms" }, "GetAttributeValue", new object[]
{
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new ObjectReference(new string[] { "this", "GlobalIdentifier" })}),
new MethodCall(new string[] { "this", "Oms" }, "GetInstance", new object[] { new CreateInstance(new string[] { "System", "Guid" }, new object[] { "{9153A637-992E-4712-ADF2-B03F0D9EDEA6}" })})
}) { GenericParameters = [ new ObjectReference(["System.DateTime"]) ]})
});
// GetAttributeValue<T>(InstanceKey source, InstanceHandle attribute, T defaultValue = default(T), DateTime? effectiveDate = null)
if (pi.GetSetMethod() != null)
{
pp.SetMethod = new Method();
}
}
else
{
// generate dynamic getters and setters for GetRelatedInstance and AssignRelationship

View File

@ -20,8 +20,12 @@ namespace Mocha.Modeling.CodeGeneration;
public class Method : IClassMember
{
public string? Name { get; set; } = null;
public AccessModifier AccessModifier { get; set; } = AccessModifier.None;
public ObjectReference? ReturnType { get; set; } = null;
public List<IMethodMember> Items { get; } = new List<IMethodMember>();
public List<MethodParameter> Parameters { get; } = new List<MethodParameter>();
public bool IsOverride { get; set; } = false;
public Method(IEnumerable<IMethodMember>? items = null) : this(null, items)
{

View File

@ -56,7 +56,6 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
if ((o?.GetType().IsAssignableTo(typeof(TOmsClass))).GetValueOrDefault())
{
TOmsClass db = (TOmsClass)o;
return db;
}
throw new TypeInitializationException(typeof(TOmsClass).Name, null);
@ -82,7 +81,6 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
if ((o?.GetType().IsAssignableTo(typeof(TOmsClass))).GetValueOrDefault())
{
TOmsClass db = (TOmsClass)o;
return db;
}
throw new TypeInitializationException(typeof(TOmsClass).Name, null);
@ -105,8 +103,17 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
Namespace ns = new Namespace(new string[] { "Mocha", "Modeling", "Models", t.Name });
Mocha.Modeling.CodeGeneration.Class cl = GenerateClass(t, globalIdentifier);
ns.Items.Add(cl);
if (t.IsAssignableTo(typeof(IOmsDatabase)))
{
Mocha.Modeling.CodeGeneration.Class cl = GenerateDatabase(t);
ns.Items.Add(cl);
}
else
{
Mocha.Modeling.CodeGeneration.Class cl = GenerateClass(t, globalIdentifier);
ns.Items.Add(cl);
}
return GenerateCode(ns);
}
@ -132,7 +139,7 @@ public class OmsObjectFactory<TOmsClass> : CSharpCodeGenerator where TOmsClass :
cl.Items.Add(new CodeGeneration.Method("Initialize", new MethodParameter[] { new MethodParameter("oms", new ObjectReference(["Mocha", "Core", "Oms"]))}, new IMethodMember[]
{
new PropertyAssignment(new ObjectReference(["this", "Oms"]), "oms")
}));
}) { AccessModifier = CodeGeneration.AccessModifier.Public});
return cl;
}