How to alter the design so that entities don't use injections?

后端 未结 4 1513
没有蜡笔的小新
没有蜡笔的小新 2021-01-18 00:19

I\'ve read and came to realize myself that entities (data objects - for JPA or serialization) with injections in them is a bad idea. Here is my current design (all appropria

4条回答
  •  鱼传尺愫
    2021-01-18 00:42

    You can use the repository pattern. Place your business logic into a service and inject the repository (which abstracts the persistence mechanism) and manager into that. The repository hides the persistence implementation details from the business service and the entities are just simple POJOs.

    It would look something like the below with Foo being the id of the entity Bar:

    public class CarService {
    
        @Inject
        CarRepository carRepository;
    
        @Inject
        CarManager manager;
    
        piblic void operate(final Foo foo) {
    
            Bar myBar = carRepository.retrieve(foo);
            manager.doSomethingTo(myBar);
            carRepository.persist(myBar);
    
        }
    }
    

    See also: Repository Pattern Step by Step Explanation, http://deviq.com/repository-pattern/. Some frameworks such as Spring Data JPA or deltaspike already implement the repository pattern for you, all you need to do is provide an interface like the following and they generate the implementation in the background:

    @Repository
    public interface CarRepository extends EntityRepository {}
    

    Mark in answer to your request for more detail I am going to provide a remodeled solution because the example in the question really did not make sense to me and exhibits quite a few anti-patterns which lead to problematic software.

    To find a good solution to the problem touches on a lot of different considerations, many of which are very large topics with many books written about them, but I will try my best to illustrate my thinking based on these to solve the above problem.

    And apologies as I have no doubt you are aware of many of these, but I shall assume limited knowledge for the sake of clarity.

    The first step in solving this problem is not about code, but about the model itself, model driven development is covered extensively in Eric Evan's book as mentioned in the comments below. The model should drive the implementation and should also exist on its own tier as part of a layered architecture and is made up of entities, value objects and factories.

    Model Driven Development

    In the model given in the question we have something called a State, which contains AbstractPlanes and AbstractCars. You are using JPA to persists the State which is effectively an aggregate of your planes and cars. Firstly calling anything a State in software is a bad smell because pretty much everything has some sort of state, but calling what we have here which is an aggregate the State makes even less sense.

    How does one State differ from another? Is one car part of one State and another part of a different State or is it the case that all planes and cars belong to a single instance of State. What is the relationship between planes and cars in this scenario? How does a list of planes and a list of cars have any relation to a single State entity?

    Well if State was actually an Airport and we were interested in how many planes and cars were currently on the ground, then this could be the correct model. If State was an Airport it would have a name or identity such as its airport code, but it does not and so...

    ... in this case, it seems that State is an object which is being used as a convenience to allow us to access the object model. So we are effectively driving our model by implementation considerations, when we should doing it the other way round and driving our implementation from our model.

    Terms like CarData are also problematic for the same reason, creating a Car entity and then a separate object to store its Data is messy and confusing.

    Failure to get the model right results in software that is at best confused and at worst completely non-functional. This is one of the largest causes of failed IT programmes and the bigger the project the harder this stuff is to get right.


    Revised Model

    So from the model I understand that we have Cars and we have Planes, instances of which are all unique entities with their own identity. They seem to me to be separate things and so there is no point in persisting them wrapped in some aggregate entity.

    public class Plane {...}
    
    public class Car {...}
    

    Another consideration is the use of abstract classes in the model, generally we want to apply the principle of favoring composition over inheritance because inheritance can result in hidden behaviors and it can make a model hard to read. For example why have we got a ProperllerPlane and an EnginePlane? Surely a propeller is just a type of engine? I have greatly simplified the model:

    public class Plane implements Serializable {
    
        @Id
        private String name;
        private String model;
        private List engines;
    

    The Plane is an entity with its own attributes and identity. There is no need to have additional classes which represent nothing in the real world just to store attributes. The engine object is currently an enum representing the type of engine used in the plane:

    public enum Engine {
        PROPELLER, JET
    }
    

    If the engine itself were to require an identity, as in real life engine serial numbers and things are tracked, then we would change this to an object. But we might not want to allow access to it except through a Plane entity instance, in which case the Plane will be known as a aggregate root - this is an advanced topic and I would recommend Evan's book for more details on aggregates.

    The same goes for the Car entity.

    @Entity
    public class Car implements Serializable{
    
        @Id
        private String registration;
        private String type;
        private int year;
    

    The above is all you need from what was provided in the question for the basis of your model. I have then created a couple of factory classes which handle creation of instances of these entities:

    public class CarFactory {
    
        public Car makePosrche(final String registrationNumber) {
            Car porsche = new Car();
            porsche.setRegistration(registrationNumber);
            porsche.setType("Posrshe");
            porsche.setYear(1986);
            return porsche;
        }
    }
    
    public class PlaneFactory {
    
        public Plane makeSevenFourSeven(final String name) {
            Plane sevenFourSeven = new Plane();
            List engines = new ArrayList();
            engines.add(JET);
            engines.add(JET);
            engines.add(JET);
            engines.add(JET);
            sevenFourSeven.setEngines(engines);
            sevenFourSeven.setName(name);
            return sevenFourSeven;
        }
    
        public Plane makeSpitFire(final String name) {
            Plane spitFire = new Plane();
            List engines = new ArrayList();
            engines.add(PROPELLER);
            spitFire.setEngines(engines);
            spitFire.setModel("Spitfire");
            spitFire.setName(name);
            return spitFire;
        }
    }
    

    What we are also doing here is separating out concerns as according to the Single Responsibility Principle each class should only really do one thing.


    Now that we have a model we need to know how to interact with it. In this case we would most likely if using JPA persist the Cars in a table called Car and the Planes likewise. We would provide access to these persisted entities via repositories, CarRepository and PlaneRespository.

    You can then create classes called services which inject the repositories (and anything else you require) to perform CRUD (Create Read Update Delete) operations on the instances of cars and planes and also this is the point where you can apply your business logic to these. Such as your method:

    void operate(int i) {..}
    

    By structuring your code this way you decouple the model (entities and value objects) from how they are persisted (repositories) from the services which operate on them as mentioned in your question:

    I'm looking for a design that decouples the injection from the data saving process.

提交回复
热议问题