Giter Site home page Giter Site logo

manualmappingguard's Introduction

ManualMappingGuard

Nuget

Applications are often required to map data between different models, e.g. from a domain model to a view model or a DTO. There are libraries like AutoMapper for such jobs, however the use of reflection has some drawbacks: navigating and debugging mapping code becomes much harder.

Writing manual mapping code on the other hand is error prone. For instance, it is easy to miss some target properties or source enum values, especially when code changes over time.

ManualMappingGuard therefore provides a Roslyn analyzer which aids writing manual mapping code. It features the following diagnostics:

  • Unmapped property detection

Installation

You need to add the NuGet package ManualMappingGuard to the project containing the mapping code. Please note that only the new SDK based projects are supported.

Usage

Unmapped property detection

Declare a method as a mapping method by decorating it with MappingMethodAttribute. The method must either return a value or own a single parameter decorated with MappingTargetAttribute.

The analyzer will report errors for any property with a public setter that is not assigned within the mapping method.

You can exclude properties by adding one or more instances of UnmappedPropertiesAttribute to the mapping method and passing the property names to it.

In the example below the analyzer will report that the method Map does not map the property Person.LastName. The property Person.Id is not reported because it is declared as unmapped property.

using ManualMappingGuard;

public class Person
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string FullName => $"{FirstName} {LastName}";
}

public class PersonModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public static class Mapper
{
    [MappingMethod]
    [UnmappedProperties(nameof(Person.Id))]
    public static Person Map(PersonModel model)
    {
        return new Person { FirstName = model.FirstName };
    }
}

/*
  MSBuild Output:

  Program.cs(19, 4): [MMG1001] Property LastName is not mapped.
*/

Limitations

Please note that the analyzer does not perform any data flow analysis and is satisfied as soon as there is at least one assignment expression for all properties with public setters.

For instance, you can trick the analyzer by the following mapping method. There are assignments to all properties, however the method returns an uninitialized object.

[MappingMethod]
public static PersonModel MapToModel(Person person)
{
    var notUsed = new PersonModel
    {
        FirstName = person.FirstName,
        LastName = person.LastName
    };

    return new PersonModel();
}

manualmappingguard's People

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

manualmappingguard's Issues

[Suggestion] Allow Custom UnmappedProperties-Attribute

Because it is not possible to create a constant string array, it is not possible to simplify some common ignored mappings (we have some methods that map to entity-framework entities and should ignore some fields that are set in the context).

One solution would be to be able to inherit the attribute (IgnoreProperties) and specify these common mappings there, so the final custom Attribute would like this:

    public class IgnoreFooAndBarPropertiesAttribute : UnmappedPropertiesAttribute
    {
        public IgnoreFooAndBarPropertiesAttribute() : base("Foo", "_Bar")
        {
        }
    }

and used like this

        [MappingMethod]
        [IgnoreFooAndBarProperties]
        protected override void MapInternal(ADto source, [MappingTarget] ABusinessObject dest)
        {
        }

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.