I'm going to create a custom user control. It require a connection to work, so I think to pass the conn in the constructor but I don't know if it's possible because the designer initialize the component itself so "he don't know" what parameters to pass. Is there a way to force the user/programmer to pass a specific parameter to the user control or to specify that it require this parameter (dependency injection)?
Asked
Active
Viewed 715 times
0
-
*"custom user control. It require a connection to work"* - start with separating view (`UserControl`) from model (that functionality what required connection). Is it WPF? Then MVVM. – Sinatr Mar 06 '17 at 15:48
-
no, winforms. what do u mean by "separating view from model"? – CRK Mar 06 '17 at 15:51
-
It's a level of abstraction, see [question here](http://softwareengineering.stackexchange.com/q/277143/156546). If you do it correctly then you don't need anymore connection in `UserControl` and won't have similar to the current problems. – Sinatr Mar 06 '17 at 15:54
-
I don't understand in which part it notify the programmer to bind (in my case a connection) the object to the control. @Sinatr – CRK Mar 06 '17 at 15:59
-
Can you not just throw a custom exception if the user control tries to access the uninitialized connection? – Wheels73 Mar 06 '17 at 16:04
-
There is no such relations as "connection and control" anymore. Connection is used by ViewModel (again, using MVVM pattern) and/or Model, while View don't need any connection to function. Could you explain how you think to use connection in the control now? Perhaps me or rather someone else then will be able to give you an example of such abstraction. – Sinatr Mar 06 '17 at 16:04
-
I need to show some data into a grid that's contained into this custom controls. To show this data it requires a connection to read from db. I don't want that "tomorrow" a programmer need to know i.e. what property he have to initialize to show data, but notify him that the control require a connection to work like the constructor do (like a contract that the control require the connection to work). – CRK Mar 06 '17 at 16:08
-
This data shown in the grid can be abstracted as collection in ViewModel. And queries are done by ViewModel/Model. And commands (buttons clicks), validation, etc. are instead invoke ViewModel methods. Now your control don't need connection anymore. It only needs bindings to ViewModel properties. Unfortunately I have no clue how to organize binding in winforms, perhaps someone else can post/find an answer for you, but I hope idea is clearer to you now. – Sinatr Mar 06 '17 at 16:16
-
you mean that the custom control have a public method to bind my model to the control? this not solve my problem to have a mandatory parameter (connection). Maybe is not clear for me. – CRK Mar 06 '17 at 16:30
-
the custom control is a specific grid with these data, no buttons or other stuff. – CRK Mar 06 '17 at 16:40