隐藏外部世界的内部服务,以确保使用正确的高级服务 [英] hiding internal services from outside world to ensure the correct high-level service is being used

查看:99
本文介绍了隐藏外部世界的内部服务,以确保使用正确的高级服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个电子商务网站上。我有包含属性和照片的广告实体。将属性写入数据库,并将照片存储在文件系统中。

I am working on an e-commerce website. I have advertisement entity which include both properties and photos. Properties are written to DB and photos are stored in file system.

我在基础设施项目中创建了 WriterService ,该服务负责保存广告...在引擎盖上应该知道属性进入数据库,照片进入文件系统...但是这个细节与外界无关...外界应该使用 WriterService 保存广告。

I have created a WriterService in my infrastructure project, this service is responsible to save an ad... under the hood it should know that properties go to DB and photos go to file system... but this details is irrelevant to the outside world... the outside world should use WriterService to save an ad.

这是我的作家服务:

public class WriterService
{
    private DbWriter _dbWriter;
    private IFileWriter _fileWriter;

    // I believe I need to change the constructor in order to achieve my goal
    public WriterService(DbWriter dbWriter, IFileWriter fileWriter)
    {
        _dbWriter = dbWriter;
        _fileWriter = fileWriter;
    }

    public void WriterSomething(string text, Stream image)
    {
        _dbWriter.Write(text);
        _fileWriter.Write(image);
    }
}

现在在我的基础架构层中,我已经实现了 DbWriter FileWriter DbWriter 看起来像这样:

Now in my infrastructure layer I have the implementation of DbWriter and FileWriter, DbWriter looks like this:

public DbWriter
{
    public void Write(string text) {/* write text to DB */}
}

FileWriter 可以有不同的选择实现:

FileWriter can have different implementations:

public interface IFileWriter
{
   void Write(Stream image);
}

照片可能会写入本地磁盘或AWS S3存储桶:

Photos may be writter to local disk or AWS S3 bucket:

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

I want to enforce the outside world (other projects in my solution) to use WriterService, so if they want to save some images they should not directly use AWSCloudWriter, they would always have to do this through WriterService. Is it possible to enforce this?

I为了避免一个很长的问题,我创建了这段代码回顾解释了我要解决的问题。

I order to avoid a very long question, I have created this code review explaining the problem that I am trying to solve.

推荐答案

我使用了适配器来映射最终的AWS / Disk编写者的中间类,从而将其隐藏在实现者中。

I used an adapter to map the intermediate class to the final AWS/Disk writers thus hiding them from the implementer.

所有这些代码都在单独的类库中

IFileWriter

IFileWriter

    internal interface IFileWriter
    {
        void Write(string text);
    }

两个实现IFileWriter的类

Two classes implementing the 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

Two adapters, one for AWS and another for the DiskWriter

    public class AWSFileWriterAdapter: AbstractFileWriterAdapter
    {
        public AWSFileWriterAdapter()
        {
            FileWriter = new AWSWriter();
        }
    }



    public class DiskDriveWriterAdapter:AbstractFileWriterAdapter
    {
        public DiskDriveWriterAdapter()
        {
            FileWriter = new DiskDriveWriter();
        }
    }

WriterService

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!!");

这篇关于隐藏外部世界的内部服务,以确保使用正确的高级服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