Jump to content

Who should dispose injected resources


Recommended Posts

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

Link to comment
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. 

Link to comment
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.

Link to comment

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).

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.