Refactor: Create a proxy class for an existing class
We have a view layer resource that directly references a DAO layer class/interface. Our architecture standards (and in our opinion, "best practice") requires that view/resource layer NOT be coupled to a DAO layer as that BYPASS the service layer which is supposed to be "hidden underneath" the service layer. It also means an additional dependency for the resource layer that we do not want.
We want the resource layer to have knowledge ONLY of the service layer, and (only) the service layer to have knowledge of the DAO layer.
So what we want to do is generate service "proxy" classes to the DAO layer where methods have (for now) a 1-to-1 correspondence.
The IDEA "Generate Delegate" comes close to what we want, but it "converts" the DAO layer into a proxy and generates the class that it proxies TO.
What we want is kinda the opposite: We want it to merely generate a PROXY to an existing class.
Is there a way to do this in IntelliJ? Should there be?
Please sign in to leave a comment.
Hello,
Can you please provide the little code sample that illustrates the feature you want. Thank you
Yea would be nice to auto generate this esp. when the source class has many fields, can be a pain. E.g of what I think Dablick means
class Foo {
private String something;
public String getSomething(){ return this.something;}
}
class FooProxy {
private final Foo foo = new Foo();
public String getSomething() {
return this.foo.getSomething();
}
}
Yes! Thank you so much @Michael.Ekeocha.
That is precisely what I had in mind. And note that it is VERY VERY similar to what IDEA already has in the form of creating a "delegate" (rather than a "proxy"). The main difference really is whether you are refactoring the current class, or constructing a new one. In fact, the Delegate refactoring tool sorta turns the current class INTO a proxy, "in place". What I really want is an option to NOT do it "in place" (in the current class) but to construct NEW class for the delegate.
I think there's a ton of opportunity for refuse between the existing "delegate" refactor and the "create proxy" idea I'm suggesting.
I also think there are LOTS and LOTS of common "use cases" for needing this. I mean... you could think of it as "abstracting" the existing class. But rather than "abstracting" it as an interface (for which there is an existing IDEA refactor option), you are abstracting it via a "layer". You're creating a "concrete" abstraction.
Thank you for the sample and description provided. Please follow the feature request created:
https://youtrack.jetbrains.com/issue/IDEA-218763