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

  • Home
  • SEARCH
  • 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 3798638
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T13:36:17+00:00 2026-05-19T13:36:17+00:00

Up until this point, I have been learning IoC/DI with Castle.Windsor using ASP.NET MVC,

  • 0

Up until this point, I have been learning IoC/DI with Castle.Windsor using ASP.NET MVC, but I have a side project that is being done in Windows Forms, and I was wondering if there is an effective way to use it for that.

My problem is in the creation of forms, services, etc. In ASP.NET MVC, there is a sort of ‘Activator’ that does this under the hood, but this isn’t the case in Windows Forms. I have to create a new Form like var form = new fclsMain();, so a Form like ..

class fclsMain : System.Windows.Forms.Form
{
 private readonly ISomeRepository<SomeClass> someRepository;
 fclsMain(ISomeRepository<SomeClass> someRepository)
 {
  this.someRepository = someRepository;
 }
}

Falls kind of short. I would basically have to do …

var form = new fclsMain(IoC.Resolve<ISomeRepository<SomeClass>);

Which as I have had pointed out in at least three of my questions isn’t smart, because it’s supposedly not the ‘correct’ usage of IoC.

So how do I work with Castle.Windsor and Windows Forms? Is there some way to design a Form Activator or something? I’m really lost, if I can’t make a static IoC container that I can resolve from, what can I do?

  • 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-19T13:36:18+00:00Added an answer on May 19, 2026 at 1:36 pm

    In order to use the same Castle container throughout your entire application, create a static class like:

    public static class CastleContainer {
        private static IWindsorContainer container;
    
        public static IWindsorContainer Instance {
            get {
                if (container == null) {
                    container = new WindsorContainer();
                }
                return container;
            }
            // exposing a setter alleviates some common component testing problems
            set { container = value; }
        }
    
        // shortcut to make your life easier :)
        public static T Resolve<T>() {
            return Instance.Resolve<T>();
        }
    
        public static void Dispose() {
            if (container != null) 
                container.Dispose();
            container = null;
        }
    }
    

    Then register/install all your components in the Main() method. You can also hook into the application shutdown event to call Dispose() (although this isn’t critical).

    Castle actually uses a Windows Forms app in their quick-start guide.

    Edit:

    The pattern I showed above is a variant of the service locator, which some people refer to as an anti-pattern. It has a bad reputation because, among other reasons, it liters your code base with references to Windsor. Ideally, you should only have a single call to container.Resolve<...>() to create your root form. All other services & forms are injected via constructors.

    Realistically, you’ll probably need a few more calls to Resolve, especially if you don’t want to load every single corner of the application at startup. In the web world, the best practice is to hand off the container to the web framework. In the Windows Forms world you’ll need to implement your own service locator, like above. (Yes, handing the container to the ASP.NET MVC framework is still a service locator pattern).

    I’ve edited the above code example so that the static container is injectable; no resources are tied up in a static context. If you do end up creating your own service locator, you might also want to create a test utility like this one to make testing easier.

    public static class TestUtilities 
    {
        public static IContainer CreateContainer(Action<IContainer> extraConfig = null) 
        {
            var container = new WindsorContainer();
            // 1. Setup common mocks to override prod configuration
            // 2. Setup specific mocks, when provided
            if (extraConfig != null)
                extraConfig(container);
            // 3. Configure container with production installers
            CastleContainer.Instance = container;
            return container;
        }
    }
    

    This makes a shortcut for creating a new container that looks a lot like the production version, but with some services replaced with mocks. Some example tests might look like:

    [Test]
    public void SubComponentWorksGreat() 
    {
        using (var container = TestUtilities.CreateContainer())
        {
            var subComponent = container.Resolve<SubComponent>();
            // test it...
        }
    }
    
    [Test]
    public void SubComponentWorksGreatWithMocks() 
    {
        var repoMock = new Mock<IRepository>();
        using (var container = TestUtilities.CreateContainer(c => 
                c.Register(Component.For<IRepository>().Instance(repoMock.Object))))
        {
            var subComponent = container.Resolve<SubComponent>();
            // test it with all IRepository instances mocked...
        }
    }
    

    One last note. Creating a full container for every test can get expensive. Another option is to create the full container but only using nested containers for the actual tests.

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

Sidebar

Related Questions

I have this classic ASP site which has been working fine until we updated
I came across this recently, up until now I have been happily overriding the
Up until now I have been using std::string in my C++ applications for embedded
I'm using a java.util.concurrent.ExecutorService that I obtained by calling Executors.newSingleThreadExecutor() . This ExecutorService can
Up until recently, I've been storing multiple values into different hashes with the same
As I am currently doing this project in only C, I've up untill this
I got this output when running sudo cpan Scalar::Util::Numeric jmm@freekbox:~/bfwsandbox/sa/angel/astroportal/dtu8e/resources$ sudo cpan Scalar::Util::Numeric [sudo]
How can this line in Java be translated to Ruby: String className = java.util.Vector;
Until Office 2007, Excel has a maximum of 65,000 rows. Office 2007 bumped that
I didn't upgrade to Vista until May or so and one of the things

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.