diff --git a/src/MapTo/SourceBuilder.cs b/src/MapTo/SourceBuilder.cs index 8c3f4df..2db8920 100644 --- a/src/MapTo/SourceBuilder.cs +++ b/src/MapTo/SourceBuilder.cs @@ -1,4 +1,5 @@ -using System.Linq; +using System; +using System.Linq; using System.Text; using MapTo.Extensions; using MapTo.Models; @@ -11,7 +12,7 @@ namespace MapTo internal const string MapFromAttributeName = "MapFrom"; internal const string IgnorePropertyAttributeName = "IgnoreProperty"; internal const string GeneratedFilesHeader = "// "; - + private const int Indent1 = 4; private const int Indent2 = Indent1 * 2; private const int Indent3 = Indent1 * 3; @@ -23,14 +24,24 @@ using System; namespace MapTo {{ + /// + /// Specifies that the annotated class can be mapped from the provided . + /// [AttributeUsage(AttributeTargets.Class, Inherited = false, AllowMultiple = false)] public sealed class {MapFromAttributeName}Attribute : Attribute {{ + /// + /// Initializes a new instance of the class + /// with the specified . + /// public {MapFromAttributeName}Attribute(Type sourceType) {{ SourceType = sourceType; }} + /// + /// Gets the type of the class that the annotated class should be able to map from. + /// public Type SourceType {{ get; }} }} }}"; @@ -45,6 +56,9 @@ using System; namespace MapTo {{ + /// + /// Specified that the annotated property should not be included in the generated mappings. + /// [AttributeUsage(AttributeTargets.Property, Inherited = false, AllowMultiple = false)] public sealed class {IgnorePropertyAttributeName}Attribute : Attribute {{ }} }}"; @@ -102,16 +116,25 @@ namespace MapTo return builder.AppendLine(); } + /// + /// + /// + /// + /// + /// private static StringBuilder GenerateConstructor(this StringBuilder builder, MapModel model) { var sourceClassParameterName = model.SourceClassName.ToCamelCase(); builder - .PadLeft(Indent2) - .AppendFormat("public {0}({1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// Initializes a new instance of the class", model.ClassName).AppendLine() + .PadLeft(Indent2).AppendFormat("/// using the property values from the specified .", sourceClassParameterName).AppendLine() + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// {0} is null", sourceClassParameterName).AppendLine() + .PadLeft(Indent2).AppendFormat("public {0}({1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) .AppendOpeningBracket(Indent2) - .PadLeft(Indent3) - .AppendFormat("if ({0} == null) throw new ArgumentNullException(nameof({0}));", sourceClassParameterName).AppendLine() + .PadLeft(Indent3).AppendFormat("if ({0} == null) throw new ArgumentNullException(nameof({0}));", sourceClassParameterName).AppendLine() .AppendLine(); foreach (var property in model.MappedProperties) @@ -126,17 +149,23 @@ namespace MapTo return builder.AppendClosingBracket(Indent2, false); } + /// + /// + /// + /// + /// + /// + /// private static StringBuilder GenerateFactoryMethod(this StringBuilder builder, MapModel model) { var sourceClassParameterName = model.SourceClassName.ToCamelCase(); return builder .AppendLine() - .PadLeft(Indent2) - .AppendFormat("public static {0} From({1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) + .AppendConvertorMethodsXmlDocs(model, sourceClassParameterName) + .PadLeft(Indent2).AppendFormat("public static {0} From({1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) .AppendOpeningBracket(Indent2) - .PadLeft(Indent3) - .AppendFormat("return {0} == null ? null : new {1}({0});", sourceClassParameterName, model.ClassName) + .PadLeft(Indent3).AppendFormat("return {0} == null ? null : new {1}({0});", sourceClassParameterName, model.ClassName) .AppendClosingBracket(Indent2); } @@ -145,15 +174,25 @@ namespace MapTo var sourceClassParameterName = model.SourceClassName.ToCamelCase(); return builder - .PadLeft(Indent2) - .AppendFormat("public static {0} To{0}(this {1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) + .AppendConvertorMethodsXmlDocs(model, sourceClassParameterName) + .PadLeft(Indent2).AppendFormat("public static {0} To{0}(this {1} {2})", model.ClassName, model.SourceClassFullName, sourceClassParameterName) .AppendOpeningBracket(Indent2) - .PadLeft(Indent3) - .AppendFormat("return {0} == null ? null : new {1}({0});", sourceClassParameterName, model.ClassName) + .PadLeft(Indent3).AppendFormat("return {0} == null ? null : new {1}({0});", sourceClassParameterName, model.ClassName) .AppendClosingBracket(Indent2); } private static StringBuilder AppendFileHeader(this StringBuilder builder) => builder.AppendLine(GeneratedFilesHeader); + + private static StringBuilder AppendConvertorMethodsXmlDocs(this StringBuilder builder, MapModel model, string sourceClassParameterName) + { + return builder + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// Creates a new instance of and sets its participating properties", model.ClassName).AppendLine() + .PadLeft(Indent2).AppendFormat("/// using the property values from .", sourceClassParameterName).AppendLine() + .PadLeft(Indent2).AppendLine("/// ") + .PadLeft(Indent2).AppendFormat("/// Instance of to use as source.", sourceClassParameterName, model.SourceClassName).AppendLine() + .PadLeft(Indent2).AppendFormat("/// A new instance of -or- null if is null.", model.ClassName, sourceClassParameterName).AppendLine(); + } } } \ No newline at end of file