Skip to main content
27 events
when toggle format what by license comment
Apr 12, 2019 at 15:43 vote accept Andre Borges
Mar 28, 2019 at 19:08 history edited Doc Brown CC BY-SA 4.0
added 144 characters in body
Mar 28, 2019 at 14:24 comment added Doc Brown @AndreBorges: sorry, I forgot to add access modifiers to my code snippet, see my edit. The idea is to keep the public interface exactly the same, so this is definitely a Proxy, not a Decorator.
Mar 28, 2019 at 14:22 history edited Doc Brown CC BY-SA 4.0
public/private added
Mar 28, 2019 at 12:27 comment added Andre Borges @DocBrown, your proposed solution looks more like a Decorator pattern than a Proxy because it focuses on adding functions to an object rather than controlling access (distinction taken from here)
Mar 28, 2019 at 9:10 history edited Doc Brown CC BY-SA 4.0
added 310 characters in body
Mar 28, 2019 at 7:30 history edited Laiv CC BY-SA 4.0
added 2 characters in body
Mar 28, 2019 at 7:08 history edited Laiv CC BY-SA 4.0
deleted 43 characters in body
Mar 28, 2019 at 5:00 history edited Doc Brown CC BY-SA 4.0
added 448 characters in body
Mar 28, 2019 at 4:48 comment added Doc Brown @user949300: you may have missed that the idea to add event mechanics to the repo for allowing to add additional business requirements in a separated manner is already in the original question. But the one-and-only thing which cannot be separated out by such an event mechanics is the event mechanics itself. My answer just adresses this problem in terms of the SRP and does not question the OP's idea of using events.
Mar 28, 2019 at 4:39 comment added Doc Brown @JohnWu: that may be a problem of the original code, or not, you need to ask the OP (from the question description I would guess _dataContext.SaveChanges does a commit itself). My solution makes the assumption that the semantics of the original code is ok, and adresses the actual question about the SRP without changing the semantics.
Mar 27, 2019 at 23:17 comment added John Wu How does this solution work if there is a transaction in progress? When that is going on, SaveChanges() does not actually create the database record, and it could end up getting rolled back. Seems like you'd have to either override AcceptAllChanges or subscribe to the TransactionCompleted event.
Mar 27, 2019 at 23:13 comment added user949300 Yeah, there would never be a problem with duplicate listeners, zombie listeners, circular listeners. :-(. I agree that listeners are cleaner, this is a nice design, but something has to set them up properly. It's like FP "get rid of state". A good idea where possible. But some state needs to continue on and something needs to hold it.
Mar 27, 2019 at 21:00 comment added Doc Brown @user949300: you know that in C# events usually support multicasting? So each event can have multiple listeners. No need to write additional proxies for them.
Mar 27, 2019 at 20:54 comment added user949300 Doc, that just hides the issue on the listeners. Do you write proxies for them to add capabilities? How do you keep SRP in them?
Mar 27, 2019 at 20:47 history edited Doc Brown CC BY-SA 4.0
added 295 characters in body
Mar 27, 2019 at 20:42 history edited Doc Brown CC BY-SA 4.0
added 295 characters in body
Mar 27, 2019 at 19:40 comment added Doc Brown @user949300: once an event mechanics was added to the repo, all other kinds of requirements of this type don't need another proxy, they can simply subscribe to the events.
Mar 27, 2019 at 18:49 comment added user949300 This is an interesting answer, and a good illustration of the OCP. But, let's extrapolate. In a week, they want to add a "add this user to the you get a month free tier", and, after that month, add "don't forget to charge their credit card since they forgot to unsubscribe". Do you suggest "proxies all the way down"? (This is where I fear the OCP starts to fall apart)
S Mar 27, 2019 at 16:43 history suggested Peter Mortensen CC BY-SA 4.0
Copy edited (e.g. ref. <https://en.wiktionary.org/wiki/justified#Adjective> and <https://en.wiktionary.org/wiki/principle#Noun>).
Mar 27, 2019 at 15:33 comment added Keith Payne This is the best/correct answer IMO. Not only does it maintain SRP, but it also maintains the Open/Closed principle. And think of all of the automated tests that changes within the class could break. Modifying existing tests when new functionality is added is a big smell.
Mar 27, 2019 at 14:25 comment added Doc Brown @Ewan: please read the question again. It was about a place in code which does certain actions which needs to be coupled with other actions outside of the responsivility of that object. And putting the action and the event raising in one place was questioned by a peer reviewer as breaking the SRP. The example of "saving new users" is only for the purpose of demonstration, call the example contrived if you like, but that is IMHO not the point of the question.
Mar 27, 2019 at 9:53 review Suggested edits
S Mar 27, 2019 at 16:43
Mar 27, 2019 at 9:45 comment added Ewan I think you miss the point, its not that raising an event breaks the SRP its that raising an event only for "New" users requires the repo to be responsible for knowing what constitutes a "New" user rather than a "Newly Added to Me" user
Mar 27, 2019 at 8:53 comment added Laiv In addition to this answer. There are alternatives to proxies, like AOP.
Mar 27, 2019 at 2:38 history edited Doc Brown CC BY-SA 4.0
added 139 characters in body
Mar 27, 2019 at 2:32 history answered Doc Brown CC BY-SA 4.0