Iason
Iason

Reputation: 249

What is the correct way to pass controls around?

I am creating a new winforms application that will have datagridviews which will load matrix data, and I want the user to be able to do a bunch of operations on the data, like showing/hiding columns, editing cell values, and filtering too.

I thought of using the MVP pattern (Model, View, Presenter).

I wanted to create a presenter class, which would handle all the logic (meaning any events that the user triggers), eventually end up in the presenter which will work on the raw data (the matrices). This seems logical up to now but my question is what do I do if I want to pass the controls themselves (like the datagridviews)? Should these controls be sent the presenter class or is that bad design?

Perhaps it's better to find ways to only modify the raw data and then update my datagridviews?

Upvotes: 1

Views: 61

Answers (1)

David Pine
David Pine

Reputation: 24535

It is not a good idea to pass around controls. If you're going to use a pattern such as "MVP", then you should have the "model" contain the representation of the "view". In this case if there are various details pertaining to a set of data it belongs in the model. Then you pass the model around.

Perhaps it's better to find ways to only modify the raw data and then update my datagridviews?

So, to answer this question, "yes". Use the model to wrap the data and pass it around.

Update

Specifically, with WinForms controls belong to containers, being that they are reference types and have lots of exposed events you run a HUGE risk of passing a reference from one Form to another Form. Now, imagine that the first Form is done being used and closes and disposes, it kills the reference to the control and attempts to unwire events. Do you see where I'm going with this? It quickly becomes a nightmare trying to correctly cleanup references, and un wire event handler and since controls belong to one container by design it breaks that paradigm.

It's better to have a separation of concerns. If you need a view to have certain data, it's always best to pass around the data itself...

Upvotes: 3

Related Questions