Wednesday, February 25, 2015

Implementing Interface in C#


I have been programming for my living for quite few years, programming is more like hobby and sometime one of the way to past time, like gaming. Uncle Bob forwarded something called SOLID for good object oriented design and sometime we do blindly follow it sometime we really need it. Today I have something for Interface Segregation.
Interface has been around for quite a while from java to now typescript. Yes its basically a contract as defined on papers about interface but why do we need a contract? how do we implement it in real world application? There are enough Foo and Bar around, knowing technical part of it, knowing syntax is the most easiest part of anything along with how to do :-), but actually getting your finger to write code along with interface takes time.

Let us assume I have class FoodEstablishment, now I want to do simple CRUD operation, so how do I do it?
I define an interface for service, but why? wait :-)

public interface IFoodEstablishmentService
{
    Task<int> AddAsync(FoodEstablishment oFoodEstablishment);
    FoodEstablishment SelectByFoodEstablishmentId(long id);
    Task<int> UpdateAsync(FoodEstablishment oFoodEstablishment);
    Task<int> DeleteAsync(FoodEstablishment oFoodEstablishment);
}

Then I will implement that contract or interface for that particular service

public class FoodEstablishmentService : IFoodEstablishmentService
{
    public async Task<int> AddAsync(FoodEstablishment oFoodEstablishment)
    {
       // Insert Operation
        return result;
    }

    public FoodEstablishment SelectByFoodEstablishmentId(long id)
    {
        // Select Logic
        return oFoodEstablishment;
    }

    public async Task<int> UpdateAsync(FoodEstablishment oFoodEstablishment)
    {
        // Update Logic
        return result;
    }

    public async Task<int> DeleteAsync(FoodEstablishment oFoodEstablishment)
    {
        // Delete Logic
        return result;
    }
}

So in my main program or where I wish to use Service, I will do

IFoodEstablishmentService oFoodEstablishmentService =  new FoodEstablishmentService();
FoodEstablishment oFoodEstablishment = // Input might be from views;
oFoodEstablishmentService.AddAsync(oFoodEstablishment);

So till now it seems like an extra step, when we could have directly done

FoodEstablishmentService oFoodEstablishmentService =  new FoodEstablishmentService();
FoodEstablishment oFoodEstablishment = // Input might be from views;
oFoodEstablishmentService.AddAsync(oFoodEstablishment);

But wait what if I might need to pass my insert logic through queue rather than directly to server, wait for insert operation to complete and then return result, rather pass on queue and then the queue worker handles those operation, might not be best idea to queue insert but yes definitely good for interface example :-). So now what I do is, create another class FoodEstablishment implementing same contract IFoodEstablishment.

public class FoodEstablishmentQueueService : IFoodEstablishmentService
{
    public async Task<int> AddAsync(FoodEstablishment oFoodEstablishment)
    {
       // Insert Queue Operation
        return result;
    }

    public FoodEstablishment SelectByFoodEstablishmentId(long id)
    {
        // Select Queue Logic
        return oFoodEstablishment;
    }

    public async Task<int> UpdateAsync(FoodEstablishment oFoodEstablishment)
    {
        // Update Queue Logic
        return result;
    }

    public async Task<int> DeleteAsync(FoodEstablishment oFoodEstablishment)
    {
        // Delete Queue Logic
        return result;
    }
}

So now if I want to use the queue version, I would just do

IFoodEstablishmentService oFoodEstablishmentService =  new FoodEstablishmentQueueService();
FoodEstablishment oFoodEstablishment = // Input might be from views;
oFoodEstablishmentService.AddAsync(oFoodEstablishment);

We could do that with the older way using class but that bounds the class instantiation with a particular class which is kind of rigid to extension, now FoodEstablishmentQueueService can do other things too, create another method until the contract is valid so interface is contact for consistency, Imaging one guy doing the normal version and other doing the queue version or someone doing cache version, there might be problems regarding signature unless its pre-specified about the working contract and people don't end up cross checking everything.

Similarly, let consider other simple example of using predefined types like IEnumerable. Suppose I pass a list of FoodEstablishment collection and return custom sorted list

public FoodEstablishment[] SortFoodEstablishment(FoodEstablishment[] list)
{
    foreach(var oFoodEstablishment in list)
    {
    // some logic
    }
    return sortedList;
}

So we will use it this way

FoodEstablishment[] list = new FoodEstablishment[]{ //some values }
var listSorted = oFoodEstablishmentService.SortFoodEstablishment(list);

But what if we send list instead of array

List<FoodEstablishment> list = //some values;
var listSorted = oFoodEstablishmentService.SortFoodEstablishment(list);

We will get error, because its strict implementation using class so instead we use IEnumerable<> which is implemented by List and that basically removes dependency from List to Interface

public IEnumerable<FoodEstablishment> SortFoodEstablishment(IEnumerable<FoodEstablishment> list)
{
    foreach(var oFoodEstablishment in list)
    {
    // some logic
    }
    return sortedList;
}

So usually implementation hugely depends on situation, its not like I woke up suddenly and start writing everywhere :-)

I am getting really hungry and more foolish than ever :D

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