Remove redundant using statement in generated code.

This commit is contained in:
Mohammadreza Taikandi 2020-12-22 08:28:14 +00:00
parent 29f96b670e
commit 8ec2830663
3 changed files with 21 additions and 16 deletions

View File

@ -68,7 +68,7 @@ namespace MapTo
.AppendLine()
.AppendLine()
.PadLeft(Indent1)
.AppendFormat("{0} static partial class {1}Extensions", model.ClassModifiers.FirstOrDefault().ToFullString().Trim(), model.SourceClassName)
.AppendFormat("{0} static partial class {1}To{2}Extensions", model.ClassModifiers.FirstOrDefault().ToFullString().Trim(), model.SourceClassName, model.ClassName)
.AppendOpeningBracket(Indent1)
// Extension class body
@ -86,13 +86,6 @@ namespace MapTo
private static StringBuilder GenerateUsings(this StringBuilder builder, MapModel model)
{
builder.AppendLine("using System;");
// NB: If class names are the same, we're going to use fully qualified names instead.
if (model.Namespace != model.SourceNamespace)
{
builder.AppendFormat("using {0};", model.SourceNamespace).AppendLine();
}
return builder.AppendLine();
}
@ -109,14 +102,20 @@ namespace MapTo
.AppendLine();
mappedProperties = new List<IPropertySymbol>();
foreach (var propertySymbol in model.SourceTypeProperties)
if (model.SourceTypeProperties.Any())
{
if (model.Properties.Any(p => p.Name == propertySymbol.Name))
builder.AppendLine();
foreach (var propertySymbol in model.SourceTypeProperties)
{
mappedProperties.Add(propertySymbol);
builder
.PadLeft(Indent3)
.AppendFormat("{0} = {1}.{2};{3}", propertySymbol.Name, sourceClassParameterName, propertySymbol.Name, Environment.NewLine);
if (model.Properties.Any(p => p.Name == propertySymbol.Name))
{
mappedProperties.Add(propertySymbol);
builder
.PadLeft(Indent3)
.AppendFormat("{0} = {1}.{2};{3}", propertySymbol.Name, sourceClassParameterName, propertySymbol.Name, Environment.NewLine);
}
}
}

View File

@ -52,6 +52,7 @@ namespace App.ViewModels
public UserViewModel(App.Data.Models.User user)
{
if (user == null) throw new ArgumentNullException(nameof(user));
FirstName = user.FirstName;
LastName = user.LastName;
}
@ -62,7 +63,7 @@ namespace App.ViewModels
}
}
public static partial class UserExtensions
public static partial class UserToUserViewModelExtensions
{
public static UserViewModel ToUserViewModel(this App.Data.Models.User user)
{

View File

@ -3,5 +3,10 @@
namespace TestConsoleApp.ViewModels
{
[MapFrom(typeof(Data.Models.User))]
public partial class UserViewModel { }
public partial class UserViewModel
{
public string FirstName { get; }
public string LastName { get; }
}
}