- 浏览: 402637 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
It is quite common that you might want to change the public interface. while as a lib developer, you should be familiar with some facts/information/lore/knowledgest that you should be aware of;
as we all know that the typical operations that we may do on an interface may falls into one/combination of the four categories.
- add a new member
- remove an existing member
- rename an existing member (you can think this is a special case of adding a new interface whilst removing an old one)
- changing the signature / changing the order of the members
some of them are safe, or relative safe? let's examine them in turn.
NOTE: in all the examination, we suppose that lib maintainer has control over both the interface and the impl to the interfaces. otherwise, we know it is a definite deal breaker to change but just a little bit of interface.
Add a new member
Suppose we have two project, one is called InterfaceLib, which represents /embodies a lib, which is maintained by a lib developer; and the other is called InterfaceApp, which represents an application that uses the InterfaceLib;
to test the compatibilty, we will build two version of the same lib, and we will build the InterfaceApp against the first version of InterfaceLib, but we will force to load the second version of the InterfaceLib at runtime;
So, suppose the first version of the IInterfaceLib is like this:
public interface IInterfaceLib { object Property1 { get; } } public class InterfaceLibImpl : IInterfaceLib { public object Property1 { get { return new object(); } } }
and below is the client's app in the InterfaceApp;
namespace InterfaceApp { class Program { // InterfaceApp is the consumer of the interface that is defined inside the // InterfaceLib // to test the compatibility // we better compile the version 1 of InterfaceLib, copied to somewhere // add the reference to lib from the copied address // later, we try to runtimely load the version 2 of the interfaceLib static void Main(string[] args) { IInterfaceLib lib = new InterfaceLibImpl(); if (null != lib.Property1) { Console.Write("You should always see this line"); } Console.Read(); } } }
now, suppose, the lib maintainer decide to add another property to the InterfaceLib, and add a new property called Name to the interface IInterfaceLib; we assume lib maintainer has all impl to that interface to his control (as in the precondition sectinon); now the IInterfaceLib has changed to this:
public interface IInterfaceLib { object Property1 { get; } string Name { get; } // in v2.0 we decide to add new property that is called Name } public class InterfaceLibImpl : IInterfaceLib // now v2.0 impl to IInterfaceLib { public object Property1 { get { return new object(); } } public string Name { get { return "InterfaceLibImpl"; } } }
we do not need to compile, InterfaceApp, just replace the reference with the new one, and then program run as usual. So adding a new member does not break the compatibility;
Let's relax the constraint a little bit; Let's suppose the use has written impl to the IInterfaceLib interface; (it is against the InterfaceLib v1.0)
// below is a client's impl to IInterfaceLib againt InterfaceLib v1.0 class AppInterfaceLibImpl : IInterfaceLib { object m_property1; public AppInterfaceLibImpl(object param) { m_property1 = param; } public object Property1 { get { return m_property1; } } }
and now the client's app is becoming this:
class Program { static void Main(string[] args) { IInterfaceLib lib = new InterfaceLibImpl(); if (null != lib.Property1) { Console.Write("You should always see this line"); } AppInterfaceLibImpl appImpl = new AppInterfaceLibImpl(lib.Property1); if (null != appImpl.Property1) { Console.Write("you shall see this as well"); } Console.Read(); } }
First, compile, it is OK, but what if we run? boom/bang!, it fails at runtime. Below is the common error that you get
Method 'get_Name' in type 'InterfaceApp.AppInterfaceLibImpl' from assembly 'InterfaceApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an implementation.
So a general rule for adding member to an interface is as follow,
NO, YOU DO NOT ADD MEMBER TO EXISTING INTERFACS!
But sometimes, you have to, how?
An always true rule for adding new mebers
All the discussion above has a big assumption, that the lib maintainer has the control over the impls to that interfaces, however, if the interface is a public interface, you cannot just simply took that no client would ever write impls for that interface, and if you add new members, you may break user's code;
So a common technique is to create a new interface, inherit the new interface, and add the new members there;
public interface IInterfaceLib { object Property1 { get; } } public interface IInterfaceLib2 : IInterfaceLib { string Name { get; } // in v2.0 we decide to add new property that is called Name }
the lib's impl will now can point to the new interface
public class InterfaceLibImpl : IInterfaceLib2 // now v2.0 impl to IInterfaceLib { public object Property1 { get { return new object(); } } public string Name { get { return "InterfaceLibImpl"; } } }
and the client's impl stay the same;
This can satisify both the compiler and runtime (for both the client and lib developer)
Removing an existing member
Rename a member
change signaure / order
发表评论
-
wpf - example to enhance ComboBox for AutoComplete
2014-09-19 15:56 1981first let’s see an example ... -
Investigate and troubleshoot possible memory leak issue of .NET application
2014-07-31 10:42 0Hi All, I would like to sh ... -
C# – CoerceValueCallback合并、替换元数据值
2013-08-05 21:59 1934Topic: C# – CoerceValueCallbac ... -
wpf – ListView交替背景色
2013-07-02 20:56 6563Wpf – Alternate background col ... -
C# - 简单介绍TaskScheduler
2013-06-29 17:18 12061标题: C# - 简单介绍TaskSchedulerTit ... -
c# - Get enum from enum attribute
2013-06-27 21:32 1259DescriptionAttribute gives the ... -
C# - PInvoke, gotchas on the RegisterClassEx and the CreateWindowEx
2013-06-24 13:49 2582I get an exception message li ... -
c# - Use PInvoke to create simple win32 Application
2013-06-24 11:59 10969In this post, .net platform h ... -
c# - Linq's Select method as the Map function
2013-06-19 18:47 1301If you comes from a functiona ... -
c# - Tips of Linq expression Any to determine if a collection is Empty
2013-06-19 18:29 951When you are @ the linq expres ... -
myth buster - typeof accepting array of types not acceptable
2013-06-19 17:17 824I have seen from some book whe ... -
windows - trying to create WIN32 application with PInvoke
2013-06-19 14:34 0While it is stupid to do such ... -
WPF - Setting foreground color of Entire window
2013-06-13 16:00 1932You might as well as I would s ... -
WPF - Enhanced TabControl - TabControlEx aka Prerendering TabControl
2013-06-13 13:12 5345As an opening word, let's che ... -
wpf - ControlTemplate and AddLogicChild/RemoveLogicalChild
2013-06-10 15:42 1196Recently I was trying to debug ... -
c# - P/Invoke, DllImport, Marshal Structures and Type conversions
2013-06-05 15:25 1722P/Invoke as in the following q ... -
c# - A study on the NativeWindow - encapsulate window handle and procedure
2013-06-05 14:40 6105NativeWindow gives you a way t ... -
WCF - Notify server when client connects
2013-06-03 18:19 1232It is sometimes very importan ... -
wcf - Debug to enable Server exception in Fault message
2013-06-03 15:47 1105WCF will be able to send back ... -
c# - determine if a type/object is serialzable
2013-05-30 16:35 873In WCF, primitives type are s ...
相关推荐
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.7.0.jar; 赠送原API文档:lucene-backward-codecs-7.7.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-7.7.0.jar; 赠送原API文档:lucene-backward-codecs-7.7.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
java运行依赖jar包
java运行依赖jar包
java运行依赖jar包
Microsoft SQL Server 2005 Backward Compatibility Components (Microsoft SQL Server 2005 向后兼容组件) SQL Server Backward Compatibility 包中包含 最新版本的 Data Transformation Services 2000 运行时 ...
本文档主要介绍了前向后向算法(Forward-Backward Algorithm),这是一种在隐藏马尔可夫模型(Hidden Markov Models, HMMs)和条件随机场(Conditional Random Fields, CRFs)中具有重要应用的动态规划算法。...
lucene-backward-codecs-8.8.2.jar
1、参考文献:Forward-Backward Error: Automatic Detection of Tracking Failures,文件附带参考文献 2、用matlab、C、Opencv 混合编程实现 3、遇到不兼容时,请在mex文件夹从新编译生成新的“mex”文件
针对这些问题,本文介绍了一种新颖的方法——**前后误差法(Forward-Backward Error Automatic Detection)**,用于自动检测跟踪过程中的失败情况。 #### 2. 前后误差法概述 该方法基于一个核心假设:如果跟踪是正确...
Forward-backward doubly stochastic differential equations with random jumps and stochastic partial differential-integral equations,朱庆峰,石玉峰,A type of forward-backward doubly stochastic ...
### 多模态循环融合:视频到语言及反向转换的研究 #### 摘要与背景 本研究提出了一种新的多模态融合方法——多模态循环融合(Multi-modal Circulant Fusion,简称MCF),旨在更深入地探索不同模态特征之间的交互...
Microsoft SQL Server 2005 Backward Compatibility Components (Microsoft SQL Server 2005 向后兼容组件) SQL Server Backward Compatibility 包中包含最新版本的 Data Transformation Services 2000 运行时 (DTS...