Incremental primary constructor on record #7424
Replies: 3 comments 51 replies
-
Sorry, it's not clear to me what is annoying here, it what you're asking for help on. Can you clarify? Thanks! |
Beta Was this translation helpful? Give feedback.
-
To clarify what I mean, what I used to do before records: // this is defined somewhere outside the UI
class Person
{
public string Name { get; set; } // etc.
}
class PersonsLogic
{
public Person GetPerson(int id) { ... }
}
// this is defined in the UI (like the web project)
class PersonModel : Person
{
public PersonModel(Person source, string someDisplayProp)
{
AutoMapper.Copy(source, this);
this.SomeDisplayProp = someDisplayProp;
}
public string SomeDisplayProp { get; }
}
// then:
var person = this._businessLogic.GetPerson(123);
var personModel = new PersonModel(person, this.GetDisplayProp(person));
return this.View(personModel); // or whatever It would be cool to replace the above with: // this is defined somewhere outside the UI
record Person(string Name /* etc. */);
class PersonsLogic
{
public Person GetPerson(int id) { ... }
}
// this is defined in the UI (like the web project)
record PersonModel(base Person source, string SomeDisplayProp) : Person(source);
// then:
var person = this._businessLogic.GetPerson(123);
var personModel = new PersonModel(person, this.GetDisplayProp(person));
return this.View(personModel); // or whatever |
Beta Was this translation helpful? Give feedback.
-
The [ConvertFrom<Person>]
public record PersonModel( ... ) The source generator then examines the two types, and fills out the method with simple assignments where the they have identical properties. Extra properties in the destination are added to the method's parameter list. |
Beta Was this translation helpful? Give feedback.
-
Is it possible without
original
property being synthesized?Beta Was this translation helpful? Give feedback.
All reactions