问:
是否有可能(自动)更改基类通过Visual Studio创建自动生成的域对象的“添加Web引用”功能,无需手动修改References.cs?指定Web服务引用自动生成的实体基类
背景:
当我们添加一个引用到Web服务(通过Visual Studio的“添加Web引用”功能),一些类是自动生成的。这些表示代理对象(例如MyServiceSoapClient)和一些自动生成的域对象(例如,CustomerInfo)。
所以,如果我做沿着以下线的东西:
MyServiceSoapClient client = new MyServiceSoapClient();
CustomerInfo cust = client.GetCustomer("John Smith");
我会回来一个CustomerInfo对象具有各种属性等,均能从任何XML服务器返回的反序列化。
问题是...
可以说,我更改卡斯特对象为“鲍勃·迪伦”的名称属性的值。
理想情况下,我想有一个名为ServiceEntity的基类,它将跟踪是否进行了更改(通过捕获基本类中提供的Continently提供的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
};
}
丹你不必编辑标题说它已经解决了,选择一个答案就足够了,让人们更容易搜索 – blowdart