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

Fix/many to many mapping #16

Merged
merged 2 commits into from
Aug 28, 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
1 change: 1 addition & 0 deletions Automapify.Client/BenchmarkTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ public void Setup()
{
Classroom = new Classroom("Jss2")
};
_student.AddTeacher("Adeola", "Ade");

_students = new List<Student> { _student };
}
Expand Down
12 changes: 8 additions & 4 deletions Automapify.Client/MappingService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Automapify.Client.Models;
using Automapify.Client.Models.Dtos;
using Automapify.Services;
using Automapify.Services.Extensions;

namespace Automapify.Client
{
Expand All @@ -11,11 +12,14 @@ 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.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)
.Map(d=>d.IsDeleted, s => false)
.Map(d => d.DOB, s => s.DateOfBirth)
.Map(d => d.IsDeleted, s => false)
.Map(d => d.Teachers.MapTo(s => s.Name), s => s.Teachers.MapFrom(p => $"{p.FirstName} {p.LastName}"))
.Map(d => d.TeacherNames, s => s.Teachers.MapFrom(p => $"{p.FirstName} {p.LastName}"))
.Map(d => d.Classrooms, s => s.Classrooms)
.CreateConfig();
}
}
Expand Down
4 changes: 4 additions & 0 deletions Automapify.Client/Models/Dtos/Classroom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Automapify.Client.Models.Dtos
{
public class Classroom
{
public Classroom()
{

}
public Classroom(string room)
{
Room = room;
Expand Down
6 changes: 6 additions & 0 deletions Automapify.Client/Models/Dtos/StudentDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@ public class StudentDto
[MapProperty("Classroom")]
public Classroom Room { get; set; }

public List<Classroom> Classrooms { get; set; }

public List<string> TeacherNames { get; set; }

public List<TeacherDto> Teachers { get; set; }

public void DisplayFullName()
{
Console.WriteLine(Name);
Expand Down
13 changes: 13 additions & 0 deletions Automapify.Client/Models/Dtos/TeacherDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automapify.Client.Models.Dtos
{
public class TeacherDto
{
public string Name { get; set; }
}
}
19 changes: 19 additions & 0 deletions Automapify.Client/Models/Student.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,38 @@ namespace Automapify.Client.Models
{
public class Student
{
public Student()
{

}
public Student(int id, string firstName, string lastName, string dateOfBirth, string _class)
{
Id = id;
FirstName = firstName;
LastName = lastName;
DateOfBirth = DateTime.Parse(dateOfBirth);
Class = _class;
Teachers = new List<Teacher>();
Classrooms = new List<Classroom>();
}


public void AddTeacher(string firstName, string lastName)
{
Teachers.Add(new Teacher(firstName, lastName).AssignClassroom(Classroom));
}

public void AddClassroom(string room)
{
Classrooms.Add(new Classroom(room));
}
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
public string Class { get; set; }
public Classroom Classroom { get; set; }
public List<Classroom> Classrooms { get; set; }
public List<Teacher> Teachers { get; set; }
}
}
27 changes: 27 additions & 0 deletions Automapify.Client/Models/Teacher.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Automapify.Client.Models.Dtos;

namespace Automapify.Client.Models
{
public class Teacher
{
public Teacher()
{

}
public Teacher(string firstName, string lastName)
{
FirstName = firstName;
LastName = lastName;
}

public Teacher AssignClassroom(Classroom classroom)
{
Classroom = classroom;
return this;
}

public string FirstName { get; set; }
public string LastName { get; set; }
public Classroom Classroom { get; set; }
}
}
9 changes: 6 additions & 3 deletions Automapify.Client/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@
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;
student.AddClassroom("Jss3");

student.AddTeacher("Violet", "James");

// 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());


Expand Down Expand Up @@ -65,12 +68,12 @@

item.DisplayAge();
}

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

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

4 changes: 4 additions & 0 deletions Automapify.Test/Models/Classroom.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ namespace Automapify.Test.Models
{
public class Classroom
{
public Classroom()
{

}
public Classroom(string name,int noOfStudents, int noOfTeachers,string classCode)
{
Id = 1;
Expand Down
4 changes: 4 additions & 0 deletions Automapify.Test/Models/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
public class Course
{
public Course()
{

}
public Course(string name, string courseCode)
{
Id = 1;
Expand Down
4 changes: 4 additions & 0 deletions Automapify.Test/Models/Lecturer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
{
public class Lecturer
{
public Lecturer()
{

}
public Lecturer(string name)
{
Id = 1;
Expand Down
7 changes: 4 additions & 3 deletions Automapify.Test/TestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Automapify.Services;
using Automapify.Services.Extensions;
using Automapify.Test.Models;

namespace Automapify.Test
Expand Down Expand Up @@ -34,9 +35,9 @@ private MapifyConfiguration SetupConfiguration()
.Map(d=>d.Name,s=>s.Name)
.Map(d=>d.NoOfLecturers, s=>s.NumberOfTeachers)
.Map(d=>d.NoOfStudents, s=>s.NumberOfStudents)
.Map(d=>d.LeadLecturers, s=>s.Courses.Select(s=>s.LeadLecturer.Name).ToList())
.Map(d=>d.IsActive, s=>s.NumberOfTeachers > 0 && s.NumberOfStudents > 0)
.Map(d=> d.Code, s=>s.ClassCode.ToString())
.Map(d=>d.LeadLecturers, s => s.Courses.MapFrom(s=>s.LeadLecturer.Name))
.Map(d=>d.IsActive, s => s.NumberOfTeachers > 0 && s.NumberOfStudents > 0)
.Map(d=> d.Code, s => s.ClassCode.ToString())
.CreateConfig();
return MapifyConfiguration;
}
Expand Down
19 changes: 15 additions & 4 deletions Automapify/Models/MapifyTuple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@ namespace Automapify.Models
{
public class MapifyTuple
{
public MapifyTuple(string destinationMemberName, LambdaExpression expression)
public MapifyTuple(List<string> destinationMembers, string sourceName ,LambdaExpression sourceExpression)
{
DestinationMemberName = destinationMemberName;
SourceExpression = expression.Compile();
DestinationMemberNames = destinationMembers;
SourceMemberName = sourceName;
SourceExpression = sourceExpression.Compile();
}
public string DestinationMemberName { get; set; }
public MapifyTuple(string destinationMember, string sourceName, LambdaExpression sourceExpression)
{
DestinationMemberNames.Add(destinationMember);
SourceMemberName = sourceName;
SourceExpression = sourceExpression.Compile();
}


public List<string> DestinationMemberNames { get; set; } = new List<string>();

public string SourceMemberName { get; set; }
public Delegate SourceExpression { get; set; }
}
}
19 changes: 19 additions & 0 deletions Automapify/Models/Property.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Automapify.Models
{
public class Property
{
public Property(string propertyName)

Check warning on line 11 in Automapify/Models/Property.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'SourceName' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
{
PropertyName = propertyName;
}

public string SourceName { get; set; }
public string PropertyName { get; set; }
}
}
18 changes: 18 additions & 0 deletions Automapify/Services/Extensions/MappingExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Linq.Expressions;

namespace Automapify.Services.Extensions
{
public static class MappingExtension
{
public static Expression<Func<TSource, SourceMember>> MapFrom<TSource, SourceMember>(this IEnumerable<TSource> source, Expression<Func<TSource, SourceMember>> sourcePredicate)
{
return sourcePredicate;
}

public static Expression<Func<TSource,SourceMember>> MapTo<TSource, SourceMember>(this IEnumerable<TSource> destination, Expression<Func<TSource, SourceMember>> sourcePredicate)
{
return sourcePredicate;
}

}
}
8 changes: 8 additions & 0 deletions Automapify/Services/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public static bool NotContains(this string source, string parameter)
return !source.Contains(parameter);
}

public static bool Contains(this IEnumerable<string> source, string firstParameter, string secondParameter)
{
if (source.Count() == 0)
return true;

return source.Contains(firstParameter) && source.Contains(secondParameter);
}

public static bool NotStartsWith(this string source, string parameter)
{
if (string.IsNullOrEmpty(source))
Expand Down
Loading
Loading