Announcement Announcement Module
Collapse
No announcement yet.
Binding form to immutable objects? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Binding form to immutable objects?

    Hi all,

    Just would like to know if anybody has done something similar to what we are trying to achieve below.

    We are developing a latency and threading sensitive system and all domain objects being passed around are immutable once created (by design). We have a web application that utilises Spring MVC and it manages mostly the static data of the system.

    In the web app, html forms are used to update the static data. When binding the form, the binding object needs to be mutable, which we are trying to avoid. We could have an intermediate Struts ActionForm like object that takes the form values and convert them into the immutable domain object, but that would mean an additional class for probably every immutable data type we have. This would create more maintenance issues, which we would like to avoid too.

    One option we are looking into is to actually override the createBinder() method in BaseCommandController to return a custom DataBinder which would be clever enough to create the relevant immutable object based on the form data.

    Another is to automatically generate a mutable version of the data type class, which wraps the mutable version as a member. Then upon form binding, a new immutable version is created and passed back to the service layer.

    Has anybody done this kind of things before? Any difficulties, suggestions or examples in implementing them?

    Many thanks,
    Charles

  • #2
    I've often wished I had this functionality. I've just opened a JIRA with the hopes that direct field binding will be supported in the future.

    http://opensource2.atlassian.com/pro...rowse/SPR-1488

    You should go add a comment, effectively "voting" for this issue. Might help.

    Comment


    • #3
      This is a really interesting question

      The Binding infrastructure (OOTB) works best if you are binding onto a Bean (i.e. with accessors), but that doesn't work at all well with the idea of immutable objects, or self validating objects.

      Some workarounds are to model the *request for a new object* as an independant object, with accessors and validators, and then provide a factory (or constructor) which takes in the RequestForNewObject and returns the immutable domain object.

      So:

      Code:
        class RequestForNewObject {
          private String name;
          private String address;
          ....
      
          ... accessors 
        }
      
        class MyDomainObject {
          public MyDomainObject(RequestForNewObject object) {
          }
        }
      
        getters...
      Introspection and validation can be done straight onto RequestForNewObject and you *know* that your domain objects will always be intact because you don't create/update them until you have sane data.

      Comment

      Working...
      X