POCOMapper 2.6.1
dotnet add package POCOMapper --version 2.6.1
NuGet\Install-Package POCOMapper -Version 2.6.1
<PackageReference Include="POCOMapper" Version="2.6.1" />
<PackageVersion Include="POCOMapper" Version="2.6.1" />
<PackageReference Include="POCOMapper" />
paket add POCOMapper --version 2.6.1
#r "nuget: POCOMapper, 2.6.1"
#addin nuget:?package=POCOMapper&version=2.6.1
#tool nuget:?package=POCOMapper&version=2.6.1
POCO.Mapper
An alternative "Plain Old C# Objects" mapper with minimal configuration required.
POCO stands for "Plain Old C# Object" or "Plain Old CLR Object", depending on who you ask. This library is a custom mapper for POCOs (map values of identical properties from one POCO to another POCO). Minimal configuration is needed.
Why?
Click me to learn more
How-To
NuGet
Add as reference in your class
using POCO.Mapper;
using POCO.Mapper.Extension;
Let's say you have a different POCO for database models and for view-models. MappedTo("")
attribute is required to map the property to a target POCO.
Note: *As of v2.0.0, you'll have an option to use extension methods or the IMapper interface. For extension methods, it is required that a POCO inherits from POCO.Mapper.Extension.ModelMap
.
/// <summary>
/// POCO entity based on actual database table
/// </summary>
public class Employee : ModelMap
{
[MappedTo("Id")]
public long EmployeeId { get; set; }
public string FirstName { get; set; } // Will not be mapped
public string Lastname { get; set; } // Will not be mapped
[MappedTo("BDay")]
[UseFormat("yyyy-MMM-dd")]
public DateTime BirthDate { get; set; }
[MappedTo("EmployeeName")]
public string FullName
{
get { return Lastname + ", " + FirstName; }
}
[MappedTo("WorkView")]
public Work Work { get; set; } = new Work();
}
public class Work : ModelMap
{
public Guid WorkId { get; set; } // Will not be mapped
[MappedTo("JobTitle")]
public string Title { get; set; }
[MappedTo("WorkAddress")]
[IgnoreIf(typeof(AnotherObject))] // This property will not get mapped if the target type is AnotherObject
public string Address { get; set; }
}
/// <summary>
/// POCO view-model entity which will be consumed outside of your data layer
/// </summary>
public class EmployeeViewModel
{
public long Id { get; set; }
public string EmployeeName { get; set; }
public string FirstName { get; set; } // Will be ignored
public string Lastname { get; set; } // Will be ignored
public string BDay { get; set; }
public WorkViewModel WorkView { get; set; } = new WorkViewModel();
}
public class WorkViewModel
{
public string JobTitle { get; set; }
public string WorkAddress { get; set; }
}
Using IMapper
Interface
void Map()
{
// Example Data Only
using Employee _employee = new Employee
{
EmployeeId = 1,
FirstName = "Nor",
Lastname = "Gelera",
BirthDate = DateTime.Now,
Work = new Work
{
WorkId = Guid.NewGuid(),
Title = ".NET Developer",
Address = "Cebu"
}
};
// Initialize Mapper
IMapper<EmployeeViewModel, Employee> _mapper = new ModelMapper<EmployeeViewModel, Employee>();
// Map to view-model
EmployeeViewModel _employeeViewModel = _mapper.from(_employee);
}
Using Extension Methods
void Map()
{
// Example Data Only
using Employee _employee = new Employee
{
EmployeeId = 1,
FirstName = "Nor",
Lastname = "Gelera",
Work = new Work
{
WorkId = Guid.NewGuid(),
Title = ".NET Developer",
Address = "Cebu"
}
};
// Map to view-model
EmployeeViewModel _employeeViewModel = _employee.MapTo<EmployeeViewModel>();
}
The result would be an instance of EmployeeViewModel
with values for Id
, EmployeeName
, BDay
from Employee
entity. FirstName
and LastName
properties of Employee
entity will be ignored by POCOMapper and will not be mapped to EmployeeViewModel
. Values for Work
property of Employee
will also be mapped to WorkViewModel
.
Note: As of the current version, POCO.Mapper
also supports mapping of values for IList<>
, List<>
and Array[]
properties (interchangeably).
Contributors
Install the following to get started
IDE
Extensions
Frameworks
Do you want to contribute? Send me an email or DM me in twitter.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.1 is compatible. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.1
- 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.
Version | Downloads | Last updated |
---|---|---|
2.6.1 | 1,117 | 1/22/2023 |
2.6.0 | 3,311 | 3/11/2022 |
2.5.1 | 1,916 | 10/26/2021 |
2.5.0 | 514 | 10/23/2021 |
2.4.0 | 4,710 | 11/12/2020 |
2.3.0 | 1,780 | 6/18/2020 |
2.2.1 | 2,486 | 12/7/2019 |
2.2.0 | 951 | 9/5/2019 |
2.1.0 | 843 | 5/9/2019 |
2.0.0 | 740 | 4/12/2019 |
1.2.2 | 741 | 4/9/2019 |
1.2.1 | 752 | 4/7/2019 |
1.2.0 | 778 | 4/2/2019 |
1.1.1 | 718 | 3/10/2019 |
1.1.0 | 818 | 2/3/2019 |
1.0.1 | 784 | 2/3/2019 |
1.0.0 | 1,091 | 1/8/2019 |
View release on https://github.com/kuromukira/poco.mapper/releases/tag/v2.6.1