1

I know this might be a "style of coding" question but at this point I'm really confused. Currently I'm trying to follow the MVVM pattern (ViewModel, Repository, Controller etc.)

But who should initiate a connection to the source of data? Especially when more than one Controller needs an active connection?

There aren't that many possibilities out there - either every Controller itself opens a new connection, the corresponding ViewModel opens the connection and passes it to the repository which in turn passes it to it's controller - or the connection gets instantiated even earlier (e.g. StartUp.cs).

I know there is no "perfect" solution but I hope to get some inspiration and maybe a good / best practice.

Update 1

Example Code:

namespace Question {
class ViewModel {

    Person.Person p;
    Department.Department d;

    Person.PersonRepository pR;
    Department.DepartmentRepository dR;

    // Here in the VM both classes (Person and Department) intersect - should I inject an instance of a "IDataProvider" from here into the Repositorys?
    // If so, I'd have to pass it to the repository which has to pass it to it's controller.
 }
}

namespace Question.Person {
class Person {
    // Person Model
}

class PersonRepository {
    // This class does whatever a repository does and delegates database query to it's controller
}

class PersonController {

    // Or should the Controller itself instantiate a new "IDataProvider" ?

    // This class needs a connection to the databse to execute querys
 }
}

namespace Question.Department {

class Department {
    // Department Model
}

class DepartmentRepository {
    // This class does whatever a repository does and delegates database query to it's controller
}

class DepartmentController {
    // This class needs a connection to the databse to execute querys
 }
}
Th1sD0t
  • 1,089
  • 3
  • 11
  • 37
  • 1
    Why would you share a database connection between classes? Just open and close whenever needed. Of-course abstract the database logic with some interface and inject it to the places where connection is needed. If you think I'm talking about something else; then please post the code. Otherwise hard to understand what you have in mind. – Sriram Sakthivel Jan 29 '15 at 07:00
  • I would share the code - if it'd exist. It's more a theoretical question. Sure I'd abstract the database logic with an interface - 'll try to write a peace of code to show you what I wanted to know. – Th1sD0t Jan 29 '15 at 07:14

2 Answers2

4

I think you're confusing MVC with MVVM:

MVVM overview MVC overview

The ViewModel is responsible for retrieving the information from the Model, using repositories which get the data from the database, you don't need a controller here.

 public ViewModel()
    {
       Person.PersonRepository pR;
       Department.DepartmentRepository dR;
     }

Or even better inject a repository interface into your ViewModel to get a clean, decoupled and testable implementation:

public ViewModel(IPersonRepository personRepo, IDepartmentRepository depRepo)
    {
       Person.PersonRepository pR = personRepo;
       Department.DepartmentRepository dR = depRepo;
     }
Community
  • 1
  • 1
D.Rosado
  • 5,634
  • 3
  • 36
  • 56
  • Very Good Answer, thank you! So when I define a ViewModel which expects a repository interface I have to declare / instantiate my ViewModel in the Code Behind of the corresponding View, haven't I? With a constructor without any parameters I'm able to instantiate the ViewModel inline in XAML. – Th1sD0t Jan 29 '15 at 10:36
  • Try to leave the view.xaml.cs file as empty as possible, put your viewmodels on different classes, I'd recommend you to use some MVVM framework to help you with the boilerplate, like MVVM Light. Also have a look to examples like http://apuntanotas.codeplex.com/ – D.Rosado Jan 29 '15 at 11:15
3

I think you misunderstand MVVM pattern. Read this article:

https://msdn.microsoft.com/en-us/magazine/dd419663.aspx

It should help better understood MVVM.

UPDATE:

Repository open connection. If you use ORM to access the database (EF, NHibernate), they usually used connection pool. If you not use ORM then you can implement pool.

http://martinfowler.com/eaaCatalog/repository.html - this article describes pattern "Repository". He implemented collection-like interface and shall hide a features of data access. Hence within the repository should be created connection.

sribin
  • 1,736
  • 1
  • 13
  • 21
  • Thank you for the link, it's really interesting - but I can't see why I should misunderstand MVVM - the code above is - as mentioned - nothing but an example dummy for showing you what I wanted to know. I know that there is no "rule" in MVVM or something like that. – Th1sD0t Jan 29 '15 at 09:16