洁癖的程序员

当一个程序员有“洁癖”的时候,它的代码才会越写越好。

虽然不敢说自己水平有多高,但是自己写代码也越来越染上洁癖,

不允许自己的代码中有一丝敷衍或者是架构的别扭。


每个细节都要优美,考虑设计模式。每个逻辑都考虑如何复用,虽然代码写起来可能比较慢,但是出自自己手下的代码一定要是“精品”。


这里奉上网上一个兄弟提供的WPF中数据绑定的属性精简写法,


本来很长一段,如下。微软官方代码生成的例子

public string Password {
            get {
                return this.PasswordField;
            }
            set {
                if ((object.ReferenceEquals(this.PasswordField, value) != true)) {
                    this.PasswordField = value;
                    this.RaisePropertyChanged("Password");
                }
            }
        }

但是可以封装基类,做到优雅的写法

private DateTime _updateTime;
        public DateTime UpdateTime
        {
            get { return _updateTime; }
            set { _updateTime = value; this.NotifyPropertyChanged(p => p.UpdateTime); }
        }

请看基类

    public class PropertyChangedBase : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
    public static class PropertyChangedBaseEx
    {
        public static void NotifyPropertyChanged<T, TProperty>(this T propertyChangedBase, Expression<Func<T, TProperty>> expression) where T : PropertyChangedBase
        {
            var memberExpression = expression.Body as MemberExpression;
            if (memberExpression != null)
            {
                string propertyName = memberExpression.Member.Name;
                propertyChangedBase.NotifyPropertyChanged(propertyName);
            }
            else
                throw new NotImplementedException();
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值