Coder Social home page Coder Social logo

kuromukira / poco.mapper Goto Github PK

View Code? Open in Web Editor NEW
8.0 1.0 0.0 146 KB

An alternative "Plain Old C# Objects" mapper with minimal configuration required.

License: GNU General Public License v3.0

C# 100.00%
poco mapper nuget entity custom-mapper

poco.mapper's Introduction

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.

Nuget Nuget GitHub

Why?

Click me to learn more

How-To

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

  1. Visual Studio Code
  2. Visual Studio Community

Extensions

  1. C# Language Extension for VSCode

Frameworks

  1. .NET Core

Do you want to contribute? Send me an email or DM me in twitter.

poco.mapper's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar kuromukira avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

poco.mapper's Issues

Update README

Description

  1. Update the README file to include the new features of the nuget package
  2. Remove the unused build badge

Add option to dynamically exclude fields during map method invoke

Use-Case

There might be instances during mapping to view model that you want to ignore a property that contains a large list. This is not supported in the current version and the workaround for this is to remove the MappedTo attribute of the property, but this will cause the property to never be mapped to another model. This might lead to a more complex model design which is not the goal of the project.


Proposed Solution

Additional parameter in

Model model = new Model();
using ModelVM modelVM = model.MapTo<ModelVM>(/*here*/);

to accept list of ignored target properties.

Add support for structs and C# 9 records types

Use-Case

In theory, both structs and records are already supported by the mapping function. But the project's coverage for the two types are limited in terms of unit testing.


Proposed Solution

  1. Create additional unit tests to cover structs
  2. Create additional unit tests to cover records
  3. Make adjustments to the mapping function if needed to support both

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.