顾品德code。国际奥委会救援 [英] Cruft code. IoC to the rescue

查看:132
本文介绍了顾品德code。国际奥委会救援的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在<一个href="http://stackoverflow.com/questions/871405/why-do-i-need-an-ioc-container-as-opposed-to-straightforward-di-$c$c/1532254#1532254">question关于IOC容器的用途,中奖提交提到,与IoC容器,你可以采取这样的:

In question about usefulness of IoC Container, the winning submitter mentioned that with an IoC container you can take this:

public class UglyCustomer : INotifyPropertyChanged
{
    private string _firstName;
    public string FirstName
    {
        get { return _firstName; }
        set
        {
            string oldValue = _firstName;
            _firstName = value;
            if(oldValue != value)
                OnPropertyChanged("FirstName");
        }
    }

    private string _lastName;
    public string LastName
    {
        get { return _lastName; }
        set
        {
            string oldValue = value;
            _lastName = value;
            if(oldValue != value)
                OnPropertyChanged("LastName");
        }
    }
}

这样:

var bindingFriendlyInstance = IoC.Resolve<Customer>(new NotifyPropertyChangedWrapper()); 

问题:

  • 在这神奇的IoC容器提供了这个善良?
  • 在实施这方面的例子?
  • 在任何缺点?
  • 在具有复杂的依赖关系的一个项目,我是否会当我尝试运用数据绑定到这些对象的哭呢?

推荐答案

有关你的第二个code段上班, NotifyPropertyChangedWrapper 就肯定要使用反射(或动态)生成一个类,它提供一个接口与客户兼容,并实现了自动物业通知。不应该有任何的数据绑定的问题,但仍然会有一个小的开销。

For your second code snippet to work, NotifyPropertyChangedWrapper would certainly have to use reflection (or dynamic) to generate a class that provides an interface compatible with Customer and implements the automatic property notification. There shouldn't be any data binding issues, but there would be a little overhead.

这是使用动态对象可能看起来像这样的一个简单的实现:

An simplified implementation that uses a dynamic object could look something like this:

public class NotifyPropertyChangedWrapper<T> 
    : DynamicObject, INotifyPropertyChanged
{
    private T _obj;

    public NotifyPropertyChangedWrapper(T obj)
    {
        _obj = obj;
    }

    public override bool TryGetMember(
        GetMemberBinder binder, out object result)
    {
        result = typeof(T).GetProperty(binder.Name).GetValue(_obj);
        return true;
    }

    // If you try to set a value of a property that is
    // not defined in the class, this method is called.
    public override bool TrySetMember(
        SetMemberBinder binder, object value)
    {
        typeof(T).GetProperty(binder.Name).SetValue(_obj, value);
        OnPropertyChanged(binder.Name);
        return true;
    }

    // Implement OnPropertyChanged...
}

显然,任何code,消耗这些对象之一将失去任何静态类型安全。另一种选择是生成一个类实现相同的接口类的包裹。有很多例子,对这个在网络上。主要的要求是,你的客户将不得不要么是一个接口或将需要其所有属性是虚拟的。

Obviously, any code that consumes one of these objects would lose any static type safety. The other option is to generate a class implementing the same interface as the class being wrapped. There are lots of examples for this on the web. The main requirement is that your Customer would have to either be an interface or it would need all of its properties to be virtual.

这篇关于顾品德code。国际奥委会救援的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