C#泛型约束与接口解耦API

时间:2015-09-17 18:23:07

标签: c# generics

在编写数据访问层时,我想将面向公众的API与具体的数据库实现分离。例如,假设我想使用MongoDb或Cassandra作为我的后备存储。

所以,我想要的是我的C#代码通过工厂方法等利用 IThingDao 。工厂将对应于实际的实现。

我有一些非常基本的界面和类样本来展示我希望实现的目标。对我来说不幸的是,这段代码产生了很多编译时错误,说这些类没有实现接口的成员。

以下定义我的数据对象:

public interface IBase
{
    Object Id { get; set; }
    DateTime CreatedDate { get; set; }    
}

public interface IBaseMongoDb : IBase
{
    // Common MongoDb related things used by all concrete implementations
}

public interface IBaseCassandra : IBase
{
    // Common Cassandra related things used by all concrete implementations
}

public interface IThing : IBase
{
    String Name { get; set; }
}

public abstract class AbstractBase
{
    public virtual Object Id { get; set; }
    public DateTime CreatedDate { get; set; }
}

public abstract class AbstractMongoDbBase : AbstractBase
{
    [BsonId]
    public override Object Id { get; set; }

    // Other specific MongoDb stuff
}

public abstract class AbstractCassandraBase : AbstractBase
{
    // Cassandra related stuff
}

public class ThingMongoDbImpl : AbstractMongoDbBase, IThing, IBaseMongoDb
{
    public String Name { get; set; }
}

public class ThingCassandraImpl : AbstractCassandraBase, IThing, IBaseCassandra
{
    public String Name { get; set; }
}

MongoDb和Cassandra数据对象有单独的基础摘要,因为每个实现都具有特定于一个与另一个的共同特征(例如注释等)。

以下是数据访问接口:

public interface IDao<T>
{
    T Save( T a_Value );
    void Update( T a_Value );
    void Delete( T a_Value );
    T Find( Object a_Key );
}

public interface IThingDao : IDao<IThing>
{
    IThing FindByName( String a_Name );
}

以下是MongoDb和Cassandra的实现:

public abstract class AbstractMongoDbDao<T> where T : IBaseMongoDb, new()
{
    public T Save( T a_Value )
    {
        // Save
        return a_Value;
    }
    public void Update( T a_Value )
    {
        // Update
    }
    public void Delete( T a_Value )
    {
        // Delete
    }
    public T Find( Object a_Key )
    {
        return new T();
    }
}

public class ThingDaoMongoDbImpl : AbstractMongoDbDao<ThingMongoDbImpl>, IThingDao
{
    public IThing FindByName( String a_Name )
    {
        // Do the lookup and return value
        return new ThingMongoDbImpl();
    }
}


public abstract class AbstractCassandraDao<T> where T : IBaseCassandra, new()
{
    public T Save( T a_Value )
    {
        // Save
        return a_Value;
    }
    public void Update( T a_Value )
    {
        // Update
    }
    public void Delete( T a_Value )
    {
        // Delete
    }
    public T Find( Object a_Key )
    {
        return new T();
    }
}

public class ThingDaoCassandraImpl : AbstractCassandraDao<ThingCassandraImpl>, IThingDao
{
    public IThing FindByName( String a_Name )
    {
        // Do the lookup and return value
        return new ThingCassandraImpl();
    }
}

对于此代码, ThingDaoMongoDbImpl ThingDaoCassandraImpl 会生成以下编译时错误:

