我的基础结构项目中有一个WriterService
:
public class WriterService
{
private DbWriter _dbWriter;
private IFileWriter _fileWriter;
public WriterService(DbWriter dbWriter, IFileWriter fileWriter)
{
_dbWriter = dbWriter;
_fileWriter = fileWriter;
}
public void WriterSomething(string text, Stream image)
{
_dbWriter.Write(text);
_fileWriter.Write(image);
}
}
如您所见,WriterService
必须将一些文本写入DB,将某些图像写入文件系统。这是IFileWriter
界面:
public interface IFileWriter
{
void Write(Stream image);
}
现在我对文件系统编写器有不同的实现,可以将其写入本地磁盘驱动器或AWS云:
public DiskDriveWriter : IFileWriter
{
public void Write(Stream image) {/* write image to Disk */}
}
public AWSCloudWriter : IFileWriter
{
public void Write(Stream image) {/* write image to AWS */}
}
我想强制外界(我的解决方案中的其他项目)使用WriterService
,因此,如果他们想保存一些图像,则不应该直接使用AWSCloudWriter
,他们总是必须通过WriterService
来完成此操作。可以强制执行吗?
我使用适配器将中间类映射到最终的AWS / Disk编写器,从而将它们对实现者隐藏。
所有这些代码都在单独的类库中
IFileWriter
internal interface IFileWriter
{
void Write(string text);
}
两个实现IFileWriter的类
internal class AWSWriter : IFileWriter
{
public void Write(string text)
{
//Write to AWS
}
}
internal class DiskDriveWriter : IFileWriter
{
public void Write(string text)
{
//Write to disk
}
}
适配器的抽象基类
public abstract class AbstractFileWriterAdapter
{
internal IFileWriter FileWriter { get; set; }
}
两个适配器,一个用于AWS,另一个用于DiskWriter
public class AWSFileWriterAdapter: AbstractFileWriterAdapter
{
public AWSFileWriterAdapter()
{
FileWriter = new AWSWriter();
}
}
public class DiskDriveWriterAdapter:AbstractFileWriterAdapter
{
public DiskDriveWriterAdapter()
{
FileWriter = new DiskDriveWriter();
}
}
WriterService
public class WriterService
{
AbstractFileWriterAdapter _writer;
public WriterService(AbstractFileWriterAdapter writer)
{
_writer = writer;
}
public void WriteMessage(string text)
{
_writer.FileWriter.Write(text);
}
}
最后是来自其他项目的电话
var awsAdapter = new AWSFileWriterAdapter();
var service1 = new WriterService(awsAdapter);
service1.WriteMessage("Some fancy text to AWS!!");
var diskDriveAdapter = new DiskDriveWriterAdapter();
var service2 = new WriterService(diskDriveAdapter);
service2.WriteMessage("Some text to the drive!!");
[如果您创建IFileWriter
internal
,并且将使用您的接口的WriterService
的构造函数也应该设置为internal
,则将起作用。
internal interface IFileWriter
internal WriterService(IFileWriter fileWriter)
类似这样的东西:
internal interface IWriter
{
void Write(string text);
}
internal class WriterB : IWriter
{
public void Write(string text) { Console.WriteLine($"A is writing '{text}'"); }
}
internal class WriterA : IWriter
{
public void Write(string text) { Console.WriteLine($"B is writing '{text}'"); }
}
public class WriterService
{
private readonly IWriter x;
internal WriterService(IWriter x) { this.x = x; }
public void Write(string text) { x.Write(text); }
public static WriterService WithA() { return new WriterService(new WriterA()); }
public static WriterService WithB() { return new WriterService(new WriterB()); }
}
public class Program
{
public static void Main(string[] args)
{
var s = new WriterService(new WriterA());
s.Write("Hello!");
WriterService.WithA().Write("Hello again!");
WriterService.WithB().Write("And again!");
}
}