Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6234357
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T10:28:17+00:00 2026-05-24T10:28:17+00:00

I was wondering if the following code shows a bad practice (about interface inheritance):

  • 0

I was wondering if the following code shows a bad practice (about interface inheritance):

public interface IFoo : IDisposable
{
    void Test();
}

public class TestImpl : IFoo
{
    public void Test()
    {
        // do something
    }

    public void Dispose()
    {
        // disposing my dependencies
    }
}

public class FooFactory
{
    public IFoo CreateFoo()
    {
        return new TestImpl();
    }
}

public class Client
{
    public void Main()
    {
        FooFactory factory = new FooFactory();
        using(IFoo foo = factory.CreateFoo())
        {
            // do stuff then auto-dispose
            foo.Test();
        }
    }
}

Here, I wanted two things:
1. Use the C#’s using statement so I can dispose my concrete object’s dependencies elegantly (no need to cast anything to IDisposable).
2. Use a factory method to create a concrete object, so I can work only with the interfaces.

Separated, both are known good practices, but together? I didn’t find anything saying it’s wrong here http://msdn.microsoft.com/en-us/library/ms229022.aspx (MSDN – Interface Design), but I think it doesn’t sound good…

I would appreciate any comment about this. If it’s a bad practice, what kind of issues I would run into using this approach?

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T10:28:18+00:00Added an answer on May 24, 2026 at 10:28 am

    In the wild, there is no such thing as a definitely good or bad practice, each has its upsides and downsides.
    A practice becomes good or bad only when it is applied to a concrete problem (read: not IFoo).

    public bool IsGood<TContext>(IPractice practice)
        where TContext : RealWorldApplication, new();
    

    So, instead of trying to find guidance on MSDN, ask yourself the following question:

    Does this solve the problem and make the code simpler to read?

    If the answer is yes, go with it.

    Personally, I like the approach. If the contract for IFoo requires proper disposal (think: IGraphicalObject, IResource, IConnection), it’s a very good design decision to make this explicit.

    Don’t be trapped by dogma: programming with interfaces makes you less dependent on concrete implementations, but programming only with interfaces can be a nightmare. Be reasonable, that’s it.

    Update

    You can use Google to find all interfaces that inherit from IDisposable in .NET Framework itself:

    intitle:interface "inherits idisposable" site:msdn.microsoft.com

    As I said above, this is normally done for entities that are known to consume resources, such as database connections, unmanaged object wrappers, graphical objects, et cetera.

    Imposing implementing Dispose() on a class that has nothing to dispose of would be a bad idea.
    In this case it’s better to leave it optional and check for IDisposable support, as suggested by Peter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm wondering why the following code doesn't work: String test = new String(new byte[]
I have some questions about the following code: using System; namespace ConsoleApplication2 { public
Is the following code legal in C++. Accessing a friend class member public method?
In the following code $j is the jquery object. I was wondering why I
I was wondering how can I allow NULL values in the following code below
I'm wondering how one could use template databinding to accomplish what the following code
I'm wondering if using the following Javascript code is reliable: if (!document.cookie) { alert('Cookies
I'm wondering why this code section prints out the following: print request.user.has_perm('bug_tracking.is_developer'): + str(request.user.has_perm('bug_tracking.is_developer'))
Just wondering if the following is considered to be good programming practice or not?
The following code shows what I would like to do; that is, I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.