-1

I have library with asynchronous thread and application who use them. Application can pass object to library of type provide by library interface. If application does not create new instance of object and pass for example object which is use to presentation layer than application be suspended for a few seconds may crash or cross thread operation exception may be throw. How to force on library user to create new instance of object?

2
  • What if the user just, like, uses a lock on their object? Commented Oct 31, 2016 at 8:22
  • Broken english. Tried to improve it but couldn't make sense of some parts. Commented Oct 31, 2016 at 9:34

1 Answer 1

5

Usually you don't.

Think about how you can improve your interface to make this a non-issue. Depending on the given context, it might be sensible to treat objects as immutable, or copy them. Maybe you can communicate your intentions better with a different design or naming.

Maybe it's unnecessary from the application's point of view to always pass new objects - if so, this limitation might just be considered a bug in the library that should get fixed. Are side effects part of the contract?

Finally there will always be usages that don't make sense - and it's often ok to just accept that. If data is shared where it's not supposed to, things can and will go wrong.

If it's very common to get this wrong and the number of objects is small, you might want to catch those programming errors by weak-hashing the used objects and throwing exceptions if they appear again. But maybe the user knows what he's doing and it would be perfectly fine in his scenario...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.