I just finished Mark Seemann's book “Dependency Injection” in .NET, and now I'm trying to refactor some old code. (At this stage, I do not rely on any specific DI container, but simply try to move all the dependencies to one place).
I consider the following factory class, which defines ArchiveTypeby reading the first few bytes of the archive with archiveReader.GetArchiveType(), and then returns an instance ArchiveRestorerbased on the enumeration ArchiveType.
public class ArchiveRestorerFactory : IArchiveRestorerFactory
{
public ArchiveRestorer Create(ArchiveReader archiveReader)
{
ArchiveType type = archiveReader.GetArchiveType();
switch (type)
{
case ArchiveType.CurrentData:
return new CurrentDataArchiveRestorer(archiveReader);
break;
case ArchiveType.HistoricalData:
return new HistoricalDataArchiveRestorer(archiveReader);
break;
case ArchiveType.AuditTrail:
return new AuditTrailArchiveRestorer(archiveReader);
break;
default:
throw new Exception("ArchiveRestorerFactory error: Unknown value for ArchiveType.");
}
}
}
How can I reorganize it so that the class does not depend on specific types CurrentDataArchiveRestorer, HistoricalDataArchiveRestorerand AuditTrailArchiveRestorer?
Should I move three specific restorers to the factory constructor?
public ArchiveRestorer Create(ArchiveReader archiveReader,
ArchiveRestorer currentDataArchiveRestorer,
ArchiveRestorer historicalDataArchiveRestorer,
ArchiveRestorer auditTrailDataArchiveRestorer)
{
}
, , ? , 20 ?
, factory , new .
?