Skip to content

Commit

Permalink
♻️ refactor project structure
Browse files Browse the repository at this point in the history
refactor toward separation of projects by use cases
  • Loading branch information
doroudi committed Feb 19, 2024
1 parent 9ad2b49 commit 452e969
Show file tree
Hide file tree
Showing 41 changed files with 315 additions and 307 deletions.
8 changes: 4 additions & 4 deletions ExcelMapper.Test/MapperProfiles/EmployeeMapperProfile.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using ExcelMapper.ExcelMapper;
using ExcelMapper.Test.Models;
using ExcelMapper.Validations;
using ExcelMapper.Test.Models;
using YummyCode.ExcelMapper.ImportMapper;
using YummyCode.ExcelMapper.Validations;

namespace ExcelMapper.Test.MapperProfiles
namespace YummyCode.ExcelMapper.Test.MapperProfiles
{
public class EmployeeMapperProfile : ExcelImportMapper<Person>
{
Expand Down
18 changes: 9 additions & 9 deletions ExcelMapper.Test/MapperProfiles/ExportProfile.cs
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
using ExcelMapper.ExcelExporter;
using ExcelMapper.Test.Models;
using ExcelMapper.Test.Models;
using NPOI.SS.UserModel;
using YummyCode.ExcelMapper.Exporter;

namespace ExcelMapper.Test.MapperProfiles
namespace YummyCode.ExcelMapper.Test.MapperProfiles
{
public class ExportProfile: ExportMapper<Person>
{
public ExportProfile(IWorkbook workbook): base(workbook)
{
_ = CreateMap()
.ForColumn("A", x => x.Name, opt => opt.WithTitle("Name"))
.ForColumn("B", x => x.Family, opt => opt.WithTitle("Family"))
.ForColumn("C", x => x.BirthDate, opt => opt.WithTitle("BirthDate").UseAction(ConvertToPersian))
.ForColumn("D", x => x.Address, opt => opt.WithTitle("Address"))
.ForColumn("E", x => x.Name, opt => opt.WithTitle("NAME").UseAction(x => x?.ToUpper()));
.ForColumn("A", x => x.Name, opt => opt.WithHeader("Name"))
.ForColumn("B", x => x.Family, opt => opt.WithHeader("Family"))
.ForColumn("C", x => x.BirthDate, opt => opt.WithHeader("BirthDate").UseAction(ConvertToPersian))
.ForColumn("D", x => x.Address, opt => opt.WithHeader("Address"))
.ForColumn("E", x => x.Name, opt => opt.WithHeader("NAME").UseAction(x => x?.ToUpper()));
// .ForColumn("B", opt => opt.MapFrom<Employee>(x => x.Name));
}

private string ConvertToPersian(DateTime? arg)
private static string ConvertToPersian(DateTime? arg)
{
return arg?.ToShortDateString() ?? "";
}
Expand Down
12 changes: 6 additions & 6 deletions ExcelMapper.Test/Program.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
using ExcelMapper;
using ExcelMapper.ExcelParser;
using ExcelMapper.Logger;
using ExcelMapper.Test.MapperProfiles;
using ExcelMapper.Test.Models;
using ExcelMapper.Test.Models;
using YummyCode.ExcelMapper.ExcelParser;
using YummyCode.ExcelMapper.Exporter;
using YummyCode.ExcelMapper.Logger;
using YummyCode.ExcelMapper.Test.MapperProfiles;

var fileName = @"AppData\persons.xlsx";
const string fileName = @"AppData\persons.xlsx";
ExcelParser<Person> parser = new (new FileInfo(fileName), new EmployeeMapperProfile());
var people = parser.GetItems();

Expand Down
4 changes: 3 additions & 1 deletion ExcelMapper.Test/YummyCode.ExcelMapper.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\ExcelMapper\YummyCode.ExcelMapper.csproj" />
<ProjectReference Include="..\ExcelMapper\YummyCode.ExcelMapper.Importer.csproj" />
<ProjectReference Include="..\YummyCode.ExcelMapper.Exporter\YummyCode.ExcelMapper.Exporter.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 19 additions & 1 deletion ExcelMapper.sln
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.1.32407.343
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YummyCode.ExcelMapper", "ExcelMapper\YummyCode.ExcelMapper.csproj", "{B72766E3-836D-48EA-ABF4-38C1F16F0DCE}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YummyCode.ExcelMapper.Importer", "ExcelMapper\YummyCode.ExcelMapper.Importer.csproj", "{B72766E3-836D-48EA-ABF4-38C1F16F0DCE}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{5D816DBB-F928-4AAC-AC10-B4170D353844}"
ProjectSection(SolutionItems) = preProject
Expand All @@ -14,6 +14,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "YummyCode.ExcelMapper.Test", "ExcelMapper.Test\YummyCode.ExcelMapper.Test.csproj", "{8D343B10-CD04-43A2-A70B-8BED34DCB78E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YummyCode.ExcelMapper.Exporter", "YummyCode.ExcelMapper.Exporter\YummyCode.ExcelMapper.Exporter.csproj", "{B051350E-13E1-4A0C-B1DE-C56DF0109A8E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YummyCode.ExcelMapper.Shared", "YummyCode.ExcelMapper.Shared\YummyCode.ExcelMapper.Shared.csproj", "{E3C9F767-3DA2-4C10-BC19-BF49E63243C5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "YummyCode.ExcelMapper.Logger", "YummyCode.ExcelMapper.Logger\YummyCode.ExcelMapper.Logger.csproj", "{2CDAA3F2-2812-44C5-AD8B-27B24CC0D2F4}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -28,6 +34,18 @@ Global
{8D343B10-CD04-43A2-A70B-8BED34DCB78E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8D343B10-CD04-43A2-A70B-8BED34DCB78E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8D343B10-CD04-43A2-A70B-8BED34DCB78E}.Release|Any CPU.Build.0 = Release|Any CPU
{B051350E-13E1-4A0C-B1DE-C56DF0109A8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B051350E-13E1-4A0C-B1DE-C56DF0109A8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B051350E-13E1-4A0C-B1DE-C56DF0109A8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B051350E-13E1-4A0C-B1DE-C56DF0109A8E}.Release|Any CPU.Build.0 = Release|Any CPU
{E3C9F767-3DA2-4C10-BC19-BF49E63243C5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E3C9F767-3DA2-4C10-BC19-BF49E63243C5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E3C9F767-3DA2-4C10-BC19-BF49E63243C5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E3C9F767-3DA2-4C10-BC19-BF49E63243C5}.Release|Any CPU.Build.0 = Release|Any CPU
{2CDAA3F2-2812-44C5-AD8B-27B24CC0D2F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2CDAA3F2-2812-44C5-AD8B-27B24CC0D2F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2CDAA3F2-2812-44C5-AD8B-27B24CC0D2F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2CDAA3F2-2812-44C5-AD8B-27B24CC0D2F4}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
17 changes: 10 additions & 7 deletions ExcelMapper/ExcelParser/ExcelParser{T}.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
using ExcelMapper.ExcelMapper;
using ExcelMapper.Exceptions;
using ExcelMapper.Models;
using ExcelMapper.Util;
using NPOI.SS.UserModel;
using System;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using ExcelMapper.ExcelMapper;
using ExcelMapper.Util;
using NPOI.SS.UserModel;
using YummyCode.ExcelMapper.Exceptions;
using YummyCode.ExcelMapper.ImportMapper;
using YummyCode.ExcelMapper.Shared.Extensions;
using YummyCode.ExcelMapper.Shared.Models;
using YummyCode.ExcelMapper.Shared.Utilities;

namespace ExcelMapper.ExcelParser
namespace YummyCode.ExcelMapper.ExcelParser
{
public class ExcelParser<TSource> where TSource : new()
{
Expand Down
14 changes: 5 additions & 9 deletions ExcelMapper/Exceptions/ExcelMappingException.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
using ExcelMapper.Models;
using System;
using System;
using System.Collections.Generic;
using YummyCode.ExcelMapper.Shared.Models;

namespace ExcelMapper.Exceptions
namespace YummyCode.ExcelMapper.Exceptions
{
public class ExcelMappingException : Exception
public class ExcelMappingException(Dictionary<string, ResultState> cols) : Exception
{
public Dictionary<string, ResultState> Cols { get; set; }
public ExcelMappingException(Dictionary<string, ResultState> cols)
{
Cols = cols;
}
public Dictionary<string, ResultState> Cols { get; set; } = cols;
}
}
29 changes: 14 additions & 15 deletions ExcelMapper/ImportMapper/ExcelImportMapper.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using ExcelMapper.Exceptions;
using ExcelMapper.Models;
using ExcelMapper.Util;
using NPOI.SS.UserModel;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ExcelMapper.ExcelMapper;
using ExcelMapper.Util;
using NPOI.SS.UserModel;
using YummyCode.ExcelMapper.Exceptions;
using YummyCode.ExcelMapper.Shared.Extensions;
using YummyCode.ExcelMapper.Shared.Models;
using YummyCode.ExcelMapper.Shared.Utilities;

namespace ExcelMapper.ExcelMapper
namespace YummyCode.ExcelMapper.ImportMapper
{
public abstract class ExcelImportMapper<TDestination> : IImportMapper<TDestination> where TDestination : new()
{
private IImportMappingExpression<TDestination> _mappingExpression;


public IImportMappingExpression<TDestination> CreateMap()
{
Expand Down Expand Up @@ -63,13 +67,8 @@ public TDestination Map(ISheet sheet, IRow row)
try
{
var actions = GetMappingActions(propertyInfo);
object converted = value;
foreach (var action in actions)
{

converted = action.Compile().DynamicInvoke(converted);

}
var converted = actions.Aggregate<LambdaExpression?, object>(value, (current, action) =>
action.Compile().DynamicInvoke(current));
TypeConverter.SetValue(item, propertyInfo.Name, converted);
}
catch (Exception ex)
Expand Down Expand Up @@ -113,7 +112,7 @@ private string GetMappingCol(PropertyInfo property)
return _mappingExpression.GetCol(property);
}

private List<LambdaExpression> GetMappingActions(PropertyInfo property)
private IEnumerable<LambdaExpression> GetMappingActions(PropertyInfo property)
{
return _mappingExpression.GetActions(property);
}
Expand Down
5 changes: 3 additions & 2 deletions ExcelMapper/ImportMapper/IImportMapper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using ExcelMapper.Exceptions;
using NPOI.SS.UserModel;
using NPOI.SS.UserModel;
using YummyCode.ExcelMapper.Exceptions;
using YummyCode.ExcelMapper.ImportMapper;

namespace ExcelMapper.ExcelMapper
{
Expand Down
7 changes: 4 additions & 3 deletions ExcelMapper/ImportMapper/IImportMappingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using ExcelMapper.ExcelMapper;

namespace ExcelMapper.ExcelMapper
namespace YummyCode.ExcelMapper.ImportMapper
{
public interface IImportMappingExpression<TDestination>
{
IImportMappingExpression<TDestination> ForMember<TMember>(Expression<Func<TDestination, TMember>> destinationMember, Action<ExcelMemberConfigurationExpression<TDestination, TMember>> memberOptions);
string GetCol(PropertyInfo propertyInfo);
List<LambdaExpression> GetActions(PropertyInfo propertyInfo);
IEnumerable<LambdaExpression> GetActions(PropertyInfo propertyInfo);
List<LambdaExpression> GetValidations(PropertyInfo propertyInfo);
List<string> GetIgnoredValues(PropertyInfo propertyInfo);

// TODO: implement ForAllMemebers
// TODO: implement ForAllMembers
//IImportMappingExpression<TDestination> ForAllMembers
// (Action<ImportConfigurationExpression<TDestination>> memberOptions);

Expand Down
16 changes: 8 additions & 8 deletions ExcelMapper/ImportMapper/ImportMappingExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using ExcelMapper.Models;
using ExcelMapper.ExcelMapper;
using YummyCode.ExcelMapper.Models;

namespace ExcelMapper.ExcelMapper
namespace YummyCode.ExcelMapper.ImportMapper
{
public class ImportMappingExpression<TDestination> : IImportMappingExpression<TDestination>
{
private readonly List<PropertyMapInfo> _memberConfigurations =
new List<PropertyMapInfo>();
private readonly List<PropertyMapInfo> _memberConfigurations = [];

public IImportMappingExpression<TDestination> ForMember<TMember>
(Expression<Func<TDestination, TMember>> destinationMember,
Expand All @@ -34,17 +34,17 @@ public IImportMappingExpression<TDestination> ForMember<TMember>

}

public List<LambdaExpression> GetActions(PropertyInfo property)
public IEnumerable<LambdaExpression> GetActions(PropertyInfo property)
{
return _memberConfigurations
.FirstOrDefault(x => x.Property.Name == property.Name)?
.Actions ?? new List<LambdaExpression>();
.Actions ?? [];
}
public List<LambdaExpression> GetValidations(PropertyInfo property)
{
return _memberConfigurations
.FirstOrDefault(x => x.Property.Name == property.Name)?
.Validations ?? new List<LambdaExpression>();
.Validations ?? [];
}

public string GetCol(PropertyInfo property)
Expand All @@ -54,7 +54,7 @@ public string GetCol(PropertyInfo property)

public List<string> GetIgnoredValues(PropertyInfo property)
{
return _memberConfigurations.FirstOrDefault(x => x.Property.Name == property.Name)?.IgnoredValues ?? new List<string>();
return _memberConfigurations.FirstOrDefault(x => x.Property.Name == property.Name)?.IgnoredValues ?? [];
}
}
}
10 changes: 3 additions & 7 deletions ExcelMapper/Models/ExcelColAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
using System;

namespace ExcelMapper.Models
namespace YummyCode.ExcelMapper.Models
{
public sealed class ExcelColAttribute : Attribute
public sealed class ExcelColAttribute(string col) : Attribute
{
public string Col { get; set; }
public ExcelColAttribute(string col)
{
Col = col;
}
public string Col { get; set; } = col;
}
}
31 changes: 6 additions & 25 deletions ExcelMapper/Models/PropertyMapInfo.cs
Original file line number Diff line number Diff line change
@@ -1,35 +1,16 @@
using NPOI.SS.UserModel;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;

namespace ExcelMapper.Models
namespace YummyCode.ExcelMapper.Models
{
public sealed class PropertyMapInfo
public sealed class PropertyMapInfo(string name, PropertyInfo property, string columnName)
{
public PropertyMapInfo(string name, PropertyInfo property, string columnName)
{
Name = name;
Property = property;
ColumnName = columnName;
}

public string Name { get; set; }
public PropertyInfo Property { get; set; }
public string ColumnName { get; set; }
public string Name { get; set; } = name;
public PropertyInfo Property { get; set; } = property;
public string ColumnName { get; set; } = columnName;
public List<LambdaExpression> Actions { get; set; } = new();
public List<LambdaExpression> Validations { get; set; } = new();
public List<string> IgnoredValues { get; set; } = new List<string>();
}

public sealed class CellMappingInfo
{
public int Column { get; set; }
public string? Title { get; set; }
public PropertyInfo? Property { get; set; }
public List<LambdaExpression> Actions { get; set; } = new();
public ICellStyle? Style { get; set; }
public string? ConstValue { get; set; }
public string? DefaultValue { get; set; }
}
}
19 changes: 0 additions & 19 deletions ExcelMapper/Models/RowModel{T}.cs

This file was deleted.

4 changes: 2 additions & 2 deletions ExcelMapper/Models/RowState.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using ExcelMapper.Models;
using System.Collections.Generic;
using System.Collections.Generic;
using YummyCode.ExcelMapper.Shared.Models;

namespace YummyCode.ExcelMapper.Models
{
Expand Down
Loading

0 comments on commit 452e969

Please sign in to comment.