问题:
是否可以(自动)更改通过 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
};
}