lundi 14 juin 2021

Is this an Adapter or a Proxy?

You work on a legacy app which has a static class UserDataAccess:

public static class UserDataAccess
{
     public static void AddUser(User user) 
     {
        // Insert user into DB
     }
}

which is used by a UserService class:

public class UserService 
{
   public bool AddUser(string firstName, string lastName)
   {
      User user = ...
      UserDataAccess.AddUser(user);
   }
}

You need to add unit tests for the UserService class, but you cannot modify the UserDataAccess (you are not allowed, you do not have access to the DB).

A good solution is to create an interface and inject into UserService:

public interface IUserDataAccess {
     void AddUser(User user);
}

and add an implementation which delegates the call to the static class:

public class UserDataAccessProxyOrAdapter : IUserDataAccess 
{
   public void AddUser(User user) { 
       UserDataAccess.AddUser(user);
   }
}

My question is, is this a Proxy or an Adapter?

Proxy is supposed to add some functionality. Can the access to the static resource be considered a functionality?

It looks like an Adapter because it adapts the UserDataAccess to be called through the IUserDataAccess interface

What is the correct reasoning and why?

EDIT: This is from this refactoring test, specifically at this step: https://youtu.be/U3QvTaw224o?t=944

Aucun commentaire:

Enregistrer un commentaire