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 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);
}
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
on the Guywire among Wire and DeWire methods as per example, you’ll have a method like
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