NCache 4.6 - Online Documentation

Sample Implementation of IWriteThruProvider Interface

 
      To utilize the IWriteThruProvider, include the following namespace in your application: Alachisoft.NCache.Runtime.DatasourceProviders.
 
      // Contains methods used to save/update an object to the master data source.
    public class SampleWriteThruProvider : IWriteThruProvider
    {
        // Object used to communicate with the Data source.
        private SqlConnection _connection;
 
        //Perform tasks like allocating resources or acquiring connections
        public void Init(IDictionary parameters, string cacheId)
        {
            object connStringObject = parameters["connstring"];
 
            string connString = connStringObject == null ? "" : connStringObject.ToString();
 
            if (connString != "")
                _connection = new SqlConnection(connString);
 
            try
            {
                _connection.Open();
            }
            catch (Exception ex)
            {
                //handle exception
            }
        }
 
        //Perform tasks associated with freeing, releasing, or resetting resources.
        public void Dispose()
        {
            if (_connection != null)
                _connection.Close();
        }
 
        //Responsible for write operations on data source
        public OperationResult WriteToDataSource(WriteOperation operation)
        {
 
            Product product = (Product)operation.ProviderCacheItem.Value;
 
            SqlCommand command = new SqlCommand();
            command.Connection = _connection;
 
            if (operation.OperationType == WriteOperationType.Add)
                command.CommandText = "INSERT INTO Products(ID,Name)VALUES( '" + operation.Key + "','" + product.ProductName + "')";
 
 
            if (operation.OperationType == WriteOperationType.Delete)
                command.CommandText = "DELETE FROM Products WHERE ID ='" + operation.Key + "'";
 
            if (operation.OperationType == WriteOperationType.Update)
                command.CommandText = "UPDATE Products SET Name = '" + product.ProductName + "' WHERE ID='" + operation.Key + "'";
 
            int result = command.ExecuteNonQuery();
 
            OperationResult operationResult = new OperationResult(operation, OperationResult.Status.Success);
 
            if (result < 1)
            {
                operationResult.DSOperationStatus = OperationResult.Status.Failure;
            }
 
            return operationResult;
 
        }
 
        public OperationResult[] WriteToDataSource(WriteOperation[] operations)
        {
            OperationResult[] operationResult = new OperationResult[operations.Length];
 
            SqlCommand command = new SqlCommand();
            command.Connection = _connection;
 
            int counter = 0;
 
            foreach (WriteOperation operation in operations)
            {
                Product product = (Product)operation.ProviderCacheItem.Value;
                if (operation.OperationType == WriteOperationType.Add)
                    command.CommandText = "INSERT INTO Products(ID,Name)VALUES( '" + operation.Key + "','" + product.ProductName + "')";
 
                if (operation.OperationType == WriteOperationType.Delete)
                    command.CommandText = "DELETE FROM Products WHERE ID ='" + operation.Key + "'";
 
                if (operation.OperationType == WriteOperationType.Update)
                    command.CommandText = "UPDATE Products SET Name = '" + product.ProductName + "' WHERE ID='" + operation.Key + "'";
 
                int result = command.ExecuteNonQuery();
 
                operationResult[counter] = new OperationResult(operation, OperationResult.Status.Success);
 
                if (result < 1)
                {
                    operationResult[counter].DSOperationStatus = OperationResult.Status.Failure;
                }
 
                counter++;
            }
 
            return operationResult;
        }
    }
 
Make sure to deploy the Write-Through Provider after implementation using NCache Manager.
 
 
See Also