ModelMapperGenerator 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package ModelMapperGenerator --version 1.0.1
                    
NuGet\Install-Package ModelMapperGenerator -Version 1.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ModelMapperGenerator" Version="1.0.1">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ModelMapperGenerator" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ModelMapperGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ModelMapperGenerator --version 1.0.1
                    
#r "nuget: ModelMapperGenerator, 1.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ModelMapperGenerator@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ModelMapperGenerator&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ModelMapperGenerator&version=1.0.1
                    
Install as a Cake Tool

ModelMapperGenerator

Build Status

This source generator generates enums and classes based on existing enums and classes.
It contains built in analyzers which ensure the correct usage of the generator.

Usage example

Types from which models and mappers should be generated

namespace SomeNamespace
{
    public enum Vehicle
    {
        Car,
        Boat,
        Plane
    }

    public class Person
    {
        public string FirstName { get; set; }
        public int Age { get; set; }
    }
}

Hook class

Uses ModelGenerationTargetAttribute, coming from ModelMapperGeneration.Attributes nuget package. Generated mappers and models will be placed in this namespace

using ModelMapperGenerator.Attributes;

namespace TargetNamespace
{
    [ModelGenerationTarget(Types = new Type[]
    {
        typeof(SomeNamespace.Vehicle),
        typeof(SomeNamespace.Person)
    })]
    public class Hook { }
}

The following code will be generated

For Person class
using SomeNamespace;

namespace TargetNamespace
{
    public static class PersonMapper
    {
        public static PersonModel ToModel(this Person value)
        {
            PersonModel model = new PersonModel()
            {
                FirstName = value.FirstName,
                Age = value.Age,

            };

            return model;
        }

        public static Person ToDomain(this PersonModel value)
        {
            Person domain = new Person()
            {
                FirstName = value.FirstName,
                Age = value.Age,

            };

            return domain;
        }
    }
}
namespace TargetNamespace
{
    public class PersonModel
    {
        public string FirstName { get; set; }
        public int Age { get; set; }

    }
}
For Vehicle enum
using System;
using SomeNamespace;

namespace TargetNamespace
{
    public static class VehicleMapper
    {
        public static VehicleModel ToModel(this Vehicle value)
        {
            return value switch
            {
                Vehicle.Car => VehicleModel.Car,
                Vehicle.Boat => VehicleModel.Boat,
                Vehicle.Plane => VehicleModel.Plane,
                _ => throw new ArgumentOutOfRangeException("Unknown enum value")
            };
        }

        public static Vehicle ToDomain(this VehicleModel value)
        {
            return value switch
            {
                VehicleModel.Car => Vehicle.Car,
                VehicleModel.Boat => Vehicle.Boat,
                VehicleModel.Plane => Vehicle.Plane,
                _ => throw new ArgumentOutOfRangeException("Unknown enum value")
            };
        }
    }
}
namespace TargetNamespace
{
    public enum VehicleModel
    {
        Car = 0,
        Boat = 1,
        Plane = 2,

    }
}

Complex classes

If a class which is a model generation source, has publicly accessible properties of types which themselves are model generation sources, then the generated model will also include models of those classes:

Classes and hook

namespace SomeNamespace
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public Address Address { get; set; }
        public Grade Grade { get; set; }
    }

    // Included as a type to generate model from
    public class Address
    {
        public string Street { get; set; }
        public string City { get; set; }
    }

    // NOT included as type to generate model from
    public class Grade
    {
        public string Name { get; set; }
        public int Value { get; set; }
    }
}

using ModelMapperGenerator.Attributes;

namespace TargetNamespace
{
    [ModelGenerationTarget(Types = new Type[]
    {
        typeof(SomeNamespace.Person),
        typeof(SomeNamespace.Address),
    })]
    public class Hook { }
}

Generated Code

Person class only, Address class omitted for brevity

namespace TargetNamespace
{
    public class PersonModel
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public TargetNamespace.AddressModel Address { get; set; } // AddressModel is used instead of an Address
        public SomeNamespace.Grade Grade { get; set; } // Grade is used - Grade type is not a source of generation

    }
}

using SomeNamespace;

namespace TargetNamespace
{
    public static class PersonMapper
    {
        public static PersonModel ToModel(this Person value)
        {
            PersonModel model = new PersonModel()
            {
                Name = value.Name,
                Age = value.Age,
                Address = value.Address.ToModel(), // Address to AddressModel
                Grade = value.Grade, // Grade

            };

            return model;
        }

        public static Person ToDomain(this PersonModel value)
        {
            Person domain = new Person()
            {
                Name = value.Name,
                Age = value.Age,
                Address = value.Address.ToDomain(), // AddressModel to Address
                Grade = value.Grade, // Grade

            };

            return domain;
        }
    }
}

Analyzers

The package comes with built in analyzers which guard against the following usage errors:

  • Placing more than one model generation hook in a single namespace
// Error - more than one hook in a single namespace
using ModelMapperGenerator.Attributes;

namespace TargetNamespace
{
    [ModelGenerationTarget(Types = new Type[]
    {
        typeof(SomeNamespace.Vehicle),
        typeof(SomeNamespace.Person)
    })]
    public class FirstHook { }

    [ModelGenerationTarget(Types = new Type[]
    {
        typeof(SomeNamespace.Vehicle),
        typeof(SomeNamespace.Person)
    })]
    public class SecondHook { }
}
  • Using a record as a type to generate code from
namespace SomeNamespace
{
    public record Todo(string Name, bool Completed);
}

// Warning - records are ignored by source generator
using ModelMapperGenerator.Attributes;

namespace TargetNamespace
{
    [ModelGenerationTarget(Types = new Type[]
    {
        typeof(SomeNamespace.Todo)
    })]
    public class Hook { }
}

  • Placing types with the same name in the same generation hook
namespace SomeNamespace
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

namespace AnotherNamespace
{
    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

// Error - multiple types with the same name placed in single hook
using ModelMapperGenerator.Attributes;

namespace TargetNamespace
{
    [ModelGenerationTarget(Types = new Type[]
    {
        typeof(SomeNamespace.Person),
        typeof(AnotherNamespace.Person),
    })]
    public class Hook { }
}
There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

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.0.0 242 5/4/2024
1.0.1 219 4/25/2024