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?
source share