anakic / Jot

Jot is a library for persisting and applying .NET application state.
http://www.codeproject.com/Articles/475498/Easier-NET-settings
MIT License
633 stars 56 forks source link

[REQUEST] Add overload for AddProperties() method #23

Closed ElektroStudios closed 5 years ago

ElektroStudios commented 5 years ago

Hi.

Maybe could you consider to add an overload for the AddProperties method that can take as parameter an Expression of an array of properties instead of using 'params' to take a variable number of expressions?.


See. In the example of the Readme.md written in C#, the syntax is so simple with the current method parameters but a little bit repetitive:

AddProperties<Window>(w => w.Height, w => w.Width, w => w.Top, w => w.Left, w => w.WindowState);

And for VB.NET usage this takes the need to write more repetitive code:

AddProperties(Of Window)(Function(w) x.Height, Function(w) w.Width, Function(w) w.Top, Function(w) w.Left, Function(w) w.WindowState)

Then, by adding the requested overload we could simplify the syntax for both C# and VB.NET to something like this:

AddProperties<Window>((w) => new[] {w.Height, w.Width, w.Top, w.Left, w.WindowState}); and: AddProperties(Of Window)(Function(w) {w.Height, w.Width, w.Top, w.Left, w.WindowState})

Thanks for read.

anakic commented 5 years ago

Interesting idea. Perhaps even an expression that returns a dynamic object like this: AddProperties<Window>(w => { w.Height, w.Width, w.Top, w.Left })

Might also be good to make the config object generic, so that the generic parameter to AddProperties can be inferred, in which case the call would be simpler still:

AddProperties(w => { w.Height, w.Width, w.Top, w.Left })

The generic type parameter can be inferred since the config object would be of type TrackingConfiguration

I'll take a look at this when I get a bit of time, but if you implement any improvements please feel free to make a pull request.

ElektroStudios commented 5 years ago

Done. Maybe the code block of "GetPropertyNamesFromExpression" could need a refactorization from your side to avoid repeating code (I commented it in my pull request) and maybe the approach of including that "GetPropertyNamesFromExpression" function at all to parse an array could be avoided in some way, but what I added and did was just the initial idea. Thanks.

anakic commented 5 years ago

Added in v2.0, so closing this issue. You can now specify properties to track like so: cfg.Properties(w => new { w.Height, w.Width }). Not sure what the corresponding VB.NET syntax is.