0

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.

1

4 Answers 4

8

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);
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

This method still needs IDisposable.
@PCG You didn't say what DataConnection is, you just talked about not having Dispose method in CreateDataConnection (which is a method).
1

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
}

Comments

1

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);
    }
}

Comments

-2

you need to implement IDisposable interface, after that, you can have Dispose method, just write your own logic in Dispose and that's all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.