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 immutable types #84

Open
FrankHileman opened this issue May 19, 2022 · 8 comments · May be fixed by #85
Open

Support for immutable types #84

FrankHileman opened this issue May 19, 2022 · 8 comments · May be fixed by #85

Comments

@FrankHileman
Copy link

I realize this is out of scope in the original design, but do you have any plans to add support for a custom mapping to an immutable type? Specifically, to map to a class that has read-only properties, with the property values passed in the constructor. This means the mapping must create the instance. Thank you.

@bytefish
Copy link
Collaborator

Do you have an API in mind, so I can get an idea? Then we could see, what’s possible and eventually make a new major release and changes to the existing API. 🙂

@FrankHileman
Copy link
Author

The only requirement for an API is that the mapping class (it must be custom) would have to call the constructor explicitly. If you use reflection, there is a complicated way to do that, but I think the simplest API would be one that allows the mapping class to extract the column values, then call the constructor, and return the instance to your infrastructure. You already have a way to extract column values (strong typing there would be bonus).

@FrankHileman
Copy link
Author

Currently the CsvMapping class requires a parameter less constructor.

@FrankHileman
Copy link
Author

You could make an equivalent to CsvMapping specifically for immutable types.

@FrankHileman
Copy link
Author

If you want to simply extend the MapUsing API, you could have a delegate parameter that does not take the instantiated object as input, only the values. Then a null return value means failure, or if you allow value types to be constructed, a Result structure is returned, and your base class (CsvImmutableMapping) would need to have a MapUsing method that accepts the correct delegate type (i.e., the delegate is strongly typed by its output).

@bytefish
Copy link
Collaborator

Sounds good! I think using records also makes a lot of sense and updating the API for it. But I cannot really promise, when I’ll be able to look at it. Any help is appreciated. 👍

@JoaoSeverino JoaoSeverino linked a pull request May 21, 2022 that will close this issue
@JoaoSeverino
Copy link

I had a try at this, and I believe both MapUsing and a similar syntax to the existing MapProperty methods can be interesting.
Feel free to have a look at PR #85 and see if it fulfills your needs.

@bytefish
Copy link
Collaborator

A great PR! Please give me some time to take a look at it. :)

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

Successfully merging a pull request may close this issue.

3 participants