如何创建动态返回模型

时间:2018-08-30 18:51:42

标签: c#

我想为存储过程创建一个库。我有一个StorageFactory继承的AbstractStorageFactory。但是我的配置类型根据ProviderType而变化。因此,我想使用动态的,但是在StorageFactory构造函数中遇到了“考虑转换动态参数”的错误。

internal abstract class AbstractStorageFactory
{
    public dynamic _configuration { get; set; }

    protected AbstractStorageFactory(dynamic configuration) => _configuration = configuration;
}
internal class StorageFactory: AbstractStorageFactory
{
    private static readonly dynamic configuration;

    public StorageFactory() : base(configuration) { }
    public ProviderType StorageProvider { get; set; }

    public override StorageStrategy GetStorageStrategy(ProviderType provider)
    {
        switch (provider)
        {
            case ProviderType.AzureStorage:
                var azureConfig = new AzureStorageConfiguration()
                {
                    AccountName = configuration.AccountName,
                    BlobServiceEndpoint = configuration.BlobServiceEndpoint,
                    Key = configuration.Key,
                    ContainerName = configuration.ContainerName
                };
                return new AzureStorageStrategy(azureConfig);
            case ProviderType.FTPStorage:
                var ftpConfig = new FTPConfiguration()
                {
                    Uri = configuration.Uri,
                    Username = configuration.Username,
                    Password = configuration.Password,
                    Port = configuration.Port,
                    EncryptionMode = configuration.EncryptionMode
                };
                return new FTPStrategy(ftpConfig);
            default: throw new Exception("Unexpected internal error occured");
        }
    }
}

1 个答案:

答案 0 :(得分:0)

也许是这样吗? 我试图模仿您的类(工厂,策略和配置)。但是我可能还是会以其他方式来做:)

def reset_password(email, from_email, template='registration/password_reset_email.html'):
    form = PasswordResetForm({'email':email})
    return form.save(from_email=from_email, email_template_name=template)