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

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. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值