views:

140

answers:

2

I want to use Ninject in my Windows application and I want to know if there is best practices that I can do; strategies to find a balance between performance and maintenance.

The problem with Windows application and Web application is that in Web application, there is a scope easy to define that is the context but with Windows application, you have no scope that is easy to use form after form.

As example, I have a service that query the database. This service have a constructor and received a UnitOfWork. With Ninject, I can create a property marked as to be injected but if I do that, each time I will create this service, a new connection will be created to the database.

Just for this reason, I must manually create my services to control the number of connection created and no dependency injector can be used.

I have found that you can call the Inject method after created the service for inject dependencies but I'm sure I can use a better strategy.

+2  A: 

With Ninject, you can have Ninject scope lifetimes of your injected dependencies to any object you want to provide (not just Singleton, Request, Thread, and Transient scopes).

From the Ninject Documentation Wiki:

You can also easily define you own scopes using the .InScope(object o) method.

You'll find some actual detail about how object scoping works in this Ninject Google Groups question & answer.

qstarin
+1 and related link http://kohari.org/2009/03/06/cache-and-collect-lifecycle-management-in-ninject-20/ is a must read for understanding Ninject 2.0 scopes
Ruben Bartelink
After reading linked articles, I now better understand how I can control the lifetime of dependent objects injected. This is good but without a great architecture, it can be awkward to maintain. It seem that very few people on the web gives tricks about how to design a good windows application and how to start the scope of object in winform always while keeping in mind that many other forms can works together.I use LightSpeed of Mindscape and the UnitOfWork use cache and when you have many winforms that works together, data must often be shared.Any body have a concrete sample of project?
Samuel
@Samuel, To be honest, I have not seen much in the way of concrete sample applications that show Windows Forms with ORM and UnitOfWork employed throughout a well architected application. I certainly would like to see them myself if I could ever find any.
qstarin
Thank you very much for your answer. I will search a sample project with these specifications and I will send it here.
Samuel
+1  A: 

This article by Ayende in MSDN Magazine is ostensibly about NHibernate, and mentions the word inject only once (and that only wrt AOP), but the phrasing of your question suggests to me it'll be great food for thought as you consider how to architect your app.

Ruben Bartelink