Skip to content

Instantly share code, notes, and snippets.

@osya
Last active November 26, 2015 07:25
Show Gist options
  • Save osya/7884ccd1e7d72622850b to your computer and use it in GitHub Desktop.
Save osya/7884ccd1e7d72622850b to your computer and use it in GitHub Desktop.
Composition & Aggregation #CSharp
class CompositeCustomService
{
// Композиция
private readonly CustomRepository _repository
= new CustomRepository();
public void DoSomething()
{
// Используем _repository
}
}
class AggregatedCustomService
{
// Агрегация
private readonly AbstractRepository _repository;
public AggregatedCustomService(AbstractRepository repository)
{
_repository = repository;
}
public void DoSomething()
{
// Используем _repository
}
}
internal interface IRepositoryFactory
{
AbstractRepository Create();
}
class CustomService
{
// Композиция
private readonly IRepositoryFactory _repositoryFactory;
public CustomService(IRepositoryFactory repositoryFactory)
{
_repositoryFactory = repositoryFactory;
}
public void DoSomething()
{
var repository = _repositoryFactory.Create();
// Используем созданный AbstractRepository
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment