views:

409

answers:

2

Hi there,

can anyone help, i have a small issue, i have an interface and also a base interface, when i try to do

  .Dispose()

It doesn't find the method as its implemented on my sub class NOT base.. and it always seems to want to call the base - even though i specifically put the namespace in front of the parameter on the constructor.

Here is some code to explain it better, basically there are 2 IhouseRepository (interfaces), 1 is the base interface and one is the subclass interface.

In the constructor i have specifically said its MarkSmith.Data (and not MarkSmith.DataBase) but it keeps pickup up the DataBase version where Dispose is not implemented.

My idea was to implement IDisposable in all subclasses and should be there responsibility to dispose.

In the constructor i have a put a single line that calls the IhouseRepository and i "CAN" access Dispose - so it does work - Why it works here on not on the param passed to the constructor is a mystery :-)

But the param on the constructor seems to be forcing the namespace DataBase and not Data

I suppose i could rename all my Interfaces on the base project to IHouseRepositoryBase but i don't understand why this is happening.

Any help really appreciated

public class HouseService : ServiceBase.HouseService, IHouseService
{
    public HouseService(MarkSmith.Data.IHouseRepository repository)
        : base(repository) 
    {

        MarkSmith.Data.IHouseRepository test =
            new MarkSmith.Data.HouseRepository(new MyDataContext);
        test.Dispose(); // THIS WORKS! NO PROBLEMS
    }

    // Dispose() calls Dispose(true)
    public void Dispose()
    {
        Dispose(true);
        GC.SuppressFinalize(this);
    }


    protected virtual void Dispose(bool disposing)
    {
        if (disposing)
        {
            // free managed resources
            if (repository != null)
            {
                repository.Dispose(); // THIS FAILS .. IT IS CALLING NS DATABASE
            }
        }
+3  A: 

Doesn't your overloaded Dispose() method need to be virtual so that subclasses can override its implementation? Otherwise, the method that ends up getting called will depend on what interface you're calling it through.

Also, if all subclasses should be required to implement IDisposable, then the base class should implement IDisposable as well -- this causes the compiler to enforce it.

Daniel Pryden
Thnaks daniel, i got a bit confused .. anyway with your suggestion its working.. Basically i my base class inherit from IDisposable but are implemented as abstract .. and then in the subclass these are overriden .. Now all works!
mark smith
+1  A: 

If you followed the recommended pattern then your Dispose method in the HouseRepository is not virtual. It means that which one will be called is statically determined compile time based on the type of the variable you use to access it - not on the type of the instance in the variable.

This is just general considerations, but this is as far as I can go because in your code sample the member definition for the repository is not present.

mfeingold