根据环境更改接口的实现 [英] Changing implementation of interface depending on environment

查看:49
本文介绍了根据环境更改接口的实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用接口IBackend与后端通信的应用程序.在生产环境中,我希望使用类ProdBackend : IBackend作为接口的实现.在测试环境中,我希望使用TestBackend : IBackend.

I have an application which uses an interface IBackend for communicating with a backend. In the production environment I wish to use class ProdBackend : IBackend as the implementation of the interface. In the test environment I wish to use TestBackend : IBackend.

该应用程序打包为一个zip文件,该文件必须与部署在产品环境或测试环境中无关.

The application is packaged into a zip file, which must be independent of whether it is deployed in the prod or test environment.

如何根据应用程序部署的环境,使应用程序使用不同的IBackend实现?

How can I make the application use a different implementation of IBackend depending on the environment it is deployed in?

是否可以通过在两个环境中简单地安装不同的.dll并命名相同的类来做到这一点?

Can I do this by simply having to different .dll's installed in the two environments and naming the classes the same?

更新11:12-15/1 : 打包的应用程序不允许包含prod实现,即ProdBackend : IBackend.因此,应用程序在编译时不知道ProdBackend : IBackend.

UPDATE 11:12 - 15/1: The packaged application is not allowed to include the prod implementation, i.e. ProdBackend : IBackend. So the application does not know ProdBackend : IBackend at compile time.

推荐答案

为不同的DLL提供相同接口的不同实现会很好.

Supplying different DLL's with different implementations of the same interfaces would work fine.

一个更优雅的解决方案是这样做,但要添加一个提供您所需实现的Service Locator.然后,您可以配置服务定位器以基于您所运行的平台返回实现.

An more elegant solution would be to do this but to add a Service Locator that supplies the implementation you need. Then you could configure the Service Locator to return an implementation based on the platform you run on.

有很多服务定位器,但是一个在Mono支持的所有平台上运行的简单定位器是 TinyIOC

There are many service locators out there, but a simple one that runs on all platforms supported by Mono is TinyIOC

这篇关于根据环境更改接口的实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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