tag:blogger.com,1999:blog-4199299413792837033.post7966344539036504138..comments2022-11-21T02:23:12.645-08:00Comments on a programmer's blog: Small case in favor of setter injectionmikehttp://www.blogger.com/profile/03503834357403512675noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-4199299413792837033.post-38421746237873620662009-11-26T10:11:52.530-08:002009-11-26T10:11:52.530-08:00No worries, thanks for following it up and startin...No worries, thanks for following it up and starting a nice conversation.<br /><br />Yes, I really meant integration tests. :)<br /><br />I know next to nothing in C#, I can read code as the syntax is similar to Java and I'm familiar with concepts like delegates and some of the functional things that were put in recently in the language but I never wrote a single line.<br /><br />Guice is a DI that allows you to do that as well.<br /><br />As far as everyone knows that in concept the class is _immutable_ but in practice it is not and people don't misuse it, I guess it's fine. :)<br /><br />Have a look at the DDD book and some talks in InfoQ about value objects. Now a days I can't live without them but some of it because of the lack of list comprehensions and closures in java.<br /><br />About SRP, if you find that they are all needed together does it still make sense to only inject half the object?nfmahttps://www.blogger.com/profile/02742274955887316911noreply@blogger.comtag:blogger.com,1999:blog-4199299413792837033.post-28798426540799975992009-11-26T08:36:28.981-08:002009-11-26T08:36:28.981-08:00thank you for your response ... and your tweet tha...thank you for your response ... and your tweet that triggered the post :)<br /><br />I forgot to mention I use C# and declare the dependencies as "public Command ToSave { get; set; }" ... I think my main objection with Java is not having properties ... so yes if I would still need to write a set/get method I would not gain anything<br /><br />my unit tests would not help me much because the dependencies are *always* mocked ... my most common error between done and done done is a Null exception because I forgot to inject the dependency ... at that point all unit tests are green but I have a red acceptance test ... i'm always happy to be reminded that i didn't think of object construction at all <br /><br />if I use a DI framework I could even auto inject dependencies based on conventions and I would almost not need to be reminded unless I'm in a special case ... but I haven't got there yet (laziness)<br /><br />I haven't got too far finding good Value Objects in my designs ... got to improve there ... still I only depend on interfaces so the class is as immutable as its interface despite having public dependencies<br /><br />SRP is very fuzzy ... I do find the smell of many dependencies a trigger unless it's a facade object ... but I was meaning more about the cohesion of the dependencies ... if they are all needed together the facade sounds to be following SRPmikehttps://www.blogger.com/profile/03503834357403512675noreply@blogger.comtag:blogger.com,1999:blog-4199299413792837033.post-66576857100348315162009-11-26T08:10:58.250-08:002009-11-26T08:10:58.250-08:00I agree with you in almost all points but I still ...I agree with you in almost all points but I still stand that in terms of intent, for instance, constructors are the language mechanism to use when you want to build an object.<br /><br />In terms of cleanliness, it appears that it is in the eye of the beholder because to me using the constructors instead of having my classes polluted with setter methods seems more clean. <br /><br />You can argue that this policy (of using constructors only) is a bit paranoid. I would say that it is less error prone. But unit tests will mitigate this risk by a lot.<br /><br />I have a compelling argument for the constructor injection which is immutability. Immutable objects are so must better to handle, test, copy and pass around.<br /><br />I agree that it is a smell to have multiple dependencies and only use some of them but the problem doesn't not lye in the constructor of the object but in its responsibilities - it might be doing too much...nfmahttps://www.blogger.com/profile/02742274955887316911noreply@blogger.com