重构SOLID原则

时间:2016-03-30 13:58:09

标签: c# solid-principles

在阅读了书籍和在线文章中的SOLID代码之后,我想重构一个现有的类,以便它可以与'SOLID'兼容。

但是我觉得我迷路了,尤其是依赖注入:当我想实例化类的对象时,我需要'注入'所有依赖项,但依赖项本身有依赖项..这就是我的地方开始迷路了。

这个想法是这样的:我想创建一个类(在我的例子中,一个简单的Amazon S3包装器类)来简单上传&获取网址操作。

如何正确使用接口和依赖注入?什么地方出了错? 班级应该怎么样?

这是我的代码:

public interface IConfigurationProvider
{
    string GetConfigurationValue(String configurationKey);
}

public interface ILogger
{
    void WriteLog(String message);
}

public interface IAWSClientProvider
{
    AmazonS3Client GetAmazonS3Client();
}

public interface IAWSBucketManager
{
    string GetDefaultBucketName();
}

public class AWSBucketManager : IAWSBucketManager
{
    ILogger logger;
    IConfigurationProvider configurationProvider;

    public AWSBucketManager(ILogger Logger, IConfigurationProvider ConfigurationProvider)
    {
        logger = Logger;
        configurationProvider = ConfigurationProvider;
    }

    public string GetDefaultBucketName()
    {
        string bucketName = string.Empty;

        try
        {
            bucketName = configurationProvider.GetConfigurationValue("Amazon_S3_ExportAds_BucketName");
        }
        catch (Exception ex)
        {
            logger.WriteLog(String.Format("getBucketName : Unable to get bucket name from configuration.\r\n{0}", ex));
        }

        return bucketName;
    }
}

public class AWSClientProvider : IAWSClientProvider
{
    IConfigurationProvider configurationProvider;
    IAWSBucketManager awsBucketManager;
    ILogger logger;

    private string awsS3BucketName;
    private Dictionary<string, RegionEndpoint> regionEndpoints;

    public AWSClientProvider(IConfigurationProvider ConfigurationProvider, IAWSBucketManager BucketManager, ILogger Logger)
    {
        logger = Logger;
        configurationProvider = ConfigurationProvider;
        awsBucketManager = BucketManager;
    }

    private RegionEndpoint getAWSRegion()
    {
        RegionEndpoint regionEndpoint = null;
        // Init endpoints dictionary
        try
        {
            IEnumerable<RegionEndpoint> regions = RegionEndpoint.EnumerableAllRegions;
            regionEndpoints = regions.ToDictionary(r => r.SystemName, r => r);
        }
        catch (Exception Ex)
        {
            logger.WriteLog(String.Format("getAWSRegion() - Failed to get region list from AWS.\r\n{0}", Ex));
            throw;
        }
        // Get configuration value
        try
        {
            string Config = configurationProvider.GetConfigurationValue("Amazon_S3_Region");
            if (String.IsNullOrEmpty(Config))
            {
                throw new Exception("getAWSRegion() : Amazon_S3_Region must not be null or empty string.");
            }

            regionEndpoint = regionEndpoints[Config];
        }
        catch (Exception Ex)
        {
            logger.WriteLog(String.Format("getAWSRegion() : Unable to get region settings from configuration.\r\n{0}", Ex));
            throw Ex;
        }

        return regionEndpoint;
    }

    private AWSCredentials getAWSCredentials()
    {
        string accessKey, secretKey;
        BasicAWSCredentials awsCredentials;

        try
        {
            accessKey = configurationProvider.GetConfigurationValue("Amazon_S3_AccessKey");
            secretKey = configurationProvider.GetConfigurationValue("Amazon_S3_SecretKey");
        }
        catch (Exception Ex)
        {
            logger.WriteLog(String.Format("getAWSCredentials() - Unable to get access key and secrey key values from configuration.\r\n", Ex.Message));
            throw;
        }

        try
        {
            awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        }
        catch (Exception Ex)
        {
            logger.WriteLog(String.Format("getAWSCredentials() - Unable to create basic AWS credentials object.\r\n{0}", Ex.Message));
            awsCredentials = null;
            throw;
        }

        return awsCredentials;
    }

    public AmazonS3Client GetAmazonS3Client()
    {
        AmazonS3Client client = null;
        RegionEndpoint region = getAWSRegion();
        AWSCredentials credentials = getAWSCredentials();
        awsS3BucketName = awsBucketManager.GetDefaultBucketName();

        if (credentials != null)
        {
            client = new AmazonS3Client(credentials, region);
        }
        return client;
    }
}

public class AWSS3Actions
{
    IConfigurationProvider configurationProvider;       // decoupling getting configuration
    ILogger logger;                                     // decoupling logger
    IAWSClientProvider awsClientProvider;

    private const int defaultExpirationDays = 14;

    public AWSS3Actions(IConfigurationProvider ConfigurationProvider, ILogger Logger, IAWSClientProvider ClientProvider)
    {
        configurationProvider = ConfigurationProvider;
        logger = Logger;
        awsClientProvider = ClientProvider;
    }

    #region Private Mmethods

    private string getFileUrl(string fileName, int expirationDaysPeriod, string awsS3BucketName)
    {
        GetPreSignedUrlRequest request = new GetPreSignedUrlRequest();
        string URL = "";

        DateTime dtBase = new DateTime();
        dtBase = DateTime.Now;
        dtBase = dtBase.AddDays(expirationDaysPeriod);

        request.BucketName = awsS3BucketName;
        request.Key = fileName;
        request.Expires = dtBase;

        try
        {
            URL = awsClientProvider.GetAmazonS3Client().GetPreSignedURL(request);
        }
        catch (AmazonS3Exception ex)
        {
            // log
            logger.WriteLog(String.Format("getFileUrl() : Could not get presigned URL for the provided request.\r\n{0}", ex));
            throw ex;
        }

        return URL;
    }

    private int getDefaultURLExpiration()
    {
        int expirationDays = 0;
        try
        {
            // set the time span in days
            int.TryParse(configurationProvider.GetConfigurationValue("getDefaultURLExpiration() : Amazon_S3_ExportAds_ExpirationDaysOfURL"), out expirationDays);     // get from configuration util
        }
        catch
        {
            // in case of exception, set the min 14 days time space exiration
            expirationDays = defaultExpirationDays;
        }
        return expirationDays;
    }

    private void validateUpload(string fileName, Stream fileStream)
    {
        if (fileName == null || fileName.Equals(string.Empty) || fileStream.Length < 1)
        {
            throw new Exception("fileName : File name must not be an empty string.");
        }
        if (fileStream == null)
        {
            throw new Exception("fileStream : Input memory stream (file stream) must not be null.");
        }
    }

    #endregion

    #region Public methods

    public bool IsFileExists(string fileName, string awsS3BucketName)
    {
        bool fileExists = false;
        try
        {
            S3FileInfo fileInfo = new S3FileInfo(awsClientProvider.GetAmazonS3Client(), awsS3BucketName, fileName);
            fileExists = fileInfo.Exists;
        }
        catch (AmazonS3Exception Ex)
        {
            // log
            logger.WriteLog(String.Format("isFileExists() : Could not determine if file (key) exists in S3 Bucket.\r\n", Ex.Message));
            throw;
        }
        return fileExists;
    }

    public bool UploadObject(string fileName, Stream fileStream, string awsS3BucketName)
    {
        bool uploadResult = true;
        // Validate input parameters
        validateUpload(fileName, fileStream);

        if (awsClientProvider.GetAmazonS3Client() != null)
        {
            try
            {
                PutObjectRequest request = new PutObjectRequest
                {
                    BucketName = awsS3BucketName,
                    Key = fileName,
                    InputStream = fileStream
                };

                PutObjectResponse response = awsClientProvider.GetAmazonS3Client().PutObject(request);
                if (response != null)
                {
                    if (response.HttpStatusCode != System.Net.HttpStatusCode.OK)
                    {
                        var meta = response.ResponseMetadata.Metadata.Keys.
                            Select(k => k.ToString() + " : " + response.ResponseMetadata.Metadata[k]).
                            ToList().Aggregate((current, next) => current + "\r\n" + next);

                        // log error
                        logger.WriteLog(String.Format("Status Code: {0}\r\nETag : {1}\r\nResponse metadata : {1}",
                            (int)response.HttpStatusCode, response.ETag, meta));

                        // set the return value
                        uploadResult = false;
                    }
                }
            }
            catch (AmazonS3Exception ex)
            {
                uploadResult = false;

                if (ex.ErrorCode != null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
                {
                    // LOG
                    logger.WriteLog(String.Format("UploadObject() : invalied credentials"));
                    throw ex;
                }
                else
                {
                    // LOG
                    logger.WriteLog(String.Format("UploadObject() : Error occurred. Message:'{0}' when writing an object", ex.Message));
                    throw ex;
                }
            }
        }
        else
        {
            throw new Exception("UploadObject() : Could not start object upload because Amazon client is null.");
        }

        return uploadResult;
    }

    public bool UploadObject(string subFolderInBucket, string FileName, Stream fileStream, string awsS3BucketName)
    {
        return UploadObject(subFolderInBucket + @"/" + FileName, fileStream, awsS3BucketName);
    }

    public string GetURL(string fileName, string bucket)
    {
        string url = string.Empty;
        try
        {
            if (IsFileExists(fileName, bucket))
            {
                url = getFileUrl(fileName, getDefaultURLExpiration(), bucket);
            }
        }
        catch (Exception Ex)
        {
            // log
            logger.WriteLog(String.Format("getURL : Failed in isFileExists() method. \r\n{0}", Ex.Message));
        }
        return url;
    }

    #endregion
}

1 个答案:

答案 0 :(得分:3)

使用您当前的类结构,Composition Root可能如下所示:

var logger = new FileLogger("c:\\temp\\log.txt");
var configurationProvider = new ConfigurationProvider();

var actions = new AWSS3Actions(
    configurationProvider,
    logger,
    new AWSClientProvider(
        configurationProvider,
        new AWSBucketManagerlogger(
            logger,
            configurationProvider),
        logger));

上面的例子显示了一个手工对象图(a.k.a。Pure DI)。我的建议是首先应用Pure DI并切换到DI库(例如Simple Injector,Autofac或StructureMap),当手工构建对象图变得麻烦且维护繁重时。

从依赖注入的角度来看,尽管您的代码闻起来,但您正在做的事情看起来很明智。以下是一些参考资料:

旁注:通常,最好先预先加载配置值(在应用程序启动时),而不是在运行时读取它。在运行时读取这些值会导致以延迟方式读取这些值,从而防止应用程序快速失败,并在整个应用程序中扩展配置抽象的使用。如果可能,将这些原始配置值直接注入需要该值的类型的构造函数中。