Interface IWriteThruProvider
Contains methods used to save/update an object to the master data source. Must be implemented by write-through components.
Assembly: Alachisoft.NCache.Runtime.dll
Syntax
public interface IWriteThruProvider
Methods
Dispose()
Perform tasks associated with freeing, releasing, or resetting resources.
Declaration
void Dispose()
Examples
The following example disconnects from the data source when disposed.
public void Dispose()
{
_source.DisConnect();
}
Init(IDictionary, String)
Perform tasks like allocating resources or acquiring connections, etc.
Declaration
void Init(IDictionary parameters, string cacheId)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.IDictionary | parameters | Startup parameters defined in the configuration. |
System.String | cacheId | Id of the cache. |
Examples
The following example connects to the data source.
public void Init(IDictionary parameters, string cacheId)
{
object connString = parameters["connstring"];
_source.Connect(connString == null ? "" : connString.ToString());
}
WriteToDataSource(WriteOperation)
Responsible for atomic write operations on the data source.
Declaration
OperationResult WriteToDataSource(WriteOperation operation)
Parameters
Type | Name | Description |
---|---|---|
WriteOperation | operation | Write operation applied on the data source. |
Returns
Type | Description |
---|---|
OperationResult | Failed operations, null otherwise. |
Examples
The following example performs a single operation on data source.
public OperationResult WriteToDataSource(WriteOperation operation)
{
bool result = false;
OperationResult operationResult = new OperationResult(operation, OperationResult.Status.Failure);
object value = operation.ProviderItem.GetValue<object>();
if (value.GetType().Equals(typeof(Customer)))
{
result = _source.SaveCustomer((Customer)value);
}
if (result) operationResult.OperationStatus = OperationResult.Status.Success;
return operationResult;
}
WriteToDataSource(ICollection<DataTypeWriteOperation>)
When datatype is created or removed as bulk, data source will be notified with this method.
Declaration
ICollection<OperationResult> WriteToDataSource(ICollection<DataTypeWriteOperation> dataTypeWriteOperations)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<DataTypeWriteOperation> | dataTypeWriteOperations | Operation containing properties. |
Returns
Type | Description |
---|---|
System.Collections.Generic.ICollection<OperationResult> | OperationResult. |
Examples
public ICollection<OperationResult> WriteToDataSource(ICollection<DataTypeWriteOperation> operations)
{
List<OperationResult> operationResult = new List<OperationResult>();
foreach (DataTypeWriteOperation operation in operations)
{
var list = new List<Product>();
ProviderDataTypeItem<object> cacheItem = operation.ProviderItem;
Product product = (Product)cacheItem.Data;
switch (operation.OperationType)
{
case DatastructureOperationType.CreateDataType:
// Insert logic for creating a new List
IList myList = new List<Product>();
myList.Add(product.Id);
break;
case DatastructureOperationType.AddToDataType:
// Insert logic for any Add operation
list.Add(product);
break;
case DatastructureOperationType.DeleteFromDataType:
// Insert logic for any Remove operation
list.Remove(product);
break;
case DatastructureOperationType.UpdateDataType:
// Insert logic for any Update operation
list.Insert(0, product);
break;
}
// Write Thru operation status can be set according to the result.
operationResult.Add(new OperationResult(operation, OperationResult.Status.Success));
}
return operationResult;
}
WriteToDataSource(ICollection<WriteOperation>)
Responsible for bulk write operations on the data source.
Declaration
ICollection<OperationResult> WriteToDataSource(ICollection<WriteOperation> operations)
Parameters
Type | Name | Description |
---|---|---|
System.Collections.Generic.ICollection<WriteOperation> | operations | Array of write operations applied on the data source. |
Returns
Type | Description |
---|---|
System.Collections.Generic.ICollection<OperationResult> | Array of failed operations. |
Examples
The following example performs multiple operations on the data source.
public ICollection<OperationResult> WriteToDataSource(ICollection<WriteOperation> operations)
{
ICollection<OperationResult> operationResults = new OperationResult[operations.Count];
foreach(WriteOperation operation in operations)
{
operationResults.Add(WriteToDataSource(operation));
}
return operationResults;
}