views:

29

answers:

1

I have a question for the Ioc gurus out there. I am working with a co-worker to wrap our minds around Castle Windsor IoC. We are having a difference of opinion about static Domain Service objects within asp.Net webforms. We have a static factory called BLServiceFactory in our Infrastructure layer that retrieves the container.

public sealed class BLServiceFactory
{
    private static BLServiceFactory _instance = new BLServiceFactory();


    IWindsorContainer _container = new WindsorContainer();
    public static BLServiceFactory Instance
    {

        get
        {return _instance;}
    }

    public T Create<T>()
    {
        return (T)_container[typeof(T)];
    }

    private BLServiceFactory()
    {

        _container.AddComponent("DataContext", typeof(DAL.DataContextFactory), typeof(DAL.CPContextFactory));
        _container.AddComponent("Repository", typeof(DAL.IRepository<>), typeof(DAL.Repository<>));
        _container.AddComponent("UserManager", typeof(BL.IUserManager), typeof(BL.UserManager));
        _container.AddComponent("RoleService", typeof(BL.IRoleService), typeof(BL.RoleService));
    }

}

We are pulling instances from the factory in our code behinds like this.

public partial class PrintList : System.Web.UI.Page
{
    private static readonly ISchoolManager _schoolService = BLServiceFactory.Instance.Create<ISchoolManager>(); 

    Models.TechSchool _tech;

    protected void Page_Load(object sender, EventArgs e)
    {

        _tech = _schoolService.GetSchoolForTechPrep(Profile.UserName);

    }

    protected void DoOtherStuff...
    {
      _schoolService.Foo(_tech);
    }

}

To me this looks like we will be serving up the same instance to every session. That would indeed be bad! My co-worker thinks that since all of our Domain Services are marked Transient, each page request will get a new instance.

I have also read a bit about memory leaks due to objects marked transient not released for garbage collection. Has this been addressed in the latest release of Castle Windsor, or should I be explicitly releasing objects? Of course as it stands now, all the objects are static and this would be irrelevant.

A: 

The BLServiceFactory is a service locator. I recommend using CommonServiceLocator instead of your own if you're going to use a service locator. Component registration does not belong inside the service locator.

Now, in the code you posted, there is no mention of those components being transient, unless you marked them with the [Transient] attribute. If you didn't, those components will be singletons, which is the default lifestyle in Windsor.

Since the variable _schoolService in PrintList is static, the same instance of ISchoolManager will be used for all requests to the PrintList page. If you really want it to be transient, remove the "static" keyword.

About releasing components, see this article.

BTW: AddComponent-style registration is deprecated, use Register() instead.

Mauricio Scheffer