0
votes

I cannot find any documentation on connecting a view model to a repository using Catel.

I have set up the Repository Pattern and my Models with EF6 Code First (all extending from ModelBase) but need to know how to use it with a ViewModel.

Do I need to create a service for the UnitOfWork? And if so, how? How will I use this in a ViewModel?

I am currently using the repository as a model in my viewmodel, but i do not think this is the correct way to do it? See my CompaniesViewModel below:

IUnitOfWork uow;

    public CompaniesViewModel()
    {
        uow = new UnitOfWork<SoftwareSolutionsContext>();

        CompanyRepository = uow.GetRepository<ICompanyRepository>();
    }

    public override string Title { get { return "Companies"; } }

    protected override async Task Close()
    {
        uow.Dispose();

        await base.Close();
    }

    protected override async Task Initialize()
    {
        Companies = new ObservableCollection<Company>(CompanyRepository.GetAll());

        await base.Initialize();
    }

    public ObservableCollection<Company> Companies
    {
        get { return GetValue<ObservableCollection<Company>>(CompaniesProperty); }
        set { SetValue(CompaniesProperty, value); }
    }

    public static readonly PropertyData CompaniesProperty = RegisterProperty("Companies", typeof(ObservableCollection<Company>), null);

    [Model]
    public ICompanyRepository CompanyRepository
    {
        get { return GetValue<ICompanyRepository>(CompanyRepositoryProperty); }
        private set { SetValue(CompanyRepositoryProperty, value); }
    }

    public static readonly PropertyData CompanyRepositoryProperty = RegisterProperty("CompanyRepository", typeof(ICompanyRepository));

Essentially, I have 2 scenarios for working on the data:

  1. getting all the data to display on a datagrid
  2. selecting a record on the datagrid to open another view for editing a single record

Any guidance would be appreciated.

1

1 Answers

1
votes

This is a very difficult subject, because there are basically a few options here:

  1. Create abstractions in services (so the VM's only work with services, the services are your API into the db). The services work with the UoW
  2. There are some people thinking that 1 is overcomplicated. In that case, you can simply use the UoW inside your VM's

Both have their pros and cons, just pick what you believe in most.