`

c# :design of generic version of interface with its non generic verion - Part 2

    博客分类:
  • C#
c# 
阅读更多

This is an follow up discussion on the previous impl of the c# - design of generic version of interface with its non generic verion. In this example, I will introduce a more real world like example (however, stil this code need to be improved).

First is the declarations of the interfaces. 

  // boqwang: [placeholder]
  //   better to call "Duplexer"?
  interface IProfilePersistenceProxy
  {
    void HookPersistenceEvent(IPersistableStateOwner stateOwner);
  }

  interface IProfilePersistenceProxy<T> : IProfilePersistenceProxy
  {
    void HookPersistenceEvent(IPersistableEnumerableStateOwner<T> stateOwner); 
  }

 

and below is class definition 

  // boqwang: [placeholder]
  class RamboProfilePersistenceProxy : // boqwang - 
          IProfilePersistenceProxy
  {
    protected internal WpfApplicationPersistence persistence;

    public RamboProfilePersistenceProxy(
      WpfApplicationPersistence appPersistence
      )
    {
      this.persistence = appPersistence;
    }

    public virtual void HookPersistenceEvent(IPersistableStateOwner stateOwner)
    {
      // boqwang - Q?
      // is it possible to determine if  a
      if (stateOwner == null) throw new ArgumentNullException("stateOwner");

      Trace.WriteLine("RamboProfilePersistenceProxy");
      Type type = stateOwner.GetType();
      if (type.GetGenericTypeDefinition() == typeof(IPersistableEnumerableStateOwner<>))
      {
        // ....
      }
      throw new NotImplementedException();
    }


  }

  class RamboProfilePersistenceProxy<T> : RamboProfilePersistenceProxy, IProfilePersistenceProxy<T>, IProfilePersistenceProxy
  {

    public RamboProfilePersistenceProxy(
      WpfApplicationPersistence appPersistence
      )
      : base(appPersistence)
    {
    }

    public override void HookPersistenceEvent(IPersistableStateOwner stateOwner)
    {
      Trace.WriteLine("RamboProfilePersistenceProxy<T>");
      if (stateOwner is IPersistableEnumerableStateOwner<T>)
      {
        HookPersistenceEvent((IPersistableEnumerableStateOwner<T>) stateOwner);
      }
      else
      {
        base.HookPersistenceEvent(stateOwner);
      }
    }


    void IProfilePersistenceProxy.HookPersistenceEvent(IPersistableStateOwner stateOwner)
    {
      this.HookPersistenceEvent(stateOwner);
    }


    public void HookPersistenceEvent(IPersistableEnumerableStateOwner<T> stateOwner)
    {
      if (stateOwner == null) throw new ArgumentNullException("stateOwner");

      stateOwner.StateSaving += new EventHandler<PersistableEnumerableStateSavingEventArgs<T>>(stateOwner_StateSaving);
      stateOwner.StateLoaded += new EventHandler<PersistableEnumerableStateLoadedEventArgs<T>>(stateOwner_StateLoaded);

      if (persistableStateOwners.FirstOrDefault(p => p.GetType() == stateOwner.GetType()) == null)
      {
        persistableStateOwners.Add(stateOwner);
      } // - boqwang: silently/quitely/noiselessly/soundlessly/stillnessly ignore. 
    }

    List<IPersistableStateOwner> persistableStateOwners = new List<IPersistableStateOwner>();

    void stateOwner_StateLoaded<T>(object sender, PersistableEnumerableStateLoadedEventArgs<T> e)
    {
      throw new NotImplementedException();
    }

    void stateOwner_StateSaving<T>(object sender, PersistableEnumerableStateSavingEventArgs<T> e)
    {
      throw new NotImplementedException();
    }
  }

 

The things that I wantted to highlight here is that the Generic class has impl both the the generic interface and the non-generic interface, also it tries to override the interfaces that is inherited from its base classs, a non-generic class. 

分享到:
评论

相关推荐

    temporary-add-to-verion-control:将现有的R Project与Github关联起来

    2. **配置Git** - 在命令行或者终端中,使用以下命令配置你的用户名和邮箱: ``` git config --global user.name "Your Name" git config --global user.email "your-email@example.com" ``` 3. **创建新的R ...

    VC资源文件版本更新工具 (与Subversion版本同步)

    Mark the revision of verion info in a rc file as svn revision of current working directory. Usage: VersionMaker [-?] -rc rc-file -? display this help. -rc rc-file specify the .rc file name. ...

    ARMCC Compiler Verdion 5编译器

    2. **架构支持**:ARMCC支持多种ARM架构,包括Cortex-M、Cortex-A和Cortex-R系列,能够针对不同处理器进行优化编译。 3. **多语言支持**:除了C和C++,还支持ARM汇编语言,允许开发者充分利用硬件特性。 4. **调试...

    RemoveSymantecMacFiles.command

    This is the default as of verion 7.0.73. -F List only regular files that are currently installed and that would be deleted. No note is added if a file is not supposed to be removed by Symantec ...

    这是一个基于龙芯处理器v2.12.92的定制qemu verion

    这是一个基于龙芯处理器v2.12.92的定制qemu verion。目前它支持以下龙芯板:福龙2e 福龙2E迷你电脑:LS1A MIPS LS1A 平台。LS1B MIPS LS1B 平台。LS1C MIPS LS1C 平台。LS232 MIPS LS232 平台。LS2F1A MIPS LS2F1A ...

    HX3313_Datesheet_verion1.2.pdf

    根据提供的文件信息,“HX3313_Datesheet_verion1.2.pdf”是一份关于HX3313心率传感器的数据表文档。该传感器由南京天亿合鑫电子有限公司制造,具有超低功耗、光学心率监测及接近检测等功能,并支持I2C接口。以下是...

    centos7.x-ssh9.3p1-ssl1.1.1u一键升级脚本

    2、隐藏openssh-版本号 3、保留scp与ssh-copy-id命令 安装: 执行 bash upgrade_ssl_ssh.sh 进行安装 验证: openssh 版本: (已带有openssl版本) [root@localhost ~]# ssh -V OpenSSH_9.3p1, OpenSSL 1.1.1u 30...

    VSIPL verion1.4

    VSIPL(Very Short Instruction Word Instruction Set)是一种为信号处理应用设计的指令集标准,它的版本1.4是一个在2013年发布的软件协议标准。在本文中,我们将详细解读VSIPL版本1.4软件协议中的主要知识点,包括...

    py-sdl2:SDL2周围的Python ctypes包装器

    PySDL2是围绕SDL2,SDL2_mixer,SDL2_image,SDL2_ttf和SDL2_gfx库的纯Python包装。 它不依赖C代码,而是使用内置的ctypes模块与SDL2进行接口,并提供简单的Python类和用于通用SDL2功能的包装器。 安装 PySDL2易于...

    Bandicam Version 2.X Cracked

    Bandicam ver 2 Cracked ...2. Extract the zip file 3. Run as Administrator with "Bandicam 2.X Crack.exe" 4. Click on Crack Bandicam for ver 2.0 or high and Register Bandicam for verion 1.0 to 1.9

    version.js:使用查询字符串的开关测试其他脚本版本

    注意:该项目已弃用,不再维护。 如果您想继续支持分支版本,请在Twitter( )上进行搜索以在此处列出它。 版本.js 使用查询字符串的开关测试不同的脚本版本。...使用Version.js并将您的设置简化为单个测试运行器:

    GAMIT/GLOBK MATLAB TOOLS

    When running from MATLAB this verion of the m-files should be downloaded. The m-files are in a folder called matlab and in this folder are folders for tsview and velview. To run the velview and ...

    华硕飞行堡垒BLOS 301Verion

    华硕飞行堡垒BLOS 301稳定版本升级,解决游戏卡顿等问题,该版本已测试确认稳定。

    logsentinel-java-client:logsentinel.com的Java API

    &lt; version&gt;0.2.0&lt;/ verion&gt; Gradle用户 将此依赖项添加到项目的构建文件中: compile " com.logsentinel:logsentinel-java-client:0.0.7 " 入门 获得客户端依赖性后,可以按以下方式使用它: public class ...

    SQLSysClrTypes.msi安装ReportViewer2015前必须要安装的软件

    SQLSysClrTypes.msi,安装ReportViewer2015前必须要安装的软件

    ccna1 exam

    【描述】"CCNA1 Exam Verion1 and Version4" 提示我们关注的是两个不同版本的考试内容。Version1可能是较早期的考试格式,而Version4则代表了更新的考试大纲,可能涵盖了更现代的网络技术和协议。这两个版本可能在...

    addflow(from 1999)_verion3.0_ActiveX

    AddFlow ActiveX ControlCopyright (c) 1997-99 Lassalle TechnologiesLicense number: 1887 7799 2978 2253

    Bumblebee-Optimus:大WaSP擎天柱的GUI

    该项目不再维护 ... 警告:将Verion更新为0.8 beta 2时,您必须稍后运行版本0.8的脚本安装程序才能删除 依赖PyQt4: 对于Ubuntu:sudo apt-get install python-qt4-dev 对于Fedora:sudo yum安装PyQt4-devel

    zanpen2000#QJDocuments#版本号控制规范 1.11

    版本号命名格式格式:Major_Version_Number.Minor_Verion_Number[.Patch_Number]说明:主版本号.次版本号[.修

    goderive:在您的类型上方添加派生注释,并为标记的类型生成源代码-Source code collection

    $ goderive --verion # show git version information Version: xxxxxxx 用法 $ goderive help GoDerive Add derive comment above your type, and generate source code for the marked type. Comm

Global site tag (gtag.js) - Google Analytics