开发者

Maintaining graph consistency in relationships with nHibernate

开发者 https://www.devze.com 2022-12-16 12:32 出处:网络
If I use bidirectional associations in nHibernate, is it necessary for me to add my own code to both sides of the relationship to automatically update the other side?

If I use bidirectional associations in nHibernate, is it necessary for me to add my own code to both sides of the relationship to automatically update the other side?

For example, if I have a Many-to-One relationship between Book and Category

public class Book{
    public Category Category{ get; set; }
}

public class Category{
    public IList Books{ get; set; }
}

Do I need to add code to Book.Category's set() something like this:

private Category _category;
public Category Category {
    get { return _category;  }
    set {
        Category priorCategory = _category;
        Category newCategory = value;

        if( newCategory != priorCategory ) {
            if( priorCategory != null ) {
                _category = null;
                priorCategory.Books.Remove( this );
            }

            _category = newCategory;
            if( newCategory != null ) {
                newCategory.Books.Add( this );
            }
        }
    }
}

and also add code to Category.Books something like this so that callers can add and remove books from Category.Books (e.g., Category.Books.Add() and Category.Books.Remove())?

(handling this by wrapping the List in an ObservableCollection so I can respond to add/remove events)

private IList _books { get; set; }
public ICollection<Book> Books
{
    get{
        if( _books == null ) _books = new ArrayList( );
        var observableBooks = new ObservableCollection<Book>( _books.Cast<Book>( ) );
        observableBooks.CollectionChanged += BookCollectionChanged;
        return observableBooks;
    }
}

private void OnBookAdded( Book addedBook ) {
    _books.Add( addedBook );
    addedBook.Category = this;
}

private void OnBookRemoved( Book removedBook ) {
    _books.Remove( removedBook );
    removedBook.Category = null;
}

// this just calls OnBookAdded() and OnBookRemoved() in response to change events
private void BookCollectionChanged( object sender, NotifyCollectionChangedEventArgs e ) {
    if( NotifyCollectionChangedAction.Add == e.Action ) {开发者_开发百科
        foreach( Book addedBook in e.NewItems ) {
            OnBookAdded( addedBook );
        }
    }

    if( NotifyCollectionChangedAction.Remove == e.Action ) {
        foreach( Book removedBook in e.OldItems ) {
            OnBookRemoved( removedBook );
        }
    }
}

Or does nHibernate do any graph consistency for you that it would automatically update some of this for you?


Not necessary to update both sides, but always necessary to update the "master" direction of the relationship (i.e. the one that is not mapped as "inverse"). Thus it is best practice to write code to keep the two directions in synch, and you will find it makes your life easier in the long run.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号