Faisal.Map.Object
1.0.4
dotnet add package Faisal.Map.Object --version 1.0.4
NuGet\Install-Package Faisal.Map.Object -Version 1.0.4
<PackageReference Include="Faisal.Map.Object" Version="1.0.4" />
<PackageVersion Include="Faisal.Map.Object" Version="1.0.4" />
<PackageReference Include="Faisal.Map.Object" />
paket add Faisal.Map.Object --version 1.0.4
#r "nuget: Faisal.Map.Object, 1.0.4"
#addin nuget:?package=Faisal.Map.Object&version=1.0.4
#tool nuget:?package=Faisal.Map.Object&version=1.0.4
How to use:
For simple class Object: Students student = _dbContext.Students.Where(a=>a.ID == 1); StudentsViewModel records = student.MapProperties<Students>();
For List class Object List<StudentsViewModel> studentsVM = new List<StudentsViewModel>(); List<Students> students = _dbContext.Students.ToList(); students.MapListProperties<Students, StudentsViewModel>(studentsVM);
Example scenario:
Let me start with one scenario. Just imagine we have an object which has lots of public properties and we need to use only some properties of it for the user profile method. For example, we have a class Teacher_Interview and another class named Teacher_College.
Public class Teacher_Interview { Public int UID {get; set;} Public string Name {get; set;} Public string Email {get; set;} Public string Subject {get; set;} }
Public class Teacher_College { Public int TID {get; set;} Public string Name {get; set;} Public string Email {get; set;} }
Here, we have written both the classes. As we can see, we have a Teacher_Interview class that contains its public properties. Now, we need to use the Teacher_college class object and want the same values from the Teacher_Interview class object. For example, we wrote Teacher_Interview object.
Teacher_Interview ti = new Teacher_Interview() { UID = 101 Name = “Faisal Pathan” Email = faisalmpathan@gmail.com Subject = “.NET” } Now, in a normal case, we copy/use the Teacher_Interview object value in Teacher_College like below.
Teacher_College tc = new Teacher_College() { TID = ti.UID, Name = ti.Name, Email = ti.Email } Just think about what happens if we have 40-50 or more than 50 properties. In this case, we have to write the same line 40 to 50 times which is definitely a time-consuming and boring thing. It is also possible to forget to assign some of the properties which will result in an error. So, what is the solution for that? We can use System.reflection namespace and classes. Here, I wrote a generic method, namely “MatchAndMap” which copies the same name properties from Teacher_Interview class object to the Teacher_College class object.
Var records = (Teacher_Interview_class_object).MapProperties<Teacher_College_class_object>();
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net40 is compatible. net403 was computed. net45 was computed. net451 was computed. net452 was computed. net46 was computed. net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
This package has no dependencies.
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Targeting multiple framework