You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by fernandospr <fe...@gmail.com> on 2011/03/08 16:13:07 UTC

DataProvider calling a Service/Dao that requires a domain object

Hi,

I have a DataProvider that implements IDataProvider, and for example in the
size method I need to call the service as follows:

    public int size() {
        return service.countAllByDomainObjectY(domainObjectY);
    }

DomainObjectX contains a manytoone relationship with DomainObjectY.
So, this countAllByDomainObjectY internally searches by the domainObjectY's
id and returns the count of DomainObjectX associated with that specific
domainObjectY.

Now, in order to do this I would need to have domainObjectY as a property of
the DataProvider which I think it is not good because it would be
serialized.

Should I change the service to receive just the domainObjectY's id and only
"save" this id as a property of the DataProvider?

--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DataProvider-calling-a-Service-Dao-that-requires-a-domain-object-tp3341572p3341572.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DataProvider calling a Service/Dao that requires a domain object

Posted by fernandospr <fe...@gmail.com>.
Thanks Michael for your quick response.

Let me know if I got it correctly:

I created the following generic model:
public class GenericDetachableModel extends LoadableDetachableModel {
	private final long id;
	private final Service service;
	
	public GenericDetachableModel(T object, Service service) {
		this(object, object.getId(), service);
	}
	
	public GenericDetachableModel(T object, long id, Service service) {
		super(object);
		if (id == 0) {
			throw new IllegalArgumentException();
		}
		this.id = id;
		this.service = service;
	}
	
	@Override
	protected T load() {
		return (T) service.find(id);
	}
}


Now, when I create the DataProvider in my page I can use:

      myModel = new GenericDetachableModel( domainObjectY, aService );
      ...
      myDataProv = new MyDataProvider( myModel, aService );

And then inside MyDataProvider I can use:

      public MyDataProvider(IModel myModel, Service aService) {
          this.myModel = myModel;
          this.aService = aService;
      }

      public int size() { 
          return service.countAllByDomainObjectY( myModel.getObject() ); 
      } 

      public void detach() {
          myModel.detach();
      }

Is this ok?
Should myModel property in MyDataProvider be @Transient?
Is it ok to call myModel.detach() inside the MyDataProvider's detach? or
should I do it outside? (I mean, from the page where I created the
DataProvider)

Thanks in advance!


--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/DataProvider-calling-a-Service-Dao-that-requires-a-domain-object-tp3341572p3341770.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: DataProvider calling a Service/Dao that requires a domain object

Posted by Michael O'Cleirigh <mi...@rivulet.ca>.
Hi,

Pass an IModel<DomainObject>model into your dataprovider instead of the 
DomainObject itself.

e.g.

class MyPanel

     private final IModel<DomainObject>model;

     public MyPanel (String id) {
         super (id);

     model = new LoadableDetachableModel() {
             protected DomainObject load() {// return object
             }


     add (new DataTable ("table", columns, new DomainObjectAwareProvider 
(model), ...);

     }
       protected void detatchModels() {
         model.detatch();
         super.detatchModels();
      }
}

The detachment of the model can be managed by the panel that creates the 
model and you can just use the object returned from the model within 
your data provider.  This is better than passing the ID because your 
provider can just work against the DomainObject without also having to 
be responsible for loading it.

Regards,

Mike

> Hi,
>
> I have a DataProvider that implements IDataProvider, and for example in the
> size method I need to call the service as follows:
>
>      public int size() {
>          return service.countAllByDomainObjectY(domainObjectY);
>      }
>
> DomainObjectX contains a manytoone relationship with DomainObjectY.
> So, this countAllByDomainObjectY internally searches by the domainObjectY's
> id and returns the count of DomainObjectX associated with that specific
> domainObjectY.
>
> Now, in order to do this I would need to have domainObjectY as a property of
> the DataProvider which I think it is not good because it would be
> serialized.
>
> Should I change the service to receive just the domainObjectY's id and only
> "save" this id as a property of the DataProvider?
>
> --
> View this message in context: http://apache-wicket.1842946.n4.nabble.com/DataProvider-calling-a-Service-Dao-that-requires-a-domain-object-tp3341572p3341572.html
> Sent from the Users forum mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org