MVVM: where is the best place to integrate an id counter, in ViewModel or Repository or... ?

Posted by msfanboy on Stack Overflow See other posts from Stack Overflow or by msfanboy
Published on 2010-05-18T06:51:11Z Indexed on 2010/05/18 7:00 UTC
Read the original article Hit count: 266

Filed under:
|
|
|
|

Hello,

I am using http://loungerepo.codeplex.com/

this library needs a unique id when I persist my entities to the repository.

I decided for integer not Guid.

The question is now where do I retrieve a new integer and how do I do it?

This is my current approach in the SchoolclassAdministrationViewModel.cs:

public SchoolclassAdministrationViewModel()
    {
        _schoolclassRepo = new SchoolclassRepository();
        _pupilRepo = new PupilRepository();
        _subjectRepo = new SubjectRepository();

        _currentSchoolclass = new SchoolclassModel();
        _currentPupil = new PupilModel();
        _currentSubject = new SubjectModel();

...
}

private void AddSchoolclass()
    {
        // get the last free id for a schoolclass entity
        _currentSchoolclass.SchoolclassID = _schoolclassRepo.LastID; 

        // add the new schoolclass entity to the repository
        _schoolclassRepo.Add(SchoolclassModel.SchoolclassModelToSchoolclass(_currentSchoolclass));

        // add the new schoolclass entity to the ObservableCollection bound to the View
        Schoolclasses.Add(_currentSchoolclass);

        // Create a new schoolclass entity and the bound UI controls content gets cleaned
        CurrentSchoolclass = new SchoolclassModel();
    }


public class SchoolclassRepository : IRepository<Schoolclass>
{
    private int _lastID;

    public SchoolclassRepository()
    {
        _lastID = FetchLastId();
    }

    public void Add(Schoolclass entity)
    {
        //repo.Store(entity);            
    }                     

    private int FetchLastId()
    {
        return // repo.GetIDOfLastEntryAndDoInc++
    }       

    public int LastID
    {
        get { return _lastID; }
    }
}

Explanation: Every time the user switches to the SchoolclassAdministrationViewModel which is datatemplated with a UserControl the saVM Ctor is called and the schoolclass repository is created wherein the FetchLastId() is called and I am up to date with the last ID doing a inc++ on it to get the free one...

Do you have any better ideas?

What I do not like about my current apporach:

-Having a private method in repositry class because a repositry is to fetch data only not "entity logic" like the counter - Having to access from the ViewModel a public property - located in the repository -, actually its not the ViewModel concern to get a entity id and assign it. Actually the ViewModel should ask for a schoolclass POCO and get a SchoolclassModel to bind to the UI. But then I have again to re-read the Schoolclass properties into the SchoolclassModel properties what I want to avoid.

© Stack Overflow or respective owner

Related posts about mvvm

Related posts about wpf