Last active
April 18, 2023 19:08
-
-
Save mrange/0a15111e809e27fbc3463892fb9ba91b to your computer and use it in GitHub Desktop.
Repo of Repos
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
var repoRepos = new RepositoryRepository(); | |
var pageRepo0 = repoRepos.GetRepository<IPage>(); | |
var pageRepo1 = repoRepos.GetRepository<IPage>(); | |
class RepositoryRepository | |
{ | |
readonly List<IBaseRepository> _repos = new(); | |
IBaseRepository CreateRepository(Type t) | |
{ | |
if (t == typeof(IPage)) return new PageRepository(); | |
throw new ArgumentException($"Unexpected type: {t.FullName}"); | |
} | |
public void CommitAll() | |
{ | |
foreach (var repo in _repos) | |
{ | |
repo.Commit(); | |
} | |
} | |
public void RollbackAll() | |
{ | |
foreach (var repo in _repos) | |
{ | |
repo.Rollback(); | |
} | |
} | |
public IBaseRepository<T> GetRepository<T>() | |
where T : IBaseType | |
{ | |
var repo = CreateRepository(typeof(T)); | |
_repos.Add(repo); | |
return (IBaseRepository<T>)repo; | |
} | |
} | |
interface IBaseType | |
{ | |
} | |
interface IBaseRepository | |
{ | |
void Commit(); | |
void Rollback(); | |
} | |
interface IBaseRepository<T> : IBaseRepository | |
where T : IBaseType | |
{ | |
T Get(); | |
} | |
interface IPage : IBaseType | |
{ | |
} | |
interface IPageRepository : IBaseRepository<IPage> | |
{ | |
} | |
sealed class Page : IPage | |
{ | |
} | |
sealed class PageRepository : IPageRepository | |
{ | |
public void Commit() | |
{ | |
} | |
public IPage Get() | |
{ | |
return new Page(); | |
} | |
public void Rollback() | |
{ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment