Spring Entity to use Service, possible design flaw, but still

The legacy database I am in "springwrapping" has Id's that are strings that provide some information. For example, the UserId looks like "DK-6715-00001", which is a user in Denmark, postal code 6715. It is so wrapped in enterprise apps that it needs to be saved, and my entities confirm this in their setup methods.

But the user also has country and postal code fields, so when the id for the bean is set, he can also specify the country and postal code. To do this, it needs the CountryService to detect that the Dk is a Denmark country object and find 6715 with the newly found Country object in the PostalService.

First of all, can I hook this up to access the CountryService and PostalService from my Entity? (Objects are defined long before service objects in the bean definition). Second, it should violate any pretty design principles. Is there a better design I could make my objects carry references to the beans service?

Greetings

Nick

+2


source to share


3 answers


thanks for your input. I decided that this was indeed a design flaw and that I was thinking about it the wrong way. I have to completely remove the setter method for the Id property and just get a getter for it and then have setters and getters for the other properties that make up the Id. This way I don't have to work much to make sure the id formatting is getting correct and I don't need to access any services.

Greetings



Nick

0


source


If you're looking for design tips, here's my $ 0.02: organizations shouldn't have service beans references. Services should work at facilities, not the other way around. Therefore, if you find that you need a service reference in one of the entity classes, this is probably an indicator that there is too much business logic inside the entity class. Typically, the business logic is inside the entity class if the logic is about only one object or is fairly simple (for example, the method is equal ). But if the business logic is "cross-entity" (involving more than one entity object), it must be implemented in the service bean.



If you don't care what I think and just want to do your design work: you can inject Spring bean references inside entities using AspectJ. I believe it requires an extra compilation step and / or runtime support from AsjectJ. It is not possible to do this with Spring, because service objects must be injected at the time the entity bean is created using the new keyword , which Spring does not support.

+2


source


It looks like this is the logic that needs to be put into the controller.

This UserCreationController (with the option of adding a UserService) should have references to these CountryService or PostalService and (possibly) JdbcService or HibernateService depending on your application.

Class entities (or POJOs) should err on the side of simplicity.

Edit: This is the business logic that separates the two. The controller receives the form data, maps it to your domain entity, calls your business logic (services), and based on the result, decides where the user should go.

+1


source







All Articles