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

Many to many mapping #13

Merged
merged 9 commits into from
Aug 19, 2024
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
44 changes: 35 additions & 9 deletions Automapify.Client/BenchmarkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,34 @@
using Automapify.Client.Models.Dtos;
using Automappify.Services;
using BenchmarkDotNet.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automapify.Client
{
public class BenchmarkTest
{
public BenchmarkTest()
[GlobalSetup]
public void Setup()
{
_student = new Student(1, "Adeola", "Aderibigbe", "11/12/2000", "jss1")
{
Classroom = new Classroom("Jss2")
};

_students = new List<Student> { _student };
}

[Benchmark]
public void MapToAnExistingObject()
{
var studentDto = new StudentDto();
var classroom = new Classroom("Jss2");
studentDto.Map(classroom);
studentDto.Map(_student);
}

[Benchmark]
public void MapToExistingObjects()
{
var studentDto = new List<StudentDto>();
studentDto.Map(_student);
}

[Benchmark]
Expand All @@ -35,18 +39,40 @@ public void MapToAnExistingObjectWithConfig()
studentDto.Map(_student, MappingService.StudentConfig());
}

[Benchmark]
public void MapToAnExistingObjectsWithConfig()
{
var studentDto = new List<StudentDto>();
studentDto.Map(_students, MappingService.StudentConfig());
}

[Benchmark]
public StudentDto MapToNewObject()
{
return _student.Map<Student, StudentDto>();
}

[Benchmark]
public List<StudentDto> MapToNewObjects()
{
return _students.Map<List<Student>, List<StudentDto>>();
}


[Benchmark]
public StudentDto MapToNewObjectWithConfig()
{
return _student.Map<Student, StudentDto>(MappingService.StudentConfig());
}

private readonly Student _student;
[Benchmark]
public List<StudentDto> MapToNewObjectsWithConfig()
{
return _students.Map<List<Student>, List<StudentDto>>(MappingService.StudentConfig());
}

private List<Student> _students;

private Student _student;
}
}
1 change: 1 addition & 0 deletions Automapify.Client/MappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public static MapifyConfiguration StudentConfig()
{
return new MapifyConfigurationBuilder<Student, StudentDto>()
.Map(d => d.Name, s => $"{s.FirstName} {s.LastName}")
.Map(d=> d.Classroom, s => s.Classroom.Room)
.Map(d=> d.Room, s => s.Classroom)
.Map(d => d.Age, s => s.DateOfBirth.ToAge())
.Map(d=>d.DOB, s => s.DateOfBirth)
Expand Down
2 changes: 1 addition & 1 deletion Automapify.Client/Models/Dtos/StudentDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class StudentDto
public DateTime DOB { get; set; }
public bool IsDeleted { get; set; }

[MapProperty(typeof(Classroom), "Room")]
[MapProperty(typeof(Classroom),"Room")]
public string Classroom { get; set; }

[MapProperty("Classroom")]
Expand Down
52 changes: 49 additions & 3 deletions Automapify.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,38 @@
using Automapify.Client.Models;
using Automapify.Client.Models.Dtos;
using Automappify.Services;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using BenchmarkDotNet.Toolchains.InProcess.NoEmit;

// Set up student and classroom data
var student = new Student(1, "Adeola", "Aderibigbe", "11/12/2000", "jss1");

var classroom = new Classroom("Jss2");

student.Classroom = classroom;

// Map a list of students to a list of student dtos using mapping configuration
var students = new List<Student>() { student };

var studentDtos = student.Map<Student, List<StudentDto>>(MappingService.StudentConfig());


// Maps data from a student object to a list of student dtos using mapping configuration
var newStudentDtos = students.Map<List<Student>, List<StudentDto>>(MappingService.StudentConfig());


// Map a student using mapping configuration
var studentDto = new StudentDto();

studentDto.Map(student, MappingService.StudentConfig());


studentDto.Map(student,MappingService.StudentConfig());
// Display data mapped for one to one mapping

studentDto.Map(classroom);
Console.WriteLine("One to one mapping values:");

studentDto.DisplayFullName();

Expand All @@ -26,5 +44,33 @@
studentDto.DisplayAge();


//var summary = BenchmarkRunner.Run(typeof(BenchmarkTest));
Console.WriteLine("\nMany to many mapping values:");

foreach (var item in studentDtos)
{
item.DisplayFullName();

item.DisplayCLassroom();

item.DisplayAge();
}


Console.WriteLine("\n One to many mapping values:");
foreach (var item in newStudentDtos)
{
item.DisplayFullName();

item.DisplayCLassroom();

item.DisplayAge();
}

var config = DefaultConfig.Instance
.AddJob(Job
.MediumRun
.WithLaunchCount(1)
.WithToolchain(InProcessEmitToolchain.Instance));

//var summary = BenchmarkRunner.Run(typeof(BenchmarkTest),config);

130 changes: 130 additions & 0 deletions Automapify.Test/MapperTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,34 @@ public void ShouldMapFromSourceObjectToNewObjectUsingAttributes()
});
}

[Test]
public void ShouldMapFromSourceObjectsToNewObjectsUsingAttributes()
{
var classroomDtos = TestClassrooms.Map<List<Classroom>, List<ClassroomDto>>();

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
Assert.IsTrue(classroomDtos.Any());
});
}

[Test]
public void ShouldMapFromSourceObjectToNewObjectsUsingAttributes()
{
var classroomDtos = TestClassroom.Map<Classroom, List<ClassroomDto>>();

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
Assert.IsTrue(classroomDtos.Any());
});
}

