Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for classes with private constructor #625

Open
bahmanbs opened this issue Dec 18, 2024 · 0 comments
Open

Support for classes with private constructor #625

bahmanbs opened this issue Dec 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@bahmanbs
Copy link

bahmanbs commented Dec 18, 2024

Is your feature request related to a problem? Please describe.

No response

Describe the solution you'd like

Automapper must accept classes with public constructors, or classes with a private constructor and a specific factory method name determined by Automapper to call and instantiate a new instance and start mapping.

As desribed in the docs we can use ConstructUsing method to call static factory methods but class signature with a private constructor causes an error.

this is an example:

class Source
{
    constructor (private _firstName: string, private _lastName: string)
    { }

    public get firstName()
    {
        return this._firstName;
    }

    public get lastName()
    {
        return this._lastName;
    }
}

class Destination
{
    private _fullName: string;

    private constructor ();
    private constructor (_fullName: string);
    private constructor (...args: any[])
    {
        if (typeof args[ 0 ] == "string")
        {
            this._fullName = args[ 0 ];
        }
    }

    public get fullName()
    {
        return this._fullName;
    }

    public static createForMapping()
    {
        return new Destination();
    }
}

const dataSource = new Source("John", "Doe");

const mapper = createMapper({ strategyInitializer: classes() })

createMap(mapper, Source, Destination,
    forMember((destination) => (destination.fullName), mapFrom((source) => (`${source.firstName} ${source.lastName}`)))
)

image

Describe alternatives you've considered

No response

Additional context

No response

@bahmanbs bahmanbs added the enhancement New feature or request label Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant