Question

If I have interface (example from apress book) which implements IDisposable like this

public interface IArchitectRepository : IDisposable
{
    IEnumerable<Architect> GetArchitects();
    Architect GetAchitectDetails(int id);
    void Create(Achitect a);
    void Update(Achitect a);
    void Delete(int id);
    void Save();
}

how would I implement Dispose method in derived class?

In book the it's left as NotImplementedException.

Was it helpful?

Solution

There is no reason why you would implement the Dispose pattern any differently than normal.

See Implementing a Dispose Method

OTHER TIPS

Unlike most interfaces which imply obligations on the part of an implementing class, the only obligation of a class which implements IDisposable is to regard a call to Dispose as adequate notice that an instance is going to be abandoned. The real obligations imparted by IDisposable fall upon instance owners: if a type implements IDisposable, each instance should have one owner who is obligated to ensure that its Dispose method will gets called before it's abandoned; the owner may do this either by calling Dispose itself, or passing ownership to a new owner who would then assume the obligation.

Note that the owner of an IDisposable instance doesn't care what Dispose does provided that the instance may be safely abandoned after it returns. If some particular object can be safely abandoned before Dispose is called, there's no need for the Dispose method to do anything. It should not throw a NotImplementedException or NotSupportedException, since it should be no able to doing everything it needs to do (i.e. nothing).

You should look at MSDN for the complete example: http://msdn.microsoft.com/en-us/library/system.idisposable.aspx

The minimum would be:

public class ArchitectRepository : IArchitectRepository
{
    // implement all the IArchitectRepository methods

    public void Dispose()
    {
        // assuming your repository has a connection property
        if (this.Connection != null)
            this.Connection.Close();
        // do the same for all other disposable objects your repository has created.
    }
}

Whiting the concrete implementation you can overwrite as follows

        public bool IsDisposed { get; set; }

        public bool IsLockedForDisposing { get; set; }


        /// <summary>
        /// Dispose the Loaded Context
        /// </summary>
        /// 
        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    // Disposing of managed code
                    context.Dispose();
                    // GC.Collect(context);
                }
                this.disposed = true;
            }
        }

        public void Dispose()
        {
            if (!IsLockedForDisposing)
            {
                IsDisposed = true;
                Dispose(true);
                GC.SuppressFinalize(this);
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top