Git Product home page Git Product logo

lynxmapper's Introduction

LynxMapper

Travis NuGet license GitHub repo size in bytes

Faster Mapper (Transformator) for .NET Core 2 (.NET Standart 2)

Description

A simple library for comparing models (mapping) to .NET Core for use in real production.

Mapping is carried out with the help of special methods - transformers (transformators), which gives greater flexibility in use for solving real problems.

Install

Install LynxMapper and it's dependencies using NuGet.

Install-Package LynxMapper

All versions can be found here

Use

Use LinxMapper is very simple.

1. Create Transformator:

1.1 Create Transformator Abstractions:

using LynxMapper;

namespace Services.Abstractions.Transformators
{
    public interface IUserTransformator: ILynxTransformator
    {
        UserViewModel ToUserViewModel(Users user);
    }
}

1.2 Create Transformator Implementations:

namespace Services.Implementations.Transformators
{
    public class UserTransformator : IUserTransformator
    {
        public UserViewModel ToUserViewModel(Users user)
        {
            try
            {
                return new UserViewModel
                {
                    Name = $"{user.LastName} {user.FirstName} {user.Patronimic}",
                    Age = (int) ((DateTime.Now - user.YearOfBirth).TotalDays / 365.2425)
                };
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}

2. Register Transformators in Startup.cs:

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddTransient<IUserTransformator, UserTransformator>();
        
        var build = services.BuildServiceProvider();
        
        services.AddLynxMapper(options =>
        {
            options.RegisterTransformator<UserViewModel, Users>(build.GetService<IUserTransformator>().ToUserViewModel);
        });
    }
}

OR

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services
            .AddLynxMapperTransformators(o =>
            {
                o.Reg<IUserTransformator, UserTransformator>();
            })
            .AddLynxMapper(options =>
            {
                options.RegisterFor<UserViewModel, Users>(options.GetTransformator<IUserTransformator>().ToUserViewModel);
            });
    }
}

3. Use it!

using LynxMapper;

namespace SimpleWebApi.Controllers
{
    [Produces("application/json")]
    [Route("api/users/[action]")]
    public class UserController: Controller
    {
        private readonly IUserService _userService;
        private readonly ILynxMapper _mapper;

        public TripController(ILynxServiceProvider LynxServiceProvider, ILynxMapper mapper)
        {
            _userService = LynxServiceProvider.UserService;
            _mapper = mapper;
        }

        /// <summary>
        /// Get list all users
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Produces("application/json", Type = typeof(ICollection<UserViewModel>))]
        public async Task<IActionResult> GetAllUsers()
        {
            try
            {
                var users = await _userService.GetAllUsersAsync();

                var result = users.Select(x => _mapper.Map<UserViewModel, Users>(x)).ToList();

                return Ok(result);
            }
            catch (Exception)
            {
                return BadRequest("Users bad request.");
            }
        }
    }
}

lynxmapper's People

Contributors

gromanev avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

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.