private void WriteInteropData(CodeWriter writer, Message contract) { writer.WriteLine("public partial interface IMessageFactory"); writer.WriteLine("{"); writer.Indent += 1; writer.Write("{0} Create{0} (", contract.Name); WriteParametersForFactoryMethod(contract, writer); writer.WriteLine(");"); writer.Indent -= 1; writer.WriteLine("}"); writer.WriteLine(); writer.WriteLine("public partial class MessageFactory"); writer.WriteLine("{"); writer.Indent += 1; writer.Write("public {0} Create{0} (", contract.Name); WriteParametersForFactoryMethod(contract, writer); writer.WriteLine(")"); writer.WriteLine("{"); writer.Indent += 1; writer.Write("return new {0}(", contract.Name); WriteNewObjectArguments(contract, writer); writer.WriteLine(");"); writer.Indent -= 1; writer.WriteLine("}"); writer.Indent -= 1; writer.WriteLine("}"); writer.WriteLine(); writer.WriteLine("[ComVisible(true)]"); writer.WriteLine(contract.InteropData.ComInterfaceGuidAttribute); writer.WriteLine("[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]"); writer.WriteLine("public partial interface I{0}", contract.Name); writer.WriteLine("{"); writer.Indent += 1; if (contract.Members.Count > 0) { WriteInterfaceMembers(contract, writer); } writer.Indent -= 1; writer.WriteLine("}"); writer.WriteLine(); writer.WriteLine("[ComVisible(true)]"); writer.WriteLine(contract.InteropData.ClassGuidAttribute); writer.WriteLine("[ClassInterface(ClassInterfaceType.None)]"); }
void WriteMembers(Message message, CodeWriter writer) { var idx = 1; foreach (var member in message.Members) { writer.WriteLine(MemberTemplate, idx, member.Type, GeneratorUtil.MemberCase(member.Name)); idx += 1; } }
private void WriteContext(CodeWriter writer, Context context) { foreach (var contract in context.Contracts) { if (contract.InteropData != null) WriteInteropData(writer, contract); writer.Write(ClassNameTemplate, contract.Name, context.CurrentExtern); if (contract.Modifiers.Any()) writer.Write(" : {0}", string.Join(", ", contract.Modifiers.Select(s => s.Interface).ToArray())); writer.WriteLine(); writer.WriteLine("{"); writer.Indent += 1; if (contract.Members.Count > 0) { WriteMembers(contract, writer); writer.WriteLine(); WriteNotSoPrivateAnymoreCtor(writer, contract); writer.Write("public {0} (", contract.Name); WriteParameters(contract, writer); writer.WriteLine(")"); writer.WriteLine("{"); writer.Indent += 1; WriteAssignments(contract, writer); writer.Indent -= 1; writer.WriteLine("}"); } WriteToString(writer, contract); writer.Indent -= 1; writer.WriteLine("}"); } foreach (var entity in context.Entities) { if ((entity.Name ?? "default") == "default") continue; GenerateEntityInterface(entity, writer, "?", "public interface I{0}ApplicationService"); GenerateEntityInterface(entity, writer, "!", "public interface I{0}State"); } }
void WriteInterfaceMembers(Message message, CodeWriter writer) { foreach (var member in message.Members) { writer.WriteLine("{0} {1} {{ get; }}", member.Type,GeneratorUtil.MemberCase(member.Name)); } }
void GenerateEntityInterface(Entity entity, CodeWriter writer, string member, string template) { var ms = member.Split(','); var matches = entity.Messages.Where(m => m.Modifiers.Select(s => s.Identifier).Intersect(ms).Any()).ToList(); if (matches.Any()) { writer.WriteLine(); writer.WriteLine(template, entity.Name); writer.WriteLine("{"); writer.Indent += 1; foreach (var contract in matches) { writer.WriteLine("void When({0} {1});", contract.Name, member == "!" ? "e" : "c"); } writer.Indent -= 1; writer.WriteLine("}"); } }
void WriteAssignments(Message message, CodeWriter writer) { foreach (var member in message.Members) { writer.WriteLine("{0} = {1};", GeneratorUtil.MemberCase(member.Name), GeneratorUtil.ParameterCase(member.Name)); } }
static void WriteNotSoPrivateAnymoreCtor(CodeWriter writer, Message contract) { var arrays = contract.Members.Where(p => p.Type.EndsWith("[]")).ToArray(); if (!arrays.Any()) { writer.WriteLine(@"public {0} () {{}}", contract.Name); } else { writer.WriteLine(@"{0} () {{", contract.Name); writer.Indent += 1; foreach (var array in arrays) { writer.WriteLine("{0} = new {1};", GeneratorUtil.MemberCase(array.Name), array.Type.Replace("[]", "[0]") ); } writer.Indent -= 1; writer.WriteLine("}"); } }
static void WriteToString(CodeWriter writer, Message contract) { if (string.IsNullOrWhiteSpace(contract.StringRepresentation)) return; writer.WriteLine(); writer.WriteLine("public override string ToString()"); writer.WriteLine("{"); writer.Indent += 1; var text = contract.StringRepresentation; var active = new List<string>(); foreach (var member in contract.Members) { text = ReplaceAdd(text, "{" + member.DslName + ":", "{" + active.Count + ":", active, member); text = ReplaceAdd(text, "{" + member.DslName + "}", "{" + active.Count + "}", active, member); if (member.DslName != member.Name) { text = ReplaceAdd(text, "{" + member.Name + ":", "{" + active.Count + ":", active, member); text = ReplaceAdd(text, "{" + member.Name + "}", "{" + active.Count + "}", active, member); } } writer.Write("return string.Format(@{0}", text); foreach (var variable in active) { writer.Write(", " + GeneratorUtil.MemberCase(variable)); } writer.WriteLine(");"); writer.Indent -= 1; writer.WriteLine("}"); }
public void Generate(Context context, IndentedTextWriter outer) { var writer = new CodeWriter(outer); if (!string.IsNullOrEmpty(Namespace)) { writer.WriteLine("namespace {0}", Namespace); writer.WriteLine("{"); } writer.Indent += 1; if (!string.IsNullOrEmpty(Region)) { writer.WriteLine("#region {0}", Region); } WriteContext(writer, context); if (!string.IsNullOrEmpty(Region)) { writer.WriteLine("#endregion"); } writer.Indent -= 1; if (!string.IsNullOrEmpty(Namespace)) { writer.WriteLine("}"); } }
public void Generate(Context context, IndentedTextWriter outer) { var writer = new CodeWriter(outer); foreach (var source in context.Using.Distinct().OrderBy(s => s)) { writer.WriteLine("using {0};", source); } writer.WriteLine(@" // ReSharper disable PartialTypeWithSinglePart // ReSharper disable UnusedMember.Local"); writer.WriteLine("namespace {0}", context.CurrentNamespace); writer.WriteLine("{"); writer.Indent += 1; if (!string.IsNullOrEmpty(Region)) { writer.WriteLine("#region {0}", Region); } WriteContext(writer, context); if (!string.IsNullOrEmpty(Region)) { writer.WriteLine("#endregion"); } writer.Indent -= 1; writer.WriteLine("}"); }
private void WriteContext(CodeWriter writer, Context context) { foreach (var contract in context.Contracts) { writer.Write(ClassNameTemplate, contract.Name); if (contract.Modifiers.Any()) { writer.Write(" : {0}", string.Join(", ", contract.Modifiers.Select(s => s.Interface).ToArray())); } writer.WriteLine(); writer.WriteLine("{"); writer.Indent += 1; if (contract.Members.Count > 0) { WriteMembers(contract, writer); writer.WriteLine(PrivateCtorTemplate, contract.Name); writer.Write("public {0} (", contract.Name); WriteParameters(contract, writer); writer.WriteLine(")"); writer.WriteLine("{"); writer.Indent += 1; WriteAssignments(contract, writer); writer.Indent -= 1; writer.WriteLine("}"); } writer.Indent -= 1; writer.WriteLine("}"); } var split = (GenerateInterfaceForEntityWithModifiers ?? "") .Split(new[]{','}, StringSplitOptions.RemoveEmptyEntries); foreach (var entity in context.Entities) { if ((entity.Name??"null") == "null") continue; var matches = entity.Messages.Where(m => m.Modifiers.Select(s =>s.Identifier).Intersect(split).Any()).ToList(); if (matches.Any()) { writer.WriteLine(); writer.WriteLine(TemplateForInterfaceName, entity.Name); writer.WriteLine("{"); writer.Indent += 1; foreach (var contract in matches) { writer.WriteLine(TemplateForInterfaceMember, contract.Name); } writer.Indent -= 1; writer.WriteLine("}"); } } }
void WriteMembers(Message message, CodeWriter writer) { var idx = 1; foreach (var member in message.Members) { var annotation = string.IsNullOrWhiteSpace(member.Annotation) ? string.Empty : string.Format("{0}{1}", member.Annotation, Environment.NewLine); var isRequired = string.Empty; if (annotation.IndexOf("required", StringComparison.InvariantCultureIgnoreCase) > 0 && annotation.IndexOf("RequiredIfNull", StringComparison.InvariantCultureIgnoreCase) < 0) { isRequired = ", IsRequired=true"; } writer.WriteLine(MemberTemplate, annotation, idx, member.Type, GeneratorUtil.MemberCase(member.Name), isRequired); idx += 1; } }
private void WriteContext(CodeWriter writer, Context context) { foreach (var contract in context.Contracts) { if (contract.Modifiers.Any()) { if (contract.Modifiers.FirstOrDefault(c => c.Identifier == "!" && c.Interface != "IIdentity") != null) { writer.Write(TemplateForEventInterfaceName, contract.Name, string.Join(", ", contract.Modifiers.Select(s => s.Interface).ToArray())); writer.WriteLine(); writer.WriteLine("{"); writer.Indent += 1; if (contract.Members.Count > 0) { WriteInterfaceMembers(contract, writer); writer.WriteLine(); } writer.Indent -= 1; writer.WriteLine("}"); } } writer.Write(ClassNameTemplate, contract.Name, context.CurrentExtern, string.Empty); //} if (contract.Modifiers.Any()) { if (contract.Modifiers.FirstOrDefault(c => c.Identifier == "!" && c.Interface != "IIdentity") != null) { //writer.Write(" : DomainEvent, {0}", string.Join(", ", contract.Modifiers.Select(s => s.Interface).ToArray())); writer.Write(" : DomainEvent, {0}", string.Format("I{0}", contract.Name)); } //else if (contract.Modifiers.FirstOrDefault(c => c.Identifier == "?" && c.Interface != "IIdentity") != null) //{ // writer.Write(" : DomainCommand, {0}", string.Join(", ", contract.Modifiers.Select(s => s.Interface).ToArray())); //} else { writer.Write(" : {0}", string.Join(", ", contract.Modifiers.Select(s => s.Interface).ToArray())); } } writer.WriteLine(); writer.WriteLine("{"); writer.Indent += 1; if (contract.Members.Count > 0) { WriteMembers(contract, writer); writer.WriteLine(); WritePrivateCtor(writer, contract); if (contract.Modifiers.FirstOrDefault(c => c.Identifier == "!" && c.Interface != "IIdentity") != null) { writer.Write("public {0} (TenancyId tenancyId, int aggregateVersion, ", contract.Name); } else if (contract.Modifiers.FirstOrDefault(c => c.Interface == "DomainCommand") != null) { writer.Write("public {0} (TenancyId tenancyId, int aggregateVersion, ", contract.Name); } else { writer.Write("public {0} (", contract.Name); } WriteParameters(contract, writer); if (contract.Modifiers.FirstOrDefault(c => c.Identifier == "!" && c.Interface != "IIdentity") != null) { writer.WriteLine(") : base(tenancyId, id, aggregateVersion)"); } else if (contract.Modifiers.FirstOrDefault(c => c.Interface == "DomainCommand") != null) { writer.WriteLine(") : base(tenancyId, aggregateVersion)"); } else { writer.WriteLine(")"); } writer.WriteLine("{"); writer.Indent += 1; WriteAssignments(contract, writer); writer.Indent -= 1; writer.WriteLine("}"); } WriteToString(writer, contract); if (contract.Modifiers.FirstOrDefault(c => c.Identifier == "!" && c.Interface == "IIdentity") != null) { writer.WriteLine(@"public override string ToString() {{ return Id.ToString(); }} protected bool Equals({0} other) {{ return Id.Equals(other.Id); }} public override bool Equals(object obj) {{ if (ReferenceEquals(null, obj)) return false; if (ReferenceEquals(this, obj)) return true; if (obj.GetType() != this.GetType()) return false; return Equals(({0}) obj); }} public override int GetHashCode() {{ return Id.GetHashCode(); }} public static bool operator ==({0} a, {0} b) {{ if (ReferenceEquals(a, b)) {{ return true; }} if (((object) a == null) || ((object) b == null)) {{ return false; }} return a.Equals(b); }} public static bool operator !=({0} a, {0} b) {{ return !(a == b); }} public static {0} New() {{ return new {0}(Guid.NewGuid()); }} public static {0} Empty() {{ return new {0}(Guid.Empty); }}", contract.Name); } writer.Indent -= 1; writer.WriteLine("}"); } foreach (var entity in context.Entities) { if ((entity.Name ?? "default") == "default") continue; GenerateEntityInterface(entity, writer, "?", "public interface I{0}Aggregate"); GenerateEntityInterface(entity, writer, "!", "public interface I{0}AggregateState"); } }
public void Generate(Context context, IndentedTextWriter outer) { // Apply templates foreach (var template in context.Templates) { if(String.IsNullOrWhiteSpace(template.Key) || String.IsNullOrWhiteSpace(template.Value)) continue; var propertyInfo = GetType().GetProperty(template.Key); if(propertyInfo == null) continue; try { propertyInfo.SetValue(this, template.Value, null); } catch (Exception) { Console.WriteLine("Error when setting value for template '{0}", template.Key); } } var writer = new CodeWriter(outer); foreach (var source in context.Using.Distinct().OrderBy(s => s)) { writer.WriteLine("using {0};", source); } writer.WriteLine(@" // ReSharper disable PartialTypeWithSinglePart // ReSharper disable UnusedMember.Local"); writer.WriteLine("namespace {0}", context.CurrentNamespace); writer.WriteLine("{"); writer.Indent += 1; if (!string.IsNullOrEmpty(Region)) { writer.WriteLine("#region {0}", Region); } WriteContext(writer, context); if (!string.IsNullOrEmpty(Region)) { writer.WriteLine("#endregion"); } writer.Indent -= 1; writer.WriteLine("}"); }
static void WriteSummary(CodeWriter writer, Message contract) { if (!string.IsNullOrWhiteSpace(contract.Representation) && contract.Representation.StartsWith("\"")) { writer.WriteLine("/// <summary>"); foreach (var line in contract.Representation.Trim('"').Split('\n')) { writer.WriteLine("/// " + line.Trim()); } writer.WriteLine("/// </summary>"); } }