mguymon / model-citizen

Annotation based model factory for Java
Apache License 2.0
94 stars 13 forks source link

Possability to add more than one blueprint of one class #29

Open shaddysignal opened 9 years ago

shaddysignal commented 9 years ago

Example:

@Blueprint(Car.class)
public class CarBlueprint {

    @Default
    String make = "car make";

    @Default
    String manufacturer = "car manufacturer";

    @Default
    Integer mileage = 100;
}

@Blueprint(Car.class)
public class CoolCarBlueprint {

    @Default
    String make = "cool brand";

    @Default
    String manufacturer = "cool brand ltd";

    @Default
    Integer mileage = 0;
}

@Blueprint(Car.class, alias = "bad")
public class BadCarBlueprint {

   @Default
   String make = "bad brand";

   @Default
   String manufacturer = "bad brand ltd";

   @Default
   Integer mileage = 500;
}

ModelFactory modelFactory = new ModelFactory();
modelFactory.registerBlueprint(CarBlueprint.class);
modelFactory.registerBlueprint("cool", CoolCarBlueprint.class,);
modelFactory.registerBlueprint(BadCarBlueprint.class);
Car car = modelFactory.createModel(Car.class); // -> Default car
Car coolCar = modelFactory.createModel("cool", Car.class); // -> Cool car
Car badCar = modelFactory.createModel("bad", Car.class); // -> Bad car

That will allow to create single factory for multiple cases, when we need to create different instances of one class. Also, it seems that @Mapped, @MappedList, @MappedSet, @Blueprint and addPolicy should also be modified for new functionality.

shaddysignal commented 9 years ago

I think call with explicit parameter should surpass alias in annotation

shaddysignal commented 9 years ago

Behavior for ModelFactory#createModel, I assume should be:

Unfortunetly, problem arise when there is no default Erector, but registered Erector for class. Would be nice if we can determine that, in nicely done snippet. I can come up with checking all keys, which I assume would be Pair<String, Class>, but then what to do when there multiple Erectors for one class.