How can I guarantee that Ninject will call one-time () automatically?

1)

public class DataProvider : IProvider , IDisposable{ private SqlConnection connection = null; public DataProvider(string ConnectionString) { this.connection = new SqlConnection(ConnectionString); this.connection.Open(); } public object GetUniqueData(SqlCommand CommandSql){} public void ExecuteInsertDeleteUpdate(SqlCommand CommandSql){} public void Dispose(){ if (this.connection != null) { this.connection.Close(); this.connection.Dispose(); } } } 

2)

 public class ManageBrandDAL : IManageBrandDAL { private IProvider provider = null; [Inject] public ManageBrandDAL (IProvider provider_){ this.provider = provider_; } public void RegisterBrand(string a_BrandName){ SqlCommand SQLCommand = new SqlCommand("INSERT INTO Brand(name) VALUES(@pm_brandname)"); SqlParameter pm_brandname= new SqlParameter(); pm_brandname.ParameterName = "@pm_brandname"; pm_brandname.DbType = DbType.String; pm_brandname.Value = a_BrandName; SQLCommand.Parameters.Add(pm_brandname); this.provider.ExecuteInsertDeleteUpdate(SQLCommand); } 

3)

 public class ModuleInfra : Ninject.Modules.NinjectModule { public override void Load(){ Bind<IProvider>() .To<ProvedorDados() .InTransientScope() .WithConstructorArgument("ConnectionString", Manage.ConnectionString); } } 

How can I guarantee that the Ninject Container will call the Dispose() method in the DataProvider class after ManageBrandDAL uses the DataProvider ?

Is InTransientScope() best lifecycle for this type of situation? If not, then what is more appropriate?

+4
source share
1 answer

When you bind your DataProvider InTransientScope() , it will not be deleted by Ninject, because in fact the temporary area is not an area at all. Ninject does not track objects bound in the transient area after it creates one for you.

Ninject provides instances of objects that implement IDisposable as soon as the main area object is assembled by the GC (but, as I said, this does not work for objects attached to the transient region, because such a region object does not exist).

You must bind your DataProvider to the scope corresponding to your application. It could be:

  • InRequestScope() for the web application (Ninject will delete instances of objects that implement IDisposable after the end of the HTTP request - do not forget to enable the OncePerWebRequest module)
  • InSingletonScope() - an instance of the object will be reused for all subsequent requests throughout the application life cycle - in my opinion, this is not an option for objects containing resources, such as SqlConnection
  • InThreadScope() - an instance of the object will be reused in one thread
  • InScope() - this can be used to create custom areas, so according to the type of application you can create your own custom area that is suitable for your needs.

There are also interesting extensions for Ninject that provide additional scope definitions: https://github.com/ninject/ninject.extensions.namedscope/wiki

hints

  • You do not need to call either Close() or Dispose() on SqlConnection . Dispose() enough because it internally calls Close() .
  • I do not see all the code, but I also remember to dispose of SqlCommand .
  • If you allow instances of ManageBrandDAL to be created in Ninject, you do not need to use InjectAttribute in its constructor. This will free you from using a specific IOC provider.
+4
source

All Articles