如何在C#中实现对自定义函数的处理?

时间:2019-06-11 04:44:52

标签: c# dispose

我有下面列出的示例类,每次调用API时,都需要使用CreateDataConnection()打开数据库连接。

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

我想按以下方式实现Activate API

public DBStatus Activate(W_Flower oFlower)
{
   using (CreateDataConnection())
   {
       DBStatus result = oDataConnection.Activate(oFlower);
   }
   return result;
}

但是这不起作用,因为我在CreateDataConnection中没有dispose方法。我如何在这里实施处置?我以前没有做过这种方法。

4 个答案:

答案 0 :(得分:8)

CreateDataConnection更改为返回新创建的连接

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

而不是将其存储在属性中。

那你就可以做

public DBStatus Activate(W_Flower oFlower) {
   using (var connection = CreateDataConnection()) {
       return connection.Activate(oFlower);
   }
}

答案 1 :(得分:1)

这应该有帮助:

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
}

答案 2 :(得分:1)

您可以通过以下方式实现它:

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

答案 3 :(得分:-2)

您需要实现IDisposable接口,之后,您可以使用Dispose方法,只需在Dispose中编写自己的逻辑即可。