Gerald Hughes
Gerald Hughes

Reputation: 6159

Cannot implement interface member because it does not have the matching return type

I'm not sure what the problem is here but i get the following compiler error:

ERROR:

Error 3 'DataAccess.NHibernate.UnitOfWork.UnitOfWork' does not implement interface member 'Domain.Repository.UnitOfWork.IUow.DBC'. 'DataAccess.NHibernate.UnitOfWork.UnitOfWork.DBC' cannot implement 'Domain.Repository.UnitOfWork.IUow.DBC' because it does not have the matching return type of 'Domain.Repository.UnitOfWork.IDBContext'. ..\DataAccess.NHibernate\UnitOfWork\UnitOfWork.cs 12 11 DataAccess.NHibernate

Bellow is my code

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
    {
    }
}

Upvotes: 0

Views: 3422

Answers (2)

Gerino
Gerino

Reputation: 1983

You state in your contract (interface) that you will return:

IDBContext DBC { get; set; }

But you implement it as:

public DBContext DBC { get; set; }

which doesn't implement IDBContext:

public class DBContext

Change it to:

public class DBContext : IDBContext

and it should work.

Upvotes: 1

Trevor Pilley
Trevor Pilley

Reputation: 16393

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

needs to be

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

so that it matches your interface:

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

The error message explains this

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

Upvotes: 2

Related Questions