Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot create an instance of the abstract type or interface 'ICollection<SomeDto>' #12

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added assets/images/icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/icon_192.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 3 additions & 3 deletions src/Mappee/Configuration/Profile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ private void CreateMappings(Type src, Type dest)
if (destinationProperty != null)
{
string mapping;
if (sourceProperty.PropertyType.IsClass && sourceProperty.PropertyType != typeof(string))
if ((sourceProperty.PropertyType.IsClass || sourceProperty.PropertyType.IsAbstract || sourceProperty.PropertyType.IsInterface) && sourceProperty.PropertyType != typeof(string))
{
if (sourceProperty.PropertyType.GetInterface(nameof(IEnumerable)) != null)
if (sourceProperty.PropertyType.GetInterface(nameof(IEnumerable)) != null || sourceProperty.PropertyType.GetInterface(nameof(ICollection)) != null)
{
var genericSourceType = sourceProperty.PropertyType.GetGenericArguments()[0];
var genericDestinationType = destinationProperty.PropertyType.GetGenericArguments()[0];
mapping = $"classInstance.{destinationProperty.Name} = new();\r\nsource.{sourceProperty.Name}?.ForEach(x => classInstance.{destinationProperty.Name}.Add(__Mappe_Quick_Mapping.{GenerateMethodName(genericSourceType, genericDestinationType)}(x)));";
mapping = $"classInstance.{destinationProperty.Name} = new List<{genericDestinationType.Namespace}.{genericDestinationType.Name}>();\r\n((List<{genericSourceType.Namespace}.{genericSourceType.Name}>)source.{sourceProperty.Name})?.ForEach(x => classInstance.{destinationProperty.Name}.Add(__Mappe_Quick_Mapping.{GenerateMethodName(genericSourceType, genericDestinationType)}(x)));";
}
else
{
Expand Down
Binary file added src/Mappee/Images/icon.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 41 additions & 37 deletions src/Mappee/Mappee.csproj
Original file line number Diff line number Diff line change
@@ -1,40 +1,44 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7;net8;net481</TargetFrameworks>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Description>Mappee - Fastest avaiable mapper for .NET (AutoMapper / TinyMapper / Mapster / Mapperly alternative)</Description>
<Copyright>https://github.com/sonquer/mappee/blob/main/LICENSE</Copyright>
<PackageProjectUrl>https://github.com/sonquer/mappee</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RepositoryUrl>https://github.com/sonquer/mappee</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Authors>Patryk Pasek</Authors>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<Version>0.1.0.0-alpha</Version>
</PropertyGroup>

<ItemGroup>
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Properties\" />
</ItemGroup>

<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net7;net8;net481</TargetFrameworks>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Description>Mappee - Fastest avaiable mapper for .NET (AutoMapper / TinyMapper / Mapster / Mapperly alternative)</Description>
<Copyright>Copyright (c) 2024 Patryk Pasek</Copyright>
<PackageLicenseUrl>https://github.com/sonquer/mappee/blob/main/LICENSE</PackageLicenseUrl>
<PackageProjectUrl>https://github.com/sonquer/mappee</PackageProjectUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageTags>Mappee;Mapper</PackageTags>
<RepositoryUrl>https://github.com/sonquer/mappee</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<Authors>Patryk Pasek</Authors>
<AssemblyVersion>0.1.1.0</AssemblyVersion>
<FileVersion>0.1.1.0</FileVersion>
<Version>0.1.1.0-alpha</Version>
<PackageIcon>icon.jpg</PackageIcon>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>
<ItemGroup>
<InternalsVisibleTo Include="Mappee.UnitTests" />
<None Include="..\..\README.md">
<Pack>True</Pack>
<PackagePath>\</PackagePath>
</None>
</ItemGroup>

</Project>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis" Version="4.8.0"/>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.8.0"/>
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.0"/>
</ItemGroup>
<ItemGroup>
<None Include="Images\icon.jpg" Pack="true" PackagePath="\"/>
</ItemGroup>
<ItemGroup>
<Folder Include="Properties\"/>
</ItemGroup>
<ItemGroup>
<InternalsVisibleTo Include="Mappee.UnitTests"/>
</ItemGroup>
</Project>
4 changes: 2 additions & 2 deletions src/Tests/Benchmark/Models/Entities/TestObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public sealed class TestObject
public string Nickname { get; set; }
public short Short { get; set; }
public List<TestObjectField> Fields { get; set; } = new();
public List<TestObjectModification> Modifications { get; set; } = new();
public List<TestObjectLink> Links { get; set; } = new();
public IEnumerable<TestObjectModification> Modifications { get; set; } = new List<TestObjectModification>();
public ICollection<TestObjectLink> Links { get; set; } = new List<TestObjectLink>();
public TestObject Child { get; set; }
}
8 changes: 7 additions & 1 deletion src/Tests/Mappee.UnitTests/Configuration/ProfileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public void Compile_ReturnsCompilationResult_WhenSuccessful()
{
var profile = new Profile();
profile.Map<TestObject, TestObjectDto>();
profile.Map<TestObject, TestObjectDto>();
profile.Map<TestObjectField, TestObjectFieldDto>();

var result = profile.Compile(compilationResult: true);

Expand All @@ -25,6 +25,7 @@ public void Compile_ReturnsGeneratedCode_WhenSuccessful()
{
var profile = new Profile();
profile.Map<TestObject, TestObjectDto>();
profile.Map<TestObjectField, TestObjectFieldDto>();

var sourceCode = profile.Compile(compilationResult: true);

Expand All @@ -44,6 +45,8 @@ public void Compile_ReturnsGeneratedCode_WhenSuccessful()
Assert.Contains("classInstance.Long = source.Long;", sourceCode.GeneratedCode);
Assert.Contains("classInstance.Nickname = source.Nickname;", sourceCode.GeneratedCode);
Assert.Contains("classInstance.Short = source.Short;", sourceCode.GeneratedCode);
Assert.Contains("classInstance.Fields = new List<Mappee.UnitTests.Models.TestObjectFieldDto>();", sourceCode.GeneratedCode);
Assert.Contains("((List<Mappee.UnitTests.Models.TestObjectField>)source.Fields)?.ForEach(x => classInstance.Fields.Add(__Mappe_Quick_Mapping.__Mappee_UnitTests_Models_TestObjectField_to_Mappee_UnitTests_Models_TestObjectFieldDto(x)));", sourceCode.GeneratedCode);
Assert.Contains("return classInstance;", sourceCode.GeneratedCode);
}

Expand All @@ -52,6 +55,7 @@ public void Compile_ReturnsNull_WhenCompilationResultIsFalse()
{
var profile = new Profile();
profile.Map<TestObject, TestObjectDto>();
profile.Map<TestObjectField, TestObjectFieldDto>();

var result = profile.Compile(compilationResult: false);

Expand All @@ -63,6 +67,7 @@ public void Compile_ReturnsGeneratedCodeWithoutOptimization_WhenAggressiveOptimi
{
var profile = new Profile();
profile.Map<TestObject, TestObjectDto>();
profile.Map<TestObjectField, TestObjectFieldDto>();

var result = profile.Compile(compilationResult: true, aggressiveOptimization: false);

Expand All @@ -74,6 +79,7 @@ public void Compile_ReturnsGeneratedCodeWithOptimization_WhenAggressiveOptimizat
{
var profile = new Profile();
profile.Map<TestObject, TestObjectDto>();
profile.Map<TestObjectField, TestObjectFieldDto>();

var result = profile.Compile(compilationResult: true, aggressiveOptimization: true);

Expand Down
1 change: 1 addition & 0 deletions src/Tests/Mappee.UnitTests/Models/TestObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public sealed class TestObject
public long Long { get; set; }
public string Nickname { get; set; }
public short Short { get; set; }
public ICollection<TestObjectField> Fields { get; set; } = new List<TestObjectField>();
}
1 change: 1 addition & 0 deletions src/Tests/Mappee.UnitTests/Models/TestObjectDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ public sealed class TestObjectDto
public long Long { get; set; }
public string Nickname { get; set; }
public short Short { get; set; }
public List<TestObjectFieldDto> Fields { get; set; } = new();
}
8 changes: 8 additions & 0 deletions src/Tests/Mappee.UnitTests/Models/TestObjectField.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Mappee.UnitTests.Models;

public sealed class TestObjectField
{
public long Id { get; set; }

public string Name { get; set; }
}
8 changes: 8 additions & 0 deletions src/Tests/Mappee.UnitTests/Models/TestObjectFieldDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Mappee.UnitTests.Models;

public sealed class TestObjectFieldDto
{
public long Id { get; set; }

public string Name { get; set; }
}