2

I'm implementing the DDD repository pattern (using an object database, but that is not important for the question) and in the repository there is a method like this:

Entity save(Entity entity);

Where Entity is an interface.

In the implementation, I create a proxy wrapping the received entity and overriding the getters and setters (which then write to a document) and then return it.

The point is that this proxy has to be created only if the entity is not yet proxied, but since ByteBuddy proxies do not depend on any ByteBuddy classes I don't know how to figure out whether the entity is already proxied or not.

What is the best mechanism to know if an object is already proxied by ByteBuddy?

diegomtassis
  • 3,557
  • 2
  • 19
  • 29

1 Answers1

1

Are you creating the instances yourself or is it a library that you are using? I would recommend you to implement some marker interface to any such instance and then you can perform a quick and cheap check: instance instanceof MyProxy.

That Byte Buddy does not expose any property is an important part of the library. If such a dependency existed, you could not use the library in OSGi environments, for example.

Rafael Winterhalter
  • 42,759
  • 13
  • 108
  • 192
  • I do everything manually. The marker is fine, but I don't see in the ByteBuddy API how to provide two classes to extend for the proxy (that is the real class plus the marker interface). Also the javadoc states that classes are not cached, which would be useful in my scenario. Is there an example of this somewhere? – diegomtassis Jan 19 '18 at 08:34
  • I think I've understood the cache thing, the Proxy class has to be created only once and then instantiated as many times as necessary. But then, all the proxies share the same interceptor instance, and I need to have one per each proxy (cause the document reference is there). I need to think more about it. – diegomtassis Jan 19 '18 at 08:50
  • 1
    You can add any amount of interfaces to a type by `typeBuilder.implement(...)`. For caching, have a look at the `TypeCache`. To make your proxy class stateless rather bundle the state in a sperate instance and add a field to your class that you can read from your delegation. You can set this field using an interface that contains a setter which you can also use as your marker interface. – Rafael Winterhalter Jan 19 '18 at 10:49