K
best. Teams must be evaluated through a variety of other ranking components that are not part of the component being developed, and communicate through DI. The average for all rangers should be taken as a score for the team. public interface ITeam { string Name { get; } }
public interface IRanker { int Rank(ITeam team); }
public interface ITopRecord { ITeam Team { get; } int Position { get; } double AverageRank { get; } }
public interface ITopBuilder { IEnumerable<ITopRecord> BuildTop(IEnumerable<ITeam> teams, int topCount); }
public interface IRankerProvider { IEnumerable<IRanker> GetRankers(); }
IRanker
( IRanker
) so that they do their work. The implementation of the IRankerProvider
interface lies with the user who knows which ranking ITopRecord
should be used, because we only have ITopRecord
and ITopBuilder
. You can also make a composite of the rangers. Do, hand over and forget. public interface IRanker { int Rank(ITeam team,int teamCount); }
public interface IRanker { int Rank(ITeam team); } public interface IRankerWithCount { int Rank(ITeam team,int teamCount); }
ITopBuilder
.ITopBuilder
parameters to the ITopBuilder
: public interface IRanker { int Rank(ITeam team, IEnumerable<ITeam> teams, int topCount); }
IEnumerable
and Int32
instances into container is bad. Wrap them in the appropriate interfaces: public interface ITeamCollection:IEnumerable<ITeam> { } public interface ITopCount { int Count { get; } }
public interface ITop : IEnumerable<ITopRecord> { }
ITopBuilder
component will look like this: public interface ITopBuilder { ITop BuildTop(ITeamCollection teams, ITopCount count); }
public ITop BuildTop(ITeamCollection teams, ITopCount count) { using (var container = _container.CreateChildContainer()) { container.RegisterInstance(teams); container.RegisterInstance(count); //do work } }
ITeamCollection
and ITopCount
rankings to be in the constructor. And, therefore, the code will not work. This is because IRankerProvider
resolved in the IRankerProvider
constructor, and it is called before we register our instances. You can solve this problem by delegating work to another object that will be created each time BuildTop
call BuildTop
: public ITop BuildTop(ITeamCollection teams, ITopCount count) { using (var container = _container.CreateChildContainer()) { container.RegisterInstance(teams); container.RegisterInstance(count); return container.Resolve<BuilerWorker>().DoWork(); } } internal sealed class BuilerWorker { public BuilerWorker( ITeamCollection teams, ITopCount count, IRankerProvider rankerProvider ... ) { } public ITop DoWork() { ... } }
Source: https://habr.com/ru/post/177075/
All Articles