开发者

Close NHibernate session in WinForm application with Windsor Castle

开发者 https://www.devze.com 2023-03-08 07:44 出处:网络
My WinForm application is configured like this: public class RepositoriesInstaller : IWindsorInstaller

My WinForm application is configured like this:

 public class RepositoriesInstaller : IWindsorInstaller
{
    public void Install(IWindsorContainer container, IConfigurationStore store)
    {
        container.Register(AllTypes.FromAssemblyContaining<EventRepository>()
          .BasedOn(typeof(IRepository<>))
          .WithService.AllInterfaces()
          .Configure(c => c.LifeStyle.Transient));
    }
}

Program.cs

   FrmStart form1 = CastleContainer.Resolve<FrmStart>();

I inject Repository class in my forms but I have to be sure that NHibernate session will be closed when I will close the form. Is开发者_如何学JAVA this the right path to dispose it?

   public class EventRepository : IRepository<Event>,IDisposable
{
    private readonly ISession session;

    public EventRepository(ISession session)
    {
        this.session = session;
    }

    public void Dispose()
    {
        session.Close();
    }

update

Is this code valid?

 private void button1_Click(object sender, EventArgs e)
    {
        FrmStart form1 = CastleContainer.Resolve<FrmStart>();
        form1.FormClosed += new FormClosedEventHandler(form1_FormClosed);
        form1.Show();

    }
    void form1_FormClosed(object sender, FormClosedEventArgs e)
    {
        CastleContainer.Instance.Release(sender);
    }


Windsor supports the IDisposable interface and should call Dispose automatically when releasing your component.

Notice that in order to support decommission properly Windsor holds reference to each components it creates. That's why it's crucial to release components. Otherwise you may have to deal with increased memory consumption.


assuming from you example you have a main frm(frmMain) and and additional one(frm1) you'd like to show on some button click You have to put all of them into the container of course among their dependencies, than UI root = frmMain contructor will look like

public partial class frmMain : Form
{
    frm1 _frm1Instance
    public frmMain(frm1 frm1Instance)
    {
      _frm1Instance = frm1Instance;
      //...
    }

        private void button1_Click(object sender, EventArgs e)
        {
            _fmr1Instance.Show();

        }
}

on the Guywire among Wire and DeWire methods as per example, you'll have a method like

public System.Windows.Forms.Form GetRoot()
{
    return container.Resolve<frmMain>();
}

than into you main method, you'll create an instance of Guywire and you'll use Form returned by GetRoot() to start your app

If you decide to follow this way, you cannot dispose your frm1 on close otherwise on next button1 click you'll attempt to access a diposed object.

If you really need to dipose frm1, I suggest you to inject a TypedFactory(WindsorFacility) instead of frm1 instance, get an instance through the factory and dipose it through the factory as well. see: http://mookid.dk/oncode/archives/1854


Why don't you try to use an intercepotor instead?

http://kozmic.pl/2010/03/11/advanced-castle-windsor-ndash-generic-typed-factories-auto-release-and-more

I don't like so much approach to keep a static reference to the container... I suggest you to avoid to call directly Resolve/Release... You should wired everything at the very begin and have components calling each others

http://fabiomaulo.blogspot.com/2009/11/guywire.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号