# Wednesday, July 01, 2009

Ich habe gerade Ralfs Posting gelesen. Ich stimme ihm zu (auch wenn ich den DataSet doch etwas kritischer gegenüber stehe:-)), dass Changetracking nicht zum Binden Fleck werden darf.

Den Ansatz das Tracking als seperated concern zu Implementieren habe ich erst vor kurzen gewählt – und da Ralf gefagt hat wer ‘s implementiert möchte ich hier ein paar Zeilen Code teilen. Ich verlasse mich dabei darauf, dass saubere Objekte das Interface INotifyPropertyChanging implementieren. Und nein PostSharp ist auch nicht drinne.

 

public class ChangeTracker<TEntity>
    where TEntity : INotifyPropertyChanging
{
    private Hashtable _data;
    private TEntity _entity;
    private static readonly Type _entityType = typeof(TEntity);

    public void AddEntity(TEntity entity)
    {
        if (entity.EntityState != EntityState.Detached) return;
        if (_entity != null)
        {
            _entity.PropertyChanging -= EntityPropertyChanging;
        }
        _data = new Hashtable();
        _entity = entity;
        _entity.PropertyChanging += EntityPropertyChanging;
    }

    private void EntityPropertyChanging(
        object sender,
        PropertyChangingEventArgs e)
    {
        if (_data.ContainsKey(e.PropertyName)) return;
        var propertyInfo = _entityType.GetProperty(e.PropertyName);
        if (propertyInfo == null) return;
        if (!propertyInfo.CanWrite) return;
        var value = propertyInfo.GetValue(_entity, null);
        _data.Add(e.PropertyName, value);
    }

    public void ResetEntity()
    {
        _entity.PropertyChanging -= EntityPropertyChanging;

        foreach (DictionaryEntry data in _data)
        {
            var propertyInfo = _entityType.GetProperty((string)data.Key);
            if (!propertyInfo.CanWrite) continue;
            propertyInfo.SetValue(_entity, data.Value, null);
        }
        _entity = null;
    }
    public Hashtable GetChanges()
    {
        return _data;
    }
}

Wednesday, July 01, 2009 4:00:08 PM (W. Europe Daylight Time, UTC+02:00)  #    Disclaimer  |  Comments [4]  | 
Wednesday, July 01, 2009 10:01:20 PM (W. Europe Daylight Time, UTC+02:00)
Wie sieht denn das INotifyPropertyChanging interface aus?
mik3
Thursday, July 02, 2009 12:07:14 AM (W. Europe Daylight Time, UTC+02:00)
public interface INotifyPropertyChanging
{
event PropertyChanging;
}

Siehe:
http://msdn.microsoft.com/de-de/library/system.componentmodel.inotifypropertychanging.aspx
Friday, July 03, 2009 11:29:25 AM (W. Europe Daylight Time, UTC+02:00)
Hallo Daniel,

ich sitze seit einiger Zeit daran ChangeTracking sauber im EntityFramework zu implementieren und habe noch keine 100% Lösung gefunden. Dein Ansatz ist ganz nett, aber stößt sehr schnell an seine Grenzen wenn es sich nicht mehr um simple Propertys (Strings etc.) handelt, sondern um Collections oder Objekte ... dort ist es leider kuam möglich auf einfachen weg Änderungen festzustellen und diese Änderungen zu speichern. Wenn du dort eine Lösung hast, wäre ich sehr sehr interessiert!

Gruß
Robert
Thursday, July 23, 2009 1:05:03 PM (W. Europe Daylight Time, UTC+02:00)
Greeting. What other dungeon is so dark as one's own heart! What jailer so inexorable as one's self! Help me! I find sites on the topic: Accident insurance quote. I found only this - <a href="http://monruk.nersc.no/Members/Finance">interest rates for bad credit homes</a>. Finance, emploi nord pas de calais - lm vous permet de consulter les offres emploi dans le nord pas de calais ou poser vos annonces mploi ou offres. Business plan for a startup venture - do be brief. With respect :mad:, Bethany from Zaire.
Comments are closed.