From 8ec28306638215188ea741412e8cb82823188927 Mon Sep 17 00:00:00 2001 From: Mohammadreza Taikandi Date: Tue, 22 Dec 2020 08:28:14 +0000 Subject: [PATCH] Remove redundant using statement in generated code. --- MapTo/SourceBuilder.cs | 27 +++++++++++----------- README.md | 3 ++- TestConsoleApp/ViewModels/UserViewModel.cs | 7 +++++- 3 files changed, 21 insertions(+), 16 deletions(-) diff --git a/MapTo/SourceBuilder.cs b/MapTo/SourceBuilder.cs index d7ebdca..2b32327 100644 --- a/MapTo/SourceBuilder.cs +++ b/MapTo/SourceBuilder.cs @@ -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(); - 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); + } } } diff --git a/README.md b/README.md index 2fbf009..3cc717e 100644 --- a/README.md +++ b/README.md @@ -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) { diff --git a/TestConsoleApp/ViewModels/UserViewModel.cs b/TestConsoleApp/ViewModels/UserViewModel.cs index 2560b98..0b56b07 100644 --- a/TestConsoleApp/ViewModels/UserViewModel.cs +++ b/TestConsoleApp/ViewModels/UserViewModel.cs @@ -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; } + } } \ No newline at end of file