dotnet-architecture / eBooks

.NET Architecture e-Books
http://dot.net/architecture
1.06k stars 241 forks source link

[Book Feedback] - Enterprise Application Patterns Using .NET MAUI #200

Closed zoli13 closed 1 year ago

zoli13 commented 1 year ago

Please provide the title of the book "Enterprise Application Patterns Using .NET MAUI"

What do you think could be improved in this book? You created sophisticated code with derived class, lambda expressions, etc to prevent using constant strings when calling OnPropertyChanged()

PDF / Page 14

public abstract class ExtendedBindableObject : BindableObject
{ 
    public void RaisePropertyChanged<T>(Expression<Func<T>> property) 
    { 
        var name = GetMemberInfo(property).Name; 
        OnPropertyChanged(name); 
    } 
    private MemberInfo GetMemberInfo(Expression expression) { // Omitted for brevity ... } 
}
[...]
public bool IsLogin
{
    get => _isLogin;
    set
    {
        _isLogin = value;
        RaisePropertyChanged(() => IsLogin);
    }
}
Using a lambda expression in this way involves a small performance cost because the lambda
expression has to be evaluated for each call. Although the performance cost is small and would not
typically impact an app, the costs can accrue when there are many change notifications. However, the
benefit of this approach is that it provides compile-time type safety and refactoring support when
renaming properties.

Why not just simple use nameof ? Then you can leave all extra code, lambda, classes out. Faster, simpler, easier, and you have the same advantage with refactoring support when renaming properties.

E.g.:

public bool IsLogin
{
    get => _isLogin;
    set
    {
        _isLogin = value;
        OnPropertyChanged(nameof(IsLogin));
    }
}
erjain commented 1 year ago

Hi @zoli13, thanks for sharing your query and feedback, as mentioned in the last paragraph of this section this approach provides compile-time type safety and refactoring support when renaming properties. I am closing this issue, please feel free to continue the conversation here in case of further queries.

zoli13 commented 1 year ago

I might be overlooking something, but my much shorter and cleaner code piece (using nameof) also provides the same : compile time check and refactoring support.