Last active
May 19, 2021 10:34
-
-
Save Lecarvalho/e09eef3a945640ce92ed3f975e14ca14 to your computer and use it in GitHub Desktop.
Pattern for implementing services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Domain.Entities | |
{ | |
public abstract class EntityBase | |
{ | |
public abstract bool isValid(); | |
} | |
public class Auth : EntityBase | |
{ | |
public string username; | |
public string password; | |
public override bool isValid() | |
{ | |
return !string.isNullOrWhitespace(username) | |
&& !string.isNullOrWhitespace(password); | |
} | |
} | |
} | |
namespace Services.Providers | |
{ | |
public interface IProvider | |
{ | |
} | |
public abstract class ProviderBase<P> : IProvider | |
{ | |
public abstract P ObjectTool; | |
public ProviderBase(P ObjectTool) | |
{ | |
this.ObjectTool = ObjectTool; | |
} | |
} | |
public class HttpProvider : ProviderBase<HttpClient> | |
{ | |
public override HttpClient client; | |
public HttpProvider(HttpClient client) | |
: base(client); | |
{ | |
this.client = client; | |
} | |
} | |
} | |
namespace Services | |
{ | |
public interface IService | |
{ | |
} | |
public abstract class ServiceBase<IProvider> : IService | |
{ | |
protected IProvider provider; | |
public ServiceBase(IProvider provider) | |
{ | |
this.provider = provider; | |
} | |
} | |
public interface IAuthService : IService | |
{ | |
Auth authenticate(string username, string password); | |
} | |
public class AuthService : ServiceBase<HttpProvider>, IAuthService | |
{ | |
public override HttpProvider http; | |
public AuthService(HttpProvider http) | |
: super(http) | |
{ | |
this.http = http; | |
} | |
public override Auth authenticate(string username, string password) | |
{ | |
// call http, do stuffs... | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment