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

Use ClassMap to create constructor parameter mapping #955

Closed
JoshClose opened this issue Mar 15, 2018 · 8 comments
Closed

Use ClassMap to create constructor parameter mapping #955

JoshClose opened this issue Mar 15, 2018 · 8 comments
Labels
Projects

Comments

@JoshClose
Copy link
Owner

JoshClose commented Mar 15, 2018

Look into what sort of work is required to do this.

Maybe everything can stay the same and there is a map type or something that can be changed.

Type = ClassMapTypes.ConstructorParameters;
Map( m => m.Id );
Map( m => m.Name );

#1484

@gojanpaolo
Copy link

Would it be worthwhile to look at how EF Core does it?

https://docs.microsoft.com/en-us/ef/core/modeling/constructors

@JoshClose
Copy link
Owner Author

I like that convention.

When EF Core creates instances of these types, such as for the results of a query, it will first call the default parameterless constructor and then set each property to the value from the database. However, if EF Core finds a parameterized constructor with parameter names and types that match those of mapped properties, then it will instead call the parameterized constructor with values for those properties and will not set each property explicitly.

@philwindsor
Copy link

any update on this, really would be a handy feature?

@JoshClose
Copy link
Owner Author

It's still planned. There are a few things that have higher priority though and I don't have a lot of time at the moment.

@philwindsor
Copy link

philwindsor commented Mar 25, 2020

I'm having massive issues with this at the moment. It may warrant a separate support ticket.

I have raised a separate ticket #1482

@JoshClose
Copy link
Owner Author

This was done as part of another feature several months ago.

Issues automation moved this from To do to Done Apr 20, 2021
@MurrayLong
Copy link

This was done as part of another feature several months ago.

Which feature? How is it used?

@JoshClose
Copy link
Owner Author

You use Parameter instead of Map.

void Main()
{
    var s = new StringBuilder();
    s.Append("Id,Name\r\n");
    s.Append("1,one\r\n");
    var config = new CsvConfiguration(CultureInfo.InvariantCulture)
    {
    };
    using (var reader = new StringReader(s.ToString()))
    using (var csv = new CsvReader(reader, config))
    {
        csv.Context.RegisterClassMap<FooMap>();
        csv.GetRecords<Foo>().ToList().Dump();
    }
}

private class Foo
{
    public int Id { get; private set; }
    public string Name { get; private set; }
    
    public Foo(int theId, string theName)
    {
        Id = theId;
        Name = theName;
    }
}

private class FooMap : ClassMap<Foo>
{
    public FooMap()    
    {
        Parameter("theId").Name("Id");
        Parameter("theName").Name("Name");
    }
}

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

No branches or pull requests

4 participants