Last active
November 26, 2020 21:48
-
-
Save jouana/84b814334f110a70ece95d8c74b789a0 to your computer and use it in GitHub Desktop.
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
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