代码之家  ›  专栏  ›  技术社区  ›  mathieu

城堡与仿制药

  •  0
  • mathieu  · 技术社区  · 15 年前

    考虑到这一准则:

    interface IRepository<T>
    {
        void Save();
    }
    
    class Repository<T>
    {
        public virtual void Save() // something 
        { }
    }
    
    interface IOtherRepository : IRepository<OtherClass>
    {
        void Other();
    }
    
    class OtherRepository : Repository<OtherClass>, IOtherRepository
    {
        public override void Save() // something different
        { }
    
        public override void Other(){ }
    }
    

    如果温莎城堡不能做到这一点,哪些国际奥委会集装箱可以做到?

    1 回复  |  直到 15 年前
        1
  •  1
  •   Mauricio Scheffer    15 年前
    var container = new WindsorContainer();
    container.Register(Component.For(typeof(IRepository<>))
                                .ImplementedBy(typeof(Repository<>));
    container.Register(Component.For<IRepository<OtherClass>, IOtherRepository>()
                                .ImplementedBy<OtherRepository>());
    var repo = container.Resolve<IRepository<Something>>();
    Assert.IsInstanceOfType(typeof(Repository<Something>), repo);
    var specificRepo = container.Resolve<IRepository<OtherClass>>();
    Assert.IsInstanceOfType(typeof(OtherRepository), specificRepo);
    var otherRepo = container.Resolve<IOtherRepository>();
    Assert.AreSame(otherRepo, specificRepo);