[Test]
public void ShouldMapFromSourceObjectToNewObjectUsingConfiguration()
{
Expand All @@ -41,6 +69,39 @@ public void ShouldMapFromSourceObjectToNewObjectUsingConfiguration()
});
}


[Test]
public void ShouldMapFromSourceObjectsToNewObjectsUsingConfiguration()
{
var classroomDtos = TestClassrooms.Map<List<Classroom>, List<ClassroomDto>>(MapifyConfiguration);

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
Assert.That(TestClassroom.ClassCode.ToString(), Is.EqualTo(classroomDtos.FirstOrDefault().Code));
Assert.That(classroomDtos.FirstOrDefault().IsActive, Is.True);
Assert.That(TestClassroom.Courses.Select(s => s.LeadLecturer.Name).Single(), Is.EqualTo(classroomDtos.FirstOrDefault().LeadLecturers.Single()));
});
}

[Test]
public void ShouldMapFromSourceObjectToNewObjectsUsingConfiguration()
{
var classroomDtos = TestClassroom.Map<Classroom, List<ClassroomDto>>(MapifyConfiguration);

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
Assert.That(TestClassroom.ClassCode.ToString(), Is.EqualTo(classroomDtos.FirstOrDefault().Code));
Assert.That(classroomDtos.FirstOrDefault().IsActive, Is.True);
Assert.That(TestClassroom.Courses.Select(s => s.LeadLecturer.Name).Single(), Is.EqualTo(classroomDtos.FirstOrDefault().LeadLecturers.Single()));
});
}

[Test]
public void ShouldMapFromSourceObjectToExistingObjectUsingAttributes()
{
Expand All @@ -56,6 +117,75 @@ public void ShouldMapFromSourceObjectToExistingObjectUsingAttributes()
});
}

[Test]
public void ShouldMapFromSourceObjectsToExistingObjectsUsingAttributes()
{
var classroomDtos = new List<ClassroomDto>();

classroomDtos.Map(TestClassrooms);

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
});
}

[Test]
public void ShouldMapFromSourceObjectToExistingObjectsUsingAttributes()
{
var classroomDtos = new List<ClassroomDto>();

classroomDtos.Map(TestClassroom);

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
});
}


[Test]
public void ShouldMapFromSourceObjectsToExistingObjectsUsingConfiguration()
{
var classroomDtos = new List<ClassroomDto>();

classroomDtos.Map(TestClassrooms, MapifyConfiguration);

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
Assert.That(TestClassroom.ClassCode.ToString(), Is.EqualTo(classroomDtos.FirstOrDefault().Code));
Assert.That(classroomDtos.FirstOrDefault().IsActive, Is.True);
Assert.That(TestClassroom.Courses.Select(s => s.LeadLecturer.Name).Single(), Is.EqualTo(classroomDtos.FirstOrDefault().LeadLecturers.Single()));
});
}


[Test]
public void ShouldMapFromSourceObjectToExistingObjectsUsingConfiguration()
{
var classroomDtos = new List<ClassroomDto>();

classroomDtos.Map(TestClassroom, MapifyConfiguration);

Assert.Multiple(() =>
{
Assert.That(TestClassroom.Name, Is.EqualTo(classroomDtos.FirstOrDefault().Name));
Assert.That(TestClassroom.NumberOfStudents, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfStudents));
Assert.That(TestClassroom.NumberOfTeachers, Is.EqualTo(classroomDtos.FirstOrDefault().NoOfLecturers));
Assert.That(TestClassroom.ClassCode.ToString(), Is.EqualTo(classroomDtos.FirstOrDefault().Code));
Assert.That(classroomDtos.FirstOrDefault().IsActive, Is.True);
Assert.That(TestClassroom.Courses.Select(s => s.LeadLecturer.Name).Single(), Is.EqualTo(classroomDtos.FirstOrDefault().LeadLecturers.Single()));
});
}


[Test]
public void ShouldMapFromSourceObjectToExistingObjectUsingConfiguration()
{
Expand Down
14 changes: 13 additions & 1 deletion Automapify.Test/TestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class TestHelper
protected virtual void Setup()
{
TestClassroom = SetUpInstance();
TestClassrooms = SetUpInstances();
SetupConfiguration();
}
private Classroom SetUpInstance()
Expand All @@ -17,7 +18,16 @@ private Classroom SetUpInstance()
.SetUpCourse("Economics100","Ec100","Adeola");
}



private List<Classroom> SetUpInstances()
{
return new List<Classroom>()
{
TestClassroom
};

}

private MapifyConfiguration SetupConfiguration()
{
MapifyConfiguration = new MapifyConfigurationBuilder<Classroom,ClassroomDto>()
Expand All @@ -32,5 +42,7 @@ private MapifyConfiguration SetupConfiguration()
}
protected MapifyConfiguration MapifyConfiguration { get; set; }
protected Classroom TestClassroom { get; set; }

protected List<Classroom> TestClassrooms { get; set; }
}
}
3 changes: 2 additions & 1 deletion Automapify/Automapify.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
Expand All @@ -7,6 +7,7 @@
<Company>Adeola Aderibigbe</Company>
<Description>This dotnet library facilitates object mappings without the need for service setup or configuration</Description>
<RepositoryUrl>https://github.com/Adeola-Aderibigbe/AutoMapify_Lib</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>.net 6.0, mapping</PackageTags>
</PropertyGroup>
</Project>
Loading
Loading