pawhan11 Posted September 21, 2021 Report Share Posted September 21, 2021 I started to play with LabVIEW interfaces and have some general architecture problems. Assume we have - IDisposable interface with single Dispose method used to clear resources - ITransport interface with Read and Write methods to pass some data - ISystem interface with ReadTime method - TCP transport implementing IDisposable and ITransport - System implementing IDisposable and ISystem TCP is injected into Sysem by main method, who should be responsible of calling Dispose on TCP? My thinking is that object that was responsible for its creation - in this case main.vi. But this leads to problem of passing TCP class on main.vi Shall ITransport implement IDisposable? My thinking is it should not - we have no guarantee that all Transport will require Dispose. The same goes for System. This leads to problems of having to pass concrete implementation all over the system just to close it when required. Another solution would be to implement additional factory classes that would move instantiation of Transport into System, but then it must implement IDisposable. What would be the best way to approach it? For now I enforced IDisposable on ITransport and ISystem Example.7z Quote Link to comment
drjdpowell Posted September 21, 2021 Report Share Posted September 21, 2021 Note that System could have a method to remove the Transport ("uninject"?). Main.vi can remove the TCP and dispose of it, just before it disposes of System. Quote Link to comment
pawhan11 Posted September 21, 2021 Author Report Share Posted September 21, 2021 34 minutes ago, drjdpowell said: Note that System could have a method to remove the Transport ("uninject"?). Main.vi can remove the TCP and dispose of it, just before it disposes of System. Problem is that System is aware of ITransport that does not have IDispose. It would require cast after uninject function to TCP to call dispose. Quote Link to comment
drjdpowell Posted September 21, 2021 Report Share Posted September 21, 2021 3 hours ago, pawhan11 said: Problem is that System is aware of ITransport that does not have IDispose. It would require cast after uninject function to TCP to call dispose. So? Main.vi knows it might need disposing, as it did the creating, so do the cast. Quote Link to comment
ak_nz Posted September 21, 2021 Report Share Posted September 21, 2021 I'll preface my answer with - I haven't opened your code (no access to LV right now). So I can't speak to specifics. But my opinion - you should have a single composition root (main.vi) that creates and injects all the main resources at the beginning of execution. Then, that same root manages disposition (including any specific ordering) at application exit. Main either keeps references for later disposal, or each level of your execution 'hierarchy' returns it's injected dependencies recursively to enable that (eg. as part of a Dispose() method). Quote Link to comment
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.