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 8759849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:54:20+00:00 2026-06-13T14:54:20+00:00

I am using Castle.Windsor in one factory class to create instance of required class.

  • 0

I am using Castle.Windsor in one factory class to create instance of required class.
You can find example below.

public class MyFactory : IDisposable
{
    protected readonly IKernel Kernel;

    protected MyFactory(IKernel kernel)
    {
        Contract.Requires<ArgumentNullException>(
            kernel.NotNull(),
            "'kernel' parameter must be initialized.");

        Kernel = kernel;
    }

    public IMyType Create(long param1, long param2)
    {
        return Kernel.Resolve<IMyType>(
            new { numberOfRows, numberOfCells });
    }

    public void Dispose()
    {
        DisposeManagedResources();
    }

    protected virtual void DisposeManagedResources()
    {
        Kernel.Dispose();
    }
}
  1. I am not sure that I need IDisposable here… should I dispose Kernel right after disposing of MyFactory instance?
  2. I am not sure that IKernel (in constructor) is the best way in order to resolve IMyType in Create method. I guess, somebody can suggest a more elegant version 🙂

Any other ideas?

Thank you advance.

  • 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-06-13T14:54:21+00:00Added an answer on June 13, 2026 at 2:54 pm

    As a general rule, a class should only dispose resources that it owns. Since this resource is supplied from the outside, that class is not responsible for disposing it, unless the ownership for that resource is ‘explicitly’ passed with it. This explicitness of passing the ownership is usually done through documentation or by using common design patterns. It is quite natural for instance, that a factory method (named CreateXXX), passes the ownership the caller with the returned instance. DI containers on the other hand contain Get, GetInstance, or Resolve methods, and they don’t pass the ownership to the caller.

    But besides this, in your case you are dealing with a DI container. A DI container instance should normally live for the total duration of the application. Although containers usually need disposing, calling dispose within this class will clearly be the wrong place to do so. It is not the responsibility of this factory class to do so. Since the container should live for the duration of the application, the correct place to do so is during application teardown. In a ASP.NET application for instance, this will typically be the Application_End event in the global.asax.

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

Sidebar

Related Questions

I'm using Castle Windsor, and I've got the following: public class NhibernateRepository<T> : IRepository<T>
I'm using Castle Windsor as part of a more specialized framework. One of the
using System; using Castle.Windsor; using Castle.MicroKernel.Registration; using System.Reflection; using Castle.MicroKernel.Resolvers.SpecializedResolvers; namespace Windsor { class
I'm using Castle Windsor 3.0 for dependency injection in a demo ASP.NET app. One
I'm trying to use Castle Windsor to create my message handlers because just using
Using Castle.Windsor in ASP.NET MVC (3.0) is there any way I can appropriately handle
I know with Castle Windsor, you can register aspects (when using method interception in
Using Castle Windsor I can register multiple implementations of a type e.g. container.Register(Component.For<IMyInterceptor>() .ImplementedBy<MyInterceptor>();
I am using Castle Windsor for IoC, and have the configuration held in the
Is is possible to inject IPrincipal using Castle Windsor into my asp.net mvc controller.

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.