Actually, RaiseAndSet is primarily a helper function - there is still
the RaisePropertyChanged method that simply takes the parameter name
as a string - if performance is a concern for certain properties,
writing the property setter by-hand is probably the fastest way
--
Paul Betts <
pa...@paulbetts.org>
On Sep 26, 2:56 pm, Oren Novotny <
o...@novotny.org> wrote:
> What about something even simple and more performant-- using a ref parameter?
>
> You'd call it somethinglike RaiseAndSet(x=> x.MyProp, ref _myPropStorage, value)
>
> Then it won't matter what the backing field is named.
>
> Also, onething to keep in mind is that the expressiontree parsing for getting the string value out of x => x.MyProp is very useful, but for properties that change very fast, there's a performance bottleneck in the Expression internals. I'd recommend having an overload that takes a string forthose cases where a profilerdeems a string faster than an expression. If aref parameteris used for the storage, then that works fine as well.
>
> Oren
>
>
>
>
>
>
>
> > -----Original Message-----
> >From:
reacti...@googlegroups.com
> > [mailto:
reacti...@googlegroups.com] On Behalf Of Jörg B.
> > Sent: Saturday, September 25, 2010 8:31 AM
> > To: ReactiveXaml mailing list
> > Subject: Re: Strongly typed INPC
>
> > Paul,
>
> > good idea about using a method...works perfectlyfine... nice!
> > > Hmmm,that's true - what do you think of this compromisefor v1, and
> > > can use any style you want, via aline at the top of your app (coding
> > > in TextArea, ignore mistakesand dumb implementations):
>
> > > // Property name "FooBarBaz" is backed by private field"fooBarBaz"
> > > RxApp.GetFieldNameForPropertyFunc = new Func<string, string>(x => {
> >> x[0] = Char.ToLower(x[0]); return x; } );
>
> > > --
> > > Paul Betts <
p...@paulbetts.org>
>
> > > On Sep 19, 3:14 am, Jörg Battermann <
j...@joergbattermann.com>wrote:
>
> > > > Paul,
>
> >> > looks good! However, the strict convention regarding the backing
> > > > field naming is somewhat 'unpleasant'. StyleCop / Microsoft Design
> > > > Guidelines say to use pascal casefor fields without'_' etc
> > > > helper librarynamed Mono.Reflection
> > > > (
http://evain.net/blog/articles/
> > > > 2009/05/01/getting-the-field-backing-a-property-using-reflection)
> > > > could but would add another dependency) so I'd suggest going with
> > > > the official naming guidelines rather than acustom one.
> > > > > Thisactuallylooks pretty similar to what I imagine, though mine
> >> > > is a bit more complex - the syntax I'd like to use is:
>
> > > > > string _SomeProperty;
> > > > >public string SomeProperty {
> > >> > get { return _SomeProperty;}
> > > > > set{ RaiseAndSet(x=> x.SomeProperty = value); }
>
> > > > > }
>
> > > > >The trick with this functionis, that you haveto make it an
> > > > > extension method,then actuallyreparse the expression into a
> >> > > getter, becausethe order of the function has to be:
>
> > >> > 1. Check for equality(Here, we rewrite the setter AST tobe an
> >> > > equalitytest then execute it) 2. Set thefield (Dig throughthe
> > > >> AST to find the propertyname, use reflection to set the backing
> > > > > field) 3. Raise property changed
>
> > > > > If you switch #2 and #3, your notification doesn't actually work
> > > > > since the framework will end up reading out the old value. Another
> > > > >way it could look is:
>
> > > > > set { RaiseAndSet(x=> x.SomeProperty,value); }
>
> > > > > Thoughts? Which one is more appealing?
>
> > > > > --
> > > > > Paul
Betts...@paulbetts.org>
> > > > > > Haveyou seen
> > > > > > expression)
> > > > > > {
> > > > >> PropertyChanged.Notify(expression);
> >> > > > }
>
> > > > > > [field:NonSerialized]
> > > > > > public event PropertyChangedEventHandler PropertyChanged;
>
> > > > > > Then to raise aproperty:
>
> > > > > > public bool MyProp
> > > > > > {
> > > > > > get { return _myProp; }
> > > > > > private set
> > > > > > {
> > > > > > _myProp = value;
> > > > > > OnPropertyChanged(()=> MyProp);
> > > >> > }
> > > > > > }
>
> > > >> > If you need the propertyname itself for the observable, then
> > > > > > I'm sure you can modify the Notify()extensionmethod or add an
> > > > > > override to get it as an out parameter.
>
> > > >> > BTW, great stuff so far. One small nitpick...could you please
> > > > >> update your code style to follow the
standard.net conventionsof
> > PascalCase?
>
> > > > > > Thanks!
>
> > > > > > On Aug 31, 11:53 pm, Paul Betts <
paul.be...@gmail.com> wrote:
>
> > > > > > > I've definitely considered that (especially to fix the
> > > > > > >abominationthat is RaiseAndSetIfChanged), I justhaven't
> > >> > > > fully wrapped my head around writingthe code to walk the
> > > > >> > expression trees yet. Definitelyin the pipeline, I'llfile a Github
> > > > >> > > Great libraryfrom what I've seenand read so far, although
> > > > > > > > I haven't had all that much time todig into it yet.
>
> > > > > > > > What I'mwonderingis if you've considered adding support
> > >> > > > > for stronglytyped INPC notifications (and in other places
> > > > > > >> where property names are used as strings)in the same way
> > > > > > > > that f.ex CaliburnMicrodoes it, with expressiontrees and
> > lambdas?
>
> > > > > > > > The literalstrings kinda irkme a bit...-Hidequoted text