Reputation: 2299
I have the sample class as listed below an I need to open the db connection using CreateDataConnection() every time I call an API.
public class FlowerController : ApiController
{
DataConnection oDataConnection { get; set; }
public void CreateDataConnection()
{
ConnectionParameters oParams = new ConnectionParameters();
oParams.strDatabaseName = "123123123123";
oParams.strPassword = "123123123123";
oParams.strSchemaName = "123123123123";
oParams.strServerIP = "192.168.1.1";
oParams.strServerPort = "12313";
oParams.strUsername = "123123123";
oDataConnection = new DataConnection(oParams);
}
[HttpPost]
[AllowAnonymous]
[Route("api/flower/Activate")]
public DBStatus Activate(W_Flower oFlower)
{
CreateDataConnection();
DBStatus result = oDataConnection.Activate(oFlower);
return result;
}
}
I want to implement Activate API as below
public DBStatus Activate(W_Flower oFlower)
{
using (CreateDataConnection())
{
DBStatus result = oDataConnection.Activate(oFlower);
}
return result;
}
But this does not work as I do not have dispose method in CreateDataConnection. How can I implement dispose here? I have not done this method before.
Upvotes: 0
Views: 331
Reputation: 63732
Change CreateDataConnection
to return the newly created connection,
public DataConnection CreateDataConnection() {
ConnectionParameters oParams = new ConnectionParameters();
oParams.strDatabaseName = "123123123123";
oParams.strPassword = "123123123123";
oParams.strSchemaName = "123123123123";
oParams.strServerIP = "192.168.1.1";
oParams.strServerPort = "12313";
oParams.strUsername = "123123123";
return new DataConnection(oParams);
}
instead of storing it in a property.
Then you can do just
public DBStatus Activate(W_Flower oFlower) {
using (var connection = CreateDataConnection()) {
return connection.Activate(oFlower);
}
}
Upvotes: 8
Reputation: 71
you need to implement IDisposable interface, after that, you can have Dispose method, just write your own logic in Dispose and that's all.
Upvotes: -2
Reputation: 1554
You can implement it following way :
public class MyClass : IDisposable
{
bool disposed;
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
if (disposing)
{
//dispose managed resources
}
}
//dispose unmanaged resources
disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
Upvotes: 1
Reputation: 117064
This should help:
public class SomeClass : IDisposable
{
#region IDisposable Support
private bool disposedValue = false; // To detect redundant calls
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
{
if (disposing)
{
// TODO: dispose managed state (managed objects).
}
// TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
// TODO: set large fields to null.
disposedValue = true;
}
}
// TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
// ~SomeClass() {
// // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
// Dispose(false);
// }
// This code added to correctly implement the disposable pattern.
public void Dispose()
{
// Do not change this code. Put cleanup code in Dispose(bool disposing) above.
Dispose(true);
// TODO: uncomment the following line if the finalizer is overridden above.
// GC.SuppressFinalize(this);
}
#endregion
}
Upvotes: 1