简体   繁体   English

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

[英]How to implement Dispose to custom function in C#?

I have the sample class as listed below an I need to open the db connection using CreateDataConnection() every time I call an API. 我有下面列出的示例类,每次调用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;
        }
}

I want to implement Activate API as below 我想如下实现Activate API

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. 但这不起作用,因为我在CreateDataConnection中没有dispose方法。 How can I implement dispose here? 我如何在这里实施处置? I have not done this method before. 我以前没有做过这种方法。

Change CreateDataConnection to return the newly created connection, 更改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);
}

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

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
}

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM