1

问题:
是否可以(自动)更改通过 Visual Studio 的“添加 Web 引用”功能创建的自动生成的域对象的基类,而无需手动修改 References.cs?

背景:
当我们添加对 Web 服务的引用时(通过 Visual Studio 的“添加 Web 引用”功能),会自动生成许多类。这些代表一个代理对象(例如 MyServiceSoapClient)和许多自动生成的域对象(例如 CustomerInfo)。

所以,如果我按照以下方式做一些事情:

MyServiceSoapClient client = new MyServiceSoapClient();
CustomerInfo cust = client.GetCustomer("John Smith");

我将取回一个带有各种属性等的 CustomerInfo 对象,所有这些都很好地从服务器返回的任何 XML 中反序列化。

问题是...
假设我将 cust 对象中的 Name 属性的值更改为“Bob Dylan”。
理想情况下,我希望有一个名为 ServiceEntity 的基类,它将跟踪是否进行了更改(通过在基类中捕获大陆提供的 INotifyPropertyChanged.PropertyChanged 事件),以提供指示对象具有的“脏”属性由于它是从服务中获取的,因此发生了变化。

解决方案
虽然下面的答案很好,但我们采取了一种稍微不同的方法......
由于同步状态只需要在少数情况下记录,因此通过 Generic 类添加同步跟踪更有意义,所以我们可以在需要时使用它。
这是一个示例泛型类和接口:

界面:

public interface ISyncEntity
{
    /// <summary>
    /// Gets or Sets the Entity Sync State
    /// </summary>
    [XmlIgnore]
    [SoapIgnore]
    EntitySyncState SyncState { get; set; }

    /// <summary>
    /// Flag for deletion
    /// </summary>
    void DeleteOnSync();

    /// <summary>
    /// Flag for Creation
    /// </summary>
    void CreateOnSync();
}

班级:

public class SyncEntity<TEntity> : ISyncEntity
{
    /// <summary>
    /// Backing Field for Entity Property
    /// </summary>
    private TEntity _entity;

    /// <summary>
    /// Gets or Sets the Entity in question
    /// </summary>
    public TEntity Entity
    {
        get { return _entity; }
        set { OnEntityChange(value); }
    }

    /// <summary>
    /// Invoked when a Property on the Entity is changing
    /// </summary>
    /// <param name="entity"></param>
    protected void OnEntityChange(TEntity entity)
    {
        // Detach the property change event handler from the previous entity?
        if (_entity is INotifyPropertyChanged)
            (entity as INotifyPropertyChanged).PropertyChanged -= OnPropertyChange;

        // Set backing field
        _entity = entity;

        // Implements INotifyPropertyChanged?
        if (entity is INotifyPropertyChanged)
            (entity as INotifyPropertyChanged).PropertyChanged += OnPropertyChange;

        // Set the Sync State
        SyncState = EntitySyncState.Unchanged;
    }



    /// <summary>
    /// Fired when a property in the entity changes
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void OnPropertyChange(object sender, PropertyChangedEventArgs e)
    {
        // If a delete or create is already pending, don't worry about the update!
        if (SyncState == EntitySyncState.Unchanged)
            SyncState = EntitySyncState.UpdatePending;
    }

    #region Sync Framework Members

    [XmlIgnore]
    [SoapIgnore]
    public EntitySyncState SyncState
    {
        get;
        set;
    }

    public void DeleteOnSync()
    {
        SyncState = EntitySyncState.DeletePending;
    }

    public void CreateOnSync()
    {
        SyncState = EntitySyncState.CreatePending;
    }

    #endregion
}

扩展方法:

public static SyncEntity<TEntity> ToSyncEntity<TEntity>(this TEntity source)
{
    if (source == null)
        throw new ArgumentException("Source cannot be null");

    return new SyncEntity<TEntity>()
    {
        Entity = source
    };
}
4

1 回答 1

1

通过 Visual Studio 的 Web 引用功能生成的客户端代理类是使用 .Net Framework 的wsdl.exe 实用程序构建的。生成时,输出会生成公共部分类。您可以在另一个文件中提供额外的类代码,而不是尝试修改自动生成的输出,以添加您尝试实现的事件代码。

当然,剩下的就是为每个对象实现类似的代码。根据您的源服务,您可以考虑扩展SoapHttpClientProtocol(或代表您的对象的任何协议基类)以为所有继承对象提供单一实现。如果不使用AOP,这可能是不可能的。因此,您的里程可能会有所不同。

于 2009-09-30T16:38:41.200 回答