Click here to Skip to main content
15,886,075 members
Articles / Programming Languages / XML

AppFabric Caching Extension

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Jan 2012CPOL4 min read 41.9K   1.2K   17  
A component which can be plugged to a WCF service or any other client to utilize AppFabric caching features
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Threading;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using TracingLibrary;
using CachingExtension;
using System.Collections.Specialized;

namespace WCFService
{
    public class WCFService : IWCFService
    {
        private const string CATEGORY_KEY_FORMAT = "Category-{0}";
        private const string CATEGORY_LIST_KEY = "CategoryList";

        List<CustomerDC> customers = new List<CustomerDC>();
        
        private string strConnection = ConfigurationManager.ConnectionStrings["strConnection"].ToString();
        private static CacheHelper _helper;              

        public List<CustomerDC> GetAllCustomers(string cust)
        {
            try
            {
                return GetCustomerFromDB();
            }
            catch(FaultException ex)
            {
                throw new FaultException(ex.Message);
            }
        }

        public CustomerDC GetCustomer(string customerID)
        {
            TraceEvents.AddEvent("Inside GetCustomer");
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("Sleep for 2 seconds.");
                Thread.Sleep(2000);
            }
            return new CustomerDC()
            {
                CustomerID = "TRST",
                FirstName = "Steve",
                LastName = "Jobs",
                CompanyName = "Apple"
            };
        }

        public string ExampleManualCacheUse()
        {
            string retStr = "inside business  layer";
            
            //Do this in constructor
            _helper = new CacheHelper();

            string key = _helper.GetEncryptedKey(CATEGORY_LIST_KEY);

            //Cache-aside pattern
            object obj = _helper.GetFromCache(key);
            if (obj == null)
            {
                //get obj from datastore
                _helper.PutInCache(key, retStr, new TimeSpan(0, 1, 0));                
                TraceEvents.AddEvent("value returned from program");
            }
            else
            {
                TraceEvents.AddEvent("value returned from cache");
            }
            return retStr;
        }

        public IEnumerable<string> GetEvents()
        {
            return TraceEvents.Events;
        }

        public void RemoveEvents()
        {
           TraceEvents.RemoveEvents();
        }

        public List<CustomerDC> GetCustomerFromDB()
        {
            TraceEvents.AddEvent("Inside GetCustomerFromDB method");

            SqlConnection objConnection = new SqlConnection(strConnection);
            SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, FirstName, LastName, CompanyName  FROM SalesLT.Customer", objConnection);
            selectCMD.CommandTimeout = 30;

            SqlDataAdapter CustomerDA = new SqlDataAdapter();
            CustomerDA.SelectCommand = selectCMD;

            objConnection.Open();

            DataSet CustomerDS = new DataSet();
            CustomerDA.Fill(CustomerDS, "SalesLT.Customer");

            foreach (DataRow pRow in CustomerDS.Tables["SalesLT.Customer"].Rows)
            {
                CustomerDC customerDC = new CustomerDC();
                customerDC.CustomerID = pRow["CustomerID"].ToString();
                customerDC.FirstName = pRow["FirstName"].ToString();
                customerDC.LastName = pRow["LastName"].ToString();
                customerDC.CompanyName = pRow["CompanyName"].ToString();
                 
                customers.Add(customerDC);
            }
            return customers;

        }      

    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions