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 (eg 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
}
}
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.
I think you're confusing MVC with MVVM:
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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.