DEV Community 👩‍💻👨‍💻

Discussion on: The Proxy pattern revisited.

Collapse
redfred7 profile image
Fred Heath Author • Edited on

Thanks Luis! Yes, I often have to use proxies. A recent use-case I had was this: Our client was using an external CRM API in their server code. We had built a new, better API and the client wanted to gradually phase out the old API and use ours instead. They had a class along these lines:

class oldAPIFacade
  def updateUser
    #call oldAPI with @user
  end
end 

What I did was to give them a proxy:

Module APIProxy
  def self.prepended
    ...
    Proxy.class_eval do
        define_method(m) do |*args|
           if @user.region == 'EMEA'
             # call our new API
           else # call old API  
             super(*args)
           end
        end
  end 
end

Which they prepended at the end of the oldAPIFacade class.

I then kept removing conditions from the proxy until eventually all API calls were routed to our new API. It was a seamless, phased transition :)

Collapse
luispcosta profile image
Luís Costa

That's awesome! Thank you for provide a real world example, made the blog post /concept much easier to understand. You should include it in the post itself