Skip to content

[Mapster Tool] Add support for global:: to disambiguate type names in generated mappers #1020

Description

@BladeWise

In some cases it could be useful to generate sources where type names are fully qualified with global:: prefix.

If the mapper class is generated in a namespace that partially collides with the one where models are defined, compilation fails.
An example:

namespace Company.Models
{
    public class MyModel
    {
    }
}

namespace ThirdParty.Models
{
    public class OtherModel
    {
    }
}

namespace Prefix.Company.Mappers
{
    using ThirdParty.Models;
    using Company.Models;

    public partial class Mapper
    {
        MyModel Map(OtherModel model);
    }
}

In this case the compiler will trigger a CS0234
The type or namespace 'Models' does not exist in the namespace 'Prefix.Company' (are you missing an assembly reference?)
due to the standard namespace resolution strategy of the compiler.
Even qualifying the name like this

namespace Prefix.Company.Mappers
{
    using ThirdParty.Models;

    public partial class Mapper
    {
        Company.Models.MyModel Map(OtherModel model);
    }
}

does not fix the error, and the only proper fix is to use the global:: qualifier

namespace Prefix.Company.Mappers
{
    using ThirdParty.Models;
    // The following is equivalent to inline global qualificatio
    // using global::Company.Models;

    public partial class Mapper
    {
        global::Company.Models.MyModel Map(OtherModel model);
    }
}

The Mapster tool allows to generate mappers with a fully qualified name, but it does not prefix types with global:: (neither provides an option to do so).
It would be nice if such feature was added.
I am willing to provide a PR eventually.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions