Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否根据配置有条件地返回带有Locator.CurrentMutable.Register的对象?_C#_Reactiveui - Fatal编程技术网

C# 是否根据配置有条件地返回带有Locator.CurrentMutable.Register的对象?

C# 是否根据配置有条件地返回带有Locator.CurrentMutable.Register的对象?,c#,reactiveui,C#,Reactiveui,我有一个应用程序,我想从文件系统或Amazon S3加载/保存附件,这取决于它是在云中运行还是在本地桌面PC上运行 以下是我到目前为止所写的内容: public class AppBootstrapper { public bool Cloud { get; set; } public AppBootstrapper() { Cloud = false; RegisterDependencies(); } public

我有一个应用程序,我想从文件系统或Amazon S3加载/保存附件,这取决于它是在云中运行还是在本地桌面PC上运行

以下是我到目前为止所写的内容:

public class AppBootstrapper
{
    public bool Cloud { get; set; }

    public AppBootstrapper()
    {
        Cloud = false;

        RegisterDependencies();
    }

    public void RegisterDependencies()
    {
        Locator.CurrentMutable.Register( () => {
            if ( Cloud ) return new AmazonS3Repo();

            return new FileRepo() as IAttachmentRepo;
        }, typeof(IAttachmentRepo) );
    }
}
当然,这意味着您必须将
AppBootstrapper
设置为单例(以便设置
Cloud
属性),并且依赖项反转的好处将丢失。如果我能做到这一点,那就太好了:

Locator.CurrentMutable.Register<bool>( cloud => {
    if ( cloud ) return new AmazonS3Repo();

    return new FileRepo() as IAttachmentRepo;
})
Locator.CurrentMutable.Register(云=>{
if(cloud)返回新的AmazonS3Repo();
将新文件repo()作为IAttachmentRepo返回;
})

然而,我想我说的是依赖注入(而不是
Splat
的设计目的)。是否有其他方法可以使用
Splat

是的,您只需要使用Register和GetService的contract参数

后来

var service = Locator.Current.GetService(typeof(IAttachmentRepo), cloud ? "AmazonS3" : "FileSystem");

希望这对你有用

我把它作为一个评论放在第一位,以确保它符合你的需要。您是否尝试过使用Register的contract参数?寄存器(()=>newamazons3repo(),“AmazonS3”,typeof(IAttachmentRepo))和寄存器(()=>newfilerepo(),“FileRepo”,typeof(IAttachmentRepo)),然后Locator.Current.GetService(typeof(IAttachmentRepo),“AmazonS3”)否!?你是说我可以做这个-
Locator.Current.GetService(cloud?“AmazonS3”:“文件系统”)
?是的,检查一下:酷。这看起来就像我之后做的那样,我想我最终会创建一个存储设置的服务,这样我就可以打这个电话了。但是
contract
参数允许这样做(不使用单例)好吧,酷。很高兴听你这么说,你走对了方向。
var service = Locator.Current.GetService(typeof(IAttachmentRepo), cloud ? "AmazonS3" : "FileSystem");