Skip to content

Instantly share code, notes, and snippets.

@Lecarvalho
Last active May 19, 2021 10:34
Show Gist options
  • Save Lecarvalho/e09eef3a945640ce92ed3f975e14ca14 to your computer and use it in GitHub Desktop.
Save Lecarvalho/e09eef3a945640ce92ed3f975e14ca14 to your computer and use it in GitHub Desktop.
Pattern for implementing services
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