I am passing data to Pages in C# by including a reference to an object as a parameter in the page's constructor. I'm only using a portion of the members of the object in the page. A call to such a page looks like so:
new PageName(DataObject);
Where the DataObject set to be the DataContext of the new Page:
this.DataContext = DataObject;
From which I can perform all data operations I need to do on DataObject. One key feature with this setup is that changes made to the DataContext affect the passed DataObject; this is intended and desirable.
This is fairly simple and works well for now. One concern i have is that PageName must know to cast DataContext to the correct object type, and must know the names of object properties, creating significant coupling. Is this generally an acceptable level of coupling, or would it be worth the effort to pass ref parameters to reduce the magnitude of coupling between PageName and DataObject?
Edit: On average, if I were passing parameters, I would usually need to pass 4-5 parameters to each page. Not every page needs the same parameters passed to it.
There is a refactoring called introduce parameter object.
The fact that it exists points to having an object instead of multiple parameters as being the more maintainable option.
This is a pattern used in the framework in multiple places - see Process and ProcessStartInfo - when an object can be configured multiple ways, supplying a configuration object (yes, that is tightly coupled to the object) is one design decision.
If the configuration object is only to be used with the object it configures, I don't really see a problem with this coupling.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With