vendredi 11 juin 2021

How to lazy load an object into model class without making connection between model class and repository class

I have an Account class and AccountData class. AccountData has a reference to the Account class.

I am using Repository pattern, MVVM, XML serialization. When serializing AccountData class I store the id of the account (which I can use to find Account class from AccountRepository).

I want to know if there is a good way to load Account object inside AccountData class. Possibly I can use Lazy Load design pattern to populate Account field in AccountData class only when I need it, but I want to do it in a way where I do not have repository calls inside my model class.

The first approach I used was like this:

public AccountData{
    [System.Xml.Serialization.XmlIgnore]
        public Account Account
        {
            get { return account; }
            set { account = value; }
        }

        public String AccountId
        {
            get { return account.Id; }
            set
            {
                AccountRepository accountRepository = new AccountRepository();
                account = accountRepository.GetAccountById(value);
            }
        }
}

I realize it is pretty bad to have a relationship between your model class and repository class. I want to refactor this and somehow fill the Account field with Account object when get method is called. Using Lazy Load pattern I can achieve this, but I still have relationship between the model and repository.

How can I achieve this?

Aucun commentaire:

Enregistrer un commentaire