MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.3k stars 328 forks source link

Allow to generate mapper between two classes outside of the registering assembly using IRegistry class #380

Open SitamMatt opened 2 years ago

SitamMatt commented 2 years ago

Hi, I tried to generate an extension mapper for two classes defined in external assemblies (in attached repo, it's dto and model projects). Running mapster tool does nothing in that case. However everyting works fine if I register classes inside current assembly. Here is the link to the example project showing the problem - https://github.com/SitamMatt/Mapster-external-classes-registring-example Run:

mapster extension -a bin\Debug\net5.0\mappers.dll

inside mappers project

From what I've found out studying the source code it's because mapster tool cannot access those assemblies. My workaround was to modify the tool to pass as arguments multiple assembly paths - including those which contains classes to be mapped.

So my question is - Is there any other official way to achieve mappers generation for external classes or could you be intersted to let me create the PR for my workaround ?

suntereo commented 2 years ago

I have the same issue and am interested in your workaround :)

Alexandr-Pletnev commented 7 months ago

Hi,

My solution has several projects:

Contracts and Domain projects should be clean and pure as much as possible.

I want to generate DTOs from Domain and put to Contract, and i want to use fluent API for registration in Application project.

public class MapsterRegister : ICodeGenerationRegister
{
    public void Register(CodeGenerationConfig config)
    {
        config.AdaptTo("[name]Dto")
            .ForType<ProjectDocument>();

        config.GenerateMapper("[name]Mapper")
            .ForType<ProjectDocument>();
    }
}

How can i do this trick?

image