【问题标题】:Cannot implement interface member because it does not have the matching return type无法实现接口成员,因为它没有匹配的返回类型
【发布时间】:2015-03-04 09:45:06
【问题描述】:

我不确定问题出在哪里,但我收到以下编译器错误:

错误:

错误 3 'DataAccess.NHibernate.UnitOfWork.UnitOfWork' 没有 实现接口成员“Domain.Repository.UnitOfWork.IUow.DBC”。 'DataAccess.NHibernate.UnitOfWork.UnitOfWork.DBC' 无法实现 'Domain.Repository.UnitOfWork.IUow.DBC' 因为它没有 匹配的返回类型 'Domain.Repository.UnitOfWork.IDBContext'。 ..\DataAccess.NHibernate\UnitOfWork\UnitOfWork.cs 12 11 DataAccess.NHibernate

下面是我的代码

UnitOfWork.cs

namespace DataAccess.NHibernate.UnitOfWork
{
    class UnitOfWork : IUow, IDisposable
    {
        public DBContext DBC { get; set; }
        public ISession Session;
        public ITransaction Transaction;

        public void BeginTransaction(IsolationLevel isolationLevel)
        { 
            //some code
        }
        public void Commit()
        {
            //some code
        }
        public void Rollback()
        {
            //some code
        }
        public void Dispose()
        {
            //some code
        }
        public UnitOfWork(ISession session)
        {
            //some code
        }
    }
}

DBContext.cs

namespace DataAccess.NHibernate.UnitOfWork
{
    public class DBContext
    {
        public IUow UnitOfWork { get; set; }
        public IUserRepository UserRepository
        {
            get
            {
                return new NHibernateUsersRepository(this.UnitOfWork);
            }
        }
    }
}

IUow.cs

namespace Domain.Repository.UnitOfWork
{
    public interface IUow : IDisposable
    {
        IDBContext DBC { get; set; }

        void Commit();
        void BeginTransaction(IsolationLevel isolationLevel);
        void Dispose();
        void Rollback();
    }
}

IDBContext.cs

namespace Domain.Repository.UnitOfWork
{
    public interface IDBContext
    {
    }
}

【问题讨论】:

    标签: c# .net class interface


    【解决方案1】:
    class UnitOfWork : IUow, IDisposable
    {
        public DBContext DBC { get; set; }
        ...
    }
    

    需要

    class UnitOfWork : IUow, IDisposable
    {
        public IDBContext DBC { get; set; }
        ...
    }
    

    使其与您的界面相匹配:

    public interface IUow : IDisposable
    {
        IDBContext DBC { get; set; }
        ...
    }
    

    错误信息解释了这一点

    Domain.Repository.UnitOfWork.IUow.DBC' because it does not have the matching return type of 'Domain.Repository.UnitOfWork.IDBContext'

    【讨论】:

      【解决方案2】:

      您在合同(界面)中声明您将返回:

      IDBContext DBC { get; set; }
      

      但你将它实现为:

      public DBContext DBC { get; set; }
      

      没有实现 IDBContext:

      public class DBContext
      

      改成:

      public class DBContext : IDBContext
      

      它应该可以工作。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-07-09
        相关资源
        最近更新 更多