Error   1   'generics.decouple.ThingDaoMongoDbImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Find(object)'. 'generics.decouple.AbstractMongoDbDao<generics.decouple.ThingMongoDbImpl>.Find(object)' cannot implement 'generics.decouple.IDao<generics.decouple.IThing>.Find(object)' because it does not have the matching return type of 'generics.decouple.IThing'.
Error   2   'generics.decouple.ThingDaoMongoDbImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Delete(generics.decouple.IThing)' C:\Projects\EDC\modules\EXCHANGES\CAD2CAD.Net\Z\Generics.cs 95  18  Z
Error   3   'generics.decouple.ThingDaoMongoDbImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Update(generics.decouple.IThing)' C:\Projects\EDC\modules\EXCHANGES\CAD2CAD.Net\Z\Generics.cs 95  18  Z
Error   4   'generics.decouple.ThingDaoMongoDbImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Save(generics.decouple.IThing)'   C:\Projects\EDC\modules\EXCHANGES\CAD2CAD.Net\Z\Generics.cs 95  18  Z
Error   5   'generics.decouple.ThingDaoCassandraImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Find(object)'. 'generics.decouple.AbstractCassandraDao<generics.decouple.ThingCassandraImpl>.Find(object)' cannot implement 'generics.decouple.IDao<generics.decouple.IThing>.Find(object)' because it does not have the matching return type of 'generics.decouple.IThing'.
Error   6   'generics.decouple.ThingDaoCassandraImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Delete(generics.decouple.IThing)'   C:\Projects\EDC\modules\EXCHANGES\CAD2CAD.Net\Z\Generics.cs 126 18  Z
Error   7   'generics.decouple.ThingDaoCassandraImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Update(generics.decouple.IThing)'   C:\Projects\EDC\modules\EXCHANGES\CAD2CAD.Net\Z\Generics.cs 126 18  Z
Error   8   'generics.decouple.ThingDaoCassandraImpl' does not implement interface member 'generics.decouple.IDao<generics.decouple.IThing>.Save(generics.decouple.IThing)' C:\Projects\EDC\modules\EXCHANGES\CAD2CAD.Net\Z\Generics.cs 126 18  Z

有关如何使其正常工作的任何建议吗?

由于

1 个答案:

答案 0 :(得分:0)

public class ThingDaoMongoDbImpl : AbstractMongoDbDao<ThingMongoDbImpl>, IThingDao
{
    public IThing FindByName( String a_Name )
    {
        // Do the lookup and return value
        return new ThingMongoDbImpl();
    }

     T Save( T a_Value ) { //implement it
     }
     void Update( T a_Value ) { 
     //implement it here
     }
     void Delete( T a_Value ) { // implement it here}
     T Find( Object a_Key ) { //implement it}
}

您收到编译时错误,因为您没有实现IThingDao接口的方法。 您的public interface IThingDao : IDao<IThing>,来自IDao,因此您必须实施IDao<IThing>的方法

更新

  public class ThingCassandraImpl : AbstractCassandraBase, IThing, IBaseCassandra, IThingDao
    {
        public String Name { get; set; }
        public IThing Save(IThing a_Value)
        {
            throw new NotImplementedException();
        }

        public void Update(IThing a_Value)
        {
            throw new NotImplementedException();
        }

        public void Delete(IThing a_Value)
        {
            throw new NotImplementedException();
        }

        public IThing Find(object a_Key)
        {
            throw new NotImplementedException();
        }

        public IThing FindByName(string a_Name)
        {
            throw new NotImplementedException();
        }
    }
    public abstract class AbstractCassandraDao<T> where T : IBaseCassandra,IThingDao, new() 
    {
        public T Save(T a_Value)
        {
            // Save
            return a_Value;
        }
        public void Update(T a_Value)
        {
            // Update
        }
        public void Delete(T a_Value)
        {
            // Delete
        }
        public T Find(Object a_Key)
        {
            return new T();
        }
    }

    public class ThingDaoCassandraImpl : AbstractCassandraDao<ThingCassandraImpl>
    {
        public IThing FindByName(String a_Name)
        {
            // Do the lookup and return value
            return new ThingCassandraImpl();
        }

        public IThing Save(IThing a_Value)
        {
            throw new NotImplementedException();
        }

        public void Update(IThing a_Value)
        {
            throw new NotImplementedException();
        }

        public void Delete(IThing a_Value)
        {
            throw new NotImplementedException();
        }

        public IThing Find(object a_Key)
        {
            throw new NotImplementedException();
        }
    }