Suppose I have 3 classes to handle all database related requests:
public class DB_A{}
public class DB_B{}
public class DB_C{}
and I got 2 windows to interact with user:
window1.xaml;window1.xaml.cs;
window2.xaml;window2.xaml.cs
as window1 & window2 need to interact with database, they need to use functions from previous 3 classes, I created a class DataHandler:
public class DataHandler
{
public DB_A a;
public DB_B b;
public DB_C c;
public DataHandler()
{
a = new DB_A();
b = new DB_B();
c = new DB_C();
}
//some functions... ...
}
now the class DataHandler can handle all database related request, and now i need to pass a instant of DataHandler to both window1 and window2.
I tried to re-write the constructor for both window1 and window2 with parameter, but it does not allow me to do that. After google i know that WPF window form does not allow constructor with parameter.
Is there any way to pass my DataHandler to the two window form classes?
Make
DataHandlera singleton, and let the window classes access it.Then,
Similarly, write other Window class.
Or even better is, apply some variant of MVP pattern, most likely, MVVM. Read these articles: