question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Automatic extension method generation for easy updating of existing class instances

See original GitHub issue

Proposal

I’m loving the library, but I think it would be very helpful to have the source generator automatically create extension methods for the updating of existing class instances. This would allow classes to become more “record-like”, allowing updates in-place.

I’m proposing here that the “lifting” method to be called BuildNew(), but I dont think this is ideal, and I think a better name should be thought of. I would call it With(), but that conflicts with the current WithX naming convention of the existing builders.

Example API:

[AutoGenerateBuilder(generateSetters: true)]
public class User
{
    public string Name { get; set; }
    public string Gender { get; set; }
}

...

var user = await dbContext.Users.FirstAsync();

user.BuildNew() // `BuildNew` originates from an auto-generated extension method class in UserBuilder.g.cs. Lifts user into a UserBuilder
    .WithName("New Name") // usual updates happen here
    .Build(); // Updates the object in-place.

// OPTIONALLY if generateSetter == True

user.SetName("Newer Name"); // Sets the name

await dbContext.SaveChangesAsync(); // user's Name property is updated.

// also allow for operations on items in a collection
var users = dbContext.Users.Where(x => x.Gender == "Male").ToListAsync();

users.SetGender("Female") // Updates all elements in-place. Could also use `BuildNew()` syntax.

dbContext.SaveChangesAsync();

I feel like this is a natural continuation of the current library. It appears like you’re supposed to be able to use UsingInstance for this purpose to update existing instances in-place, but I can’t get it to work. This would also bring existing non-record classes to be more “record like”.

Issue Analytics

  • State:closed
  • Created 6 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
StefHcommented, Apr 8, 2023

Official NuGet will be released shorty.

1reaction
Meigs2commented, Apr 8, 2023

@StefH I’ve given it a test, looks great! Appreciate it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - How to override an existing extension method
Extension methods cannot be overridden since they are not instance methods and they are not virtual. The compiler will complain if you import ......
Read more >
Extension Methods - C# Programming Guide
Extension methods in C# enable you to add methods to existing types without creating a new derived type, recompiling, or otherwise modifying ...
Read more >
Customizing Existing Classes
The only way to add a traditional property—backed by a new instance variable—to an existing class is to use a class extension, as...
Read more >
C# Extension Methods (How It Works For Developers)
Extension methods are a powerful feature in C# that allows you to add new functionality to existing types without modifying their source ...
Read more >
Extension Methods - Essential C#
With extension methods, it is now possible to add “instance methods” to any class, including classes outside of your assembly. The resultant CIL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found