-
Notifications
You must be signed in to change notification settings - Fork 232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Randomize existing bean #149
Comments
Hi, Thank you for this feature request. I see the idea.
I would go for providing a public API to randomize an existing bean. Let me come up with a working prototype and keep you informed. You are welcome to contribute if you want. Kind regards |
I was working on adding a method to randomize an existing bean, but I struggled to find a good method name.. This is because So I'm a bit confused if this feature should really go into the library or left to the developer (may be create a random instance of the object's type and copy the desired fields?) .. |
Why not have the option to add to the EnhacedRandomBuilder a Map of Supplier instances, each one associated to the type of object to be created by the EnhancedRandomImpl. That way the user can inject default values to the created instances, so stops the randomizer from producing values for those. |
This is possible by registering a custom registry: https://github.com/benas/random-beans/wiki/Grouping-Randomizers |
I think, that library must have ability to randomize existed object. We already have private method randomize in class EnhancedRandomImpl. I suggest to create public method randomize which has parameters Object and List exclusion fields. |
Hello I've recently facing issue: I've got DTO which assigns default values like this:
It is not possible to properly randomize this object because there is:
It would be nice and useful feature to be able randomize this sort of object. I am writing it here because i think is related. |
@petrholik I guess for this usecase it would suffice to add an option for overwriting existing field values during randomization. |
Definitely, should I make PR implementing it? |
@petrholik @benas is the boss, so he gets to decide, but I think a PR would be welcome. |
Hi @petrholik, Sure, you are welcome! Please open a separate issue with the failing example. Kind regards |
Hi @sansherbina
Randomizing an existing bean was out of scope of RB in first place. As I said in my previous comment, RB has been designed as an extension of Even though it is (of course) possible to make RB randomize existing objects, for API consistency, I'm not sure this feature should be added to the library.
As of v3.4, RB provides:
How would you like to customize it to your project without being able to use one of these extension points? Can I help? Kind regards |
For my project I needed option for control recursion. I searched possibility to customize library without contributing changes to library. But this was impossible. In new version we already have this. So, my problem is resolved. |
Random beans is not perfect. There are many rooms for improvements. You asked for a feature, we've added and we are happy we solved your problem 😄 We've implemented this feature by adding an extension point, not by allowing you to modify an internal class. We try to follow the open/closed principle. So this:
is not completely true. Spring is so flexible because it provides a lot of extension points. But it has hundred of internal classes that you cannot override.
Indeed! This class has many collaborators and probably should be split. But it acts as a mediator by delegating and coordinating work among all collaborators. We've tried to make every collaborator do one thing, and at the end, we need some class to coordinate the core work, which is EnhancedRandomImpl. Anyway, I agree with you and I've got the idea of making some internal collaborators public and "overridable". We need to define a clear contract for every component, provide a default implementation, and let the user override it if needed. I'll try to proceed this way for the next major release. |
Hi, After re-reading this thread, I came to the conclusion that we actually have two approaches: making things configurable vs making things overridable (or replaceable). Currently, RB is probably trying to make everything configurable by adding configuration/extension points but not allowing to override or replace internal components. The other approach would be to provide default behavior and make components completely overridable/replaceable by the user. @sansherbina You are right, the second option is likely to be more flexible. It's even easier from the point of view of the library maintainer! Instead of trying to imagine every configuration option that every user requests.. try to define a clear contract for every component, provide defaults and make them overridable (That is, program to interfaces). This requires more design effort at first place but it leads to more flexible library and cheaper maintaince in the long term. I'll try to improve the design of RB in this direction for next major release. Best regards |
Hi Benas, |
I'm closing this issue for now. I prefer keeping the API consistent with the base class which starts from a type and returns a random instance. The suggested feature can be implemented in a fork. OSS FTW! Nevertheless, I retain all the good design ideas discussed here and will try to implement them in the next major release v4. |
Hi,
it would be nice if it would be possible to randomize an existing bean. Sometimes it's necessary to do the bean creation by yourself.
Either provide explicit methods to populate one existing bean or make the randomizer more extensible. Perhaps make more config objects public and move their creation (e.g. the ObjectFactory) to the builder and pass them to the randomizer.
What do you think?
Best regards
Lars
The text was updated successfully, but these errors were encountered: