John Gall wrote:A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over with a working simple system.
Stan Belen wrote:
At this point I'll be creating 3 interfaces:
EmployeeSource ShouldUpdateDecide EmployeeRepository
And, the interfaces will require classes that will implement them.
The code that I wrote is just a rough example. My question is: is it okay to be creating so many interfaces?
Junilu Lacar wrote:I'm going to take a slightly different tack.
TDD is mostly a design technique. When you're writing tests before the production code, the tests are actually specifications, specifically, design specifications. One of the things that jumps out at me when I read your post is that you're immediately using mocks. That means you're already pulling dependencies into your unit test. This can be a signal that your design is more complicated than it needs to be and that there might be a simpler way to organize the ideas in your code.
You started out with a service class. Why do you need a service class to establish a relationship between Employee and Department? Why bring in the concept of "mapping" right away? Do you even need "mapping"? Why couldn't the Employee and Department classes maintain their own relationships with each other?
If it were me, I'd probably start with a test like this:
This is easy enough to implement without pulling in too many dependencies right off the bat. Granted, this may be a naive implementation but it gets me started with a simple design and one that I can easily understand. Prefer to always start with a simple system. then work your way incrementally to the more complex.
Keep Gall's Law in mind when you're designing:
John Gall wrote:A complex system that works is invariably found to have evolved from a simple system that worked. A complex system designed from scratch never works and cannot be patched up to make it work. You have to start over with a working simple system.
I wrote:Grammatically, "when should save" doesn't make sense to me.
Stan Belen wrote:
I see the advantage of your approach, actually, from the very beginning, you made a design decision by using the Department.UNASSIGNED value as the default value of a newly created Employee object. At this initial point, you could have decided also that let the default value of the Employee department is null or Optional.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Stan Belen wrote:
I don't want my service to blindly update all the Employees in the repository because there will be employees whose department did not change so I will need a mechanism to choose. I'll create a class with a method that returns a boolean, takes as parameters an Employee object and a Department object.
Stan Belen wrote:
Building on top of the previous test, I probably need a class with a method that will accept a list of the Employees and a Map of String key and Department object value (the key will be the Id of an Employee and the value is the department object for this employee). This method should return a list of employees that need to be updated in the repository
Stan Belen wrote:To be honest, I have a feeling that I might have took too big of steps/tests in the above example but not sure how to make them smaller.
Stan Belen wrote:Junilu, thank you so much for your help. What you talk about is really awesome things. In addition to this forum, are there any resources that you can recommend to get better at this?
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
The secret of how to be miserable is to constantly expect things are going to happen the way that they are "supposed" to happen.
You can have faith, which carries the understanding that you may be disappointed. Then there's being a willfully-blind idiot, which virtually guarantees it.
Beauty is in the eye of the tiny ad.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|