Alex Bibiano
Alex Bibiano

Reputation: 653

Entity Framework 4.1, Generic Repository and ObservableCollection

I'm implementing the repository pattern with EF 4.1, http://huyrua.wordpress.com/2011/04/13/entity-framework-4-poco-repository-and-specification-pattern-upgraded-to-ef-4-1

My proble is that I'm working in a legacy Winform application and in order to databind the returned collection to grid controls and detect new items, I need an ObservableCollection, but all repository method I have seen on varios repository patterns examples only return IList collections.

What I do now is:

IList<AccountType> accountTypes = _repository.GetAll<AccountType>().ToList();
var a1 = new AccountType { Name = "Bank2" };
var a2 = new AccountType { Name = "Cash2" };
accountTypes.Add(a1);
accountTypes.Add(a2);
_repository.UnitOfWork.SaveChanges();
Console.Write("AccountType Saved.");  

but with this code, added items are not persisted by the repository.

Have someone any idea how to avoid this and return a BindigList or ObservableCollection from a generic repository using EF 4.1?

EDITED:

If I convert the IList returned to a ObservableColletion, do you mean somethink like this test code I have wrote is OK?:

[TestMethod]
public void CreateAccountList()
{
    _accountTypes = new ObservableCollection<AccountType>(_repository.GetAll<AccountType>().ToList());
    _accountTypes.CollectionChanged += CollectionChanged;

    var a1 = new AccountType { Name = "Bank2" };
    var a2 = new AccountType { Name = "Cash2" };
    _accountTypes.Add(a1);
    _accountTypes.Add(a2);           
    _accountTypes.Remove(a2);
    _repository.UnitOfWork.SaveChanges();
    int count = _repository.Count<AccountType>();
    Assert.AreEqual(1, count);
}

void CollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
{
    switch (notifyCollectionChangedEventArgs.Action)
    {
        case NotifyCollectionChangedAction.Add:
            foreach (var accountType in notifyCollectionChangedEventArgs.NewItems)
            {
                _repository.Add((AccountType)accountType);    
            }       
            break;
        case NotifyCollectionChangedAction.Remove:
            foreach (var accountType in notifyCollectionChangedEventArgs.OldItems)
            {
                _repository.Delete((AccountType)accountType);
            }
            break;
    }
}

or is there an generic way of doing it?

Upvotes: 2

Views: 1142

Answers (1)

Alexey Raga
Alexey Raga

Reputation: 7525

Why can't you get IList from the repository, then build an observable collection from this list and just work with it?

You should not expect that elements will be added to the EF context when you add them into the list/collection you get from the repository. It is up to you to call the repository explicitly in order to save/delete elements you want, you will have Add and Remove methods in your repository.

If you want to do it "automatically" in your UnitOfWork, then UnitOfWork should subscribe to the events of the collection in order to know when the collection is changed, but it looks a bit weird.

Upvotes: 1

Related Questions