Skip to content

Instantly share code, notes, and snippets.

@jouana
Last active November 26, 2020 21:48
Show Gist options
  • Save jouana/84b814334f110a70ece95d8c74b789a0 to your computer and use it in GitHub Desktop.
Save jouana/84b814334f110a70ece95d8c74b789a0 to your computer and use it in GitHub Desktop.
public interface IHasId
{
string id { get; }
}
public class IRepository<Domain>
where Domain : IHasId {
Domain FindById(string id);
}
public class IUserRepository : IRepository<User>
where Domain : IHasId {
User FindById(string id);
}
public class RepositoryWithCacheProxy<Domain> :IRepository<Domain>
where Domain : IHasId
{
private IList<Domain> _cache;
private IRepository<Domain> _repository;
public RepositoryWithProxy(IRepository<Domain> repository) {
_cache = new List<Domain>();
_repository = repository;
}
Domain FindById(string id) {
var expected = _cache.FirstOrDefault(element => elem.id == id);
if (expected == null) {
expected = _repository.FindById(id);
}
return expected;
}
}
public UserRepository : IUserRepository, Repository<User> {
}
public UserRepositoryWithCache : IUserRepository, RepositoryWithCacheProxy {
private IUserRepository _userRepository;
UserRepositoryWithCache(IUserRepository userRepository) :base(userRepository) {
_userRepository = userRepository;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment