질문 :
이 가능합니다 (자동)는 Visual Studio를 통해 생성 된 자동 생성 된 도메인 객체의 기본 클래스를 직접 변경하지 않고, 기능의 웹 참조 추가 ' References.cs 수정?웹 서비스 참조 엔터티의 기본 클래스를 자동 생성 지정
배경 :
우리는 웹 서비스에 대한 참조를 추가합니다 (Visual Studio를 통해 기능 '웹 참조 추가'), 클래스의 숫자가 자동으로 생성됩니다. 이 문서는 프록시 객체 (예 : MyServiceSoapClient) 및 자동 생성 도메인 객체 (예 : CustomerInfo)를 나타냅니다. 나는 다음의 라인을 따라 뭔가 할 경우
그래서 :
MyServiceSoapClient client = new MyServiceSoapClient();
CustomerInfo cust = client.GetCustomer("John Smith");
을 나는 다양한 속성 등으로 CustomerInfo 객체를 다시 얻을 것이다, 모든 멋지게 서버가 반환 어떤 XML로 직렬화.
문제가 ...
내가 '밥 딜런'에 CUST 개체의 Name 속성의 값을 변경 말할 수 있습니다.
이상한 점은, 기본 클래스에 지속적으로 제공되는 INotifyPropertyChanged.PropertyChanged 이벤트를 트래핑하여 변경 사항을 추적하면 ServiceEntity라는 기본 클래스를 갖고 싶습니다. 객체가 서비스에서 가져온 이후 변경되었습니다. 동기화 상태가 몇 상황에 기록 될 필요로
솔루션을
아래의 대답은 좋은 일이지만
했다, 그것은 추가하는 것이 더 의미가 만든 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