Sunday, February 8, 2015

Factory Pattern with Azure Blobs


I have been playing with Windows Azure quite a bit these days in my own noob ways :-). In Azure we have blobs which stores files on storage account rather than on the websites itself.

Practically in our part of world we store images and files on folder inside websites with different folder structure such as Uploads -> Images -> Company, its easy and its good but what if we want to transfer the same file to production? we commit same file to production right, but why do you want to commit same test file to production? yes might not be dynamic but for system specific files such as image we need to. So here blobs acts as another layering which is makes it independent. Further serving huge files which is what blobs stands for (big objects) from the server where websites hosts does not exhaust webserver feeding huge static content. And its easy with AzureCDN too. I actually tried free CDN with CloudFront too but its point of presence is far than AzureCDNs, so the network latency was relatively high with CloudFront resulting in poor performance so it was better without CDN then actually forcing to have one :-).

I have been through factory pattern on the web and some pdfs too, I find it good and easily implementable. Which describes the way the object are created with subclasses deciding type of class to be instantiate, so basically we instantiate classes based on parameter here. I am not too much on copy pasting with diagram and sorts of thing but have simple approach which can atleast convince us to use those. Here is how I implement basic of pattern on instantiating Azure Blobs

public class BlobCore
    {
        private readonly CloudBlockBlob _blockBlob;
 
        public CloudBlockBlob CloudBlockBlob
        {
            get { return _blockBlob; }
        }
 
        public BlobCore(string referenceType, string name)
        {
            // Can use Azure Cloud Service but for this used the traditional Web.Config
            CloudStorageAccount storageAccount =
                CloudStorageAccount.Parse(
                    ConfigurationManager.AppSettings["StorageConnectionString"]
            );
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer container = blobClient.GetContainerReference(referenceType);
 
            container.CreateIfNotExistsAsync();
 
            // Giving the blobs Public Access
            container.SetPermissions(new BlobContainerPermissions() {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });
 
            _blockBlob = container.GetBlockBlobReference(name);
        }
 
        public static BlobCore ProductContainer(string name)
        {
            return new BlobCore("Product", name);
        }
 
        public static BlobCore CompanyContainer(string name)
        {
            return new BlobCore("Company", name);
        }
 
        public int Upload(HttpPostedFileBase file)
        {
            CloudBlockBlob.UploadFromStream(file.InputStream);
            return 1;
        }
 
        public int Delete()
        {
            CloudBlockBlob.Delete();
            return 1;
        }
    }

So inorder to instantiate I would do something like this

var oBlobCore = BlobCore.ProductContainer('imageName');
oBlobCore.Upload(file);


So this gives me clear idea if I am using this as client not knowing the detail which clearly states that I am using ProudContainer with name of image that I wish to operate to. But here instead of using multiple classes my solution was best using single class. So implementation of patterns varies with need and someday if I come accross through the full detail factory pattern will love to post some codes here.

Staying very hungry and doing things foolishly

No comments :

Post a Comment