c# - How to clean up a doubly-linked many-to-many relationship? -
First of all, I'm not sure the title accurately describes what I'm saying, so be reluctant to leave Do not make a comment to call it, or if you have a representative then just rename it. For example, for example I have two classes, books and libraries. In a library there is a property which is a list of all books. A book has a property that is a list of all libraries.
In the book, I have an RemoveFromLibrary method that removes the library from the list of libraries. I also want the same method to clear the other end, i.e. the list of library books that it owns. The same goes for the other end, an exit method on the library that clears the list of library libraries.
First of all, does this make sense? It's convenient for me to call for the code that do not worry about cleaning and do not call 2 methods to take a logical action. I mean that it is never meant to be removed from one list, but not the second, at the same time, it can be said that it has been tightly mixed together, but I am thinking that if it becomes a problem then I will just tell the reflector to fix it.
I am not sure about this, as long as it calls the general public system, I will end it with the eternal loop of each method called to each other. I can work in a separate internal asset (C #) which does not clear, but it seems that I am enforcing the API with a method which means only to be told by another method. Alternatively, I can highlight the underlying compilation internally, which is slightly better, but it still does not seem ideal. Is there a better solution or should I go with one of those two, or make sure the calling code itself cleanses itself?
Maybe it will work before calling the remove method of another class , Check that the list of objects (books, for example) is still the desired-to-to-object
BTW This is only pseudocode (can not be compiled)
class book {library [library] library; Zero RemoveFromLibrary (Library Lib) {libraries.remove (lib); If (Isin Library (Lib) & amp; Lib. Habsbook (this)) // prevents call-loop lib. RemoveBook (this); } Bool IsInLibrary (Library Lib) {Return Libraries. Contact (lib); }} Class library {book [] books; Remove Zero (Book BK) {books.remove (bk); If (Hasbook (BK) and BK. ISI Library (this)) // Call-loop BK RIM libraries (this) stops; } Bool HasBook (book BK) {return books. Contain (bk); }} edit : code based on comments fixed ... I was sleeping while writing: P
Comments
Post a Comment