Add xml docs.

This commit is contained in:
Mohammadreza Taikandi 2020-12-27 08:13:21 +00:00
parent f7a0e42380
commit f13f8783c3
1 changed files with 53 additions and 14 deletions

View File

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