Translate

Wednesday, 29 October 2014

A little more CSS magic !

One of the coolest things about CSS and jQuery is the simplicity that you can achieve in design.

Here is an example of a simple timeline that I created with css



The vertical line is not a .gif or an image but a line drawn, given co-ordinates in pixels.

The CSS for the same is in this fiddle that I have created for you to play around with.

And with a little jQuery, the animation of the timeline becomes simply simple!

Saturday, 4 October 2014

The Repository Pattern - I (The Interface and ConcreteRepository)

The use case for a repository can be many - code, books, project etc but it is when these repos need to be combined and managed that the Repository pattern begins to make sense and the SharpRepository and the Unit of work technique to maintain/manage transactions for the different repositories, gain importance.

How the aggregate is defined and managed is how the question of whether 'one size fits all?' question will get an answer.

Although there is nothing new that could probably be added to the many excellent resources on the SharpRepository implementation, I did find some curious elements that were not as well or as clearly implemented in the many available resources on the web. This is the simplest of them all.

I will post my code first and continue with the explanations inline as and when and where I found it necessary.

The example that I have chosen is dependent on many interfaces and concrete implementations so posting the fully tested code will be done in stages.

The interface

using System.Collections.Generic;

namespace LinqExpressionsHashSet
{
    interface IRepository<T>
    {
            T GetById(int id);
            T GetByName(string name);
            IEnumerable<T> GetAll();
            IEnumerable<T> FindAll(IDictionary<string, object> propertyValuePairs);
            T FindOne(IDictionary<string, object> propertyValuePairs);
            T SaveOrUpdate(T entity);
            void Delete(T entity);  
    }
}

The concrete repository

using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using SharpRepository.Repository.Queries;

namespace LinqExpressionsHashSet
{
        public class Repository<T> : IRepository<T>,IEnumerable<T> where T : class,IIdentifiable,new()
        {
          // for InMemoryRepository testing
            private readonly HashSet<T> data;
            private readonly IQueryable<T> queryableData;

        public Repository()
            : this(Enumerable.Empty<T>())
        {
        }
        public Repository(IEnumerable<T> testData)
        {
            data = new HashSet<T>(testData);
            queryableData = data.AsQueryable();
        }

        public Expression Expression { get; private set; }

        public Type ElementType { get; set; }

        public IQueryProvider Provider { get; private set; }

        public void Add(T entity)
        {
            data.Add(entity);
        }

        public void Add(IEnumerable<T> entities)
        {
            foreach (var e in entities)
            {
                data.Add(e);
            }
        }

        public void Update(T entity)
        {
            data.RemoveWhere(i => i.Id == entity.Id);
            data.Add(entity);
        }

        public void Update(IEnumerable<T> entities)
        {
            foreach (var e in entities)
            {
                Update(e);
            }
        }
        /* To use for Unit of Work
        public IBatch<T> BeginBatch()
        {
            return (IBatch<T>)data.ToList<T>();
        }
        */
        public void Delete(T entity)
        {
            data.RemoveWhere(i => i.Id == entity.Id);
        }

        public void Delete(IEnumerable<T> entities)
        {
            foreach (var e in entities)
            {
                data.RemoveWhere(x => x.Id == e.Id); // The id is from MongoDB
            }
        }

        public T Find(Expression<Func<T, bool>> predicate, IQueryOptions<T> queryOptions = null)
        {
//            Extracting the lambda expression
            Expression<Func<T, bool>> ep = Expression.Lambda<Func<T, bool>>(predicate);
            // without queryOptions
            return queryableData.First(ep);
        }

/*
            //Need to test this
        public TResult Find<TResult>(Expression<Func<T, bool>> predicate, Expression<Func<T, TResult>> selector,
                                     IQueryOptions<T> queryOptions = null)
        {
            Expression<Func<T, TResult>> ep = Expression.Lambda<Func<T, TResult>>(selector);
            IEnumerable<TResult> someResults = queryableData.Select(selector);
            Expression<Func<T, bool>> exp = Expression.Lambda<Func<T, bool>>(predicate);
            TResult tt = default(TResult); // to escape uninitialized compiler error and null.
            foreach (TResult a in someResults)
            {
                var e = queryableData.First(exp);
                if (data.Contains(e))
                {
                    tt = a;
                    break;
                }
            }
            return tt;
        }
*/
        public bool Exists(Expression<Func<T, bool>> predicate)
        {
            Expression<Func<T, bool>> ep = Expression.Lambda<Func<T, bool>>(predicate);
            Func<T, bool> fexp = ep.Compile();
            var t = fexp(new T()); // Returns bool from the expression

            return t; // This is not the correct return value. Used only to pass the compiler.
        }

            /* To be used with EF
            protected DbSet<T> DbSet;

            public Repository(DbContext dataContext)
            {
                DbSet = dataContext.Set<T>();
            }
         
            public void Insert(T entity)
            {
                DbSet.Add(entity);
            }

            public void Delete(T entity)
            {
                DbSet.Remove(entity);
            }

            public IQueryable<T> SearchFor(Expression<Func<T, bool>> predicate)
            {
                return DbSet.Where(predicate);
            }

            public IQueryable<T> GetAll()
            {
                return DbSet;
            }

            public T GetById(int id)
            {
                return DbSet.Find(id);
            }
             */
        }
    }

and an identifiable interface for in memory and MongoDB implementation. This could be substituted with an IEntity interface that can help provide the 'id' but on this (the BS, that is :)) I will post later. This is being used to query an expression with an 'id' in the Delete method above.

// The Identity interface

using MongoDB.Bson;
namespace LinqExpressionsHashSet
{
    public interface IIdentifiable : IIdentifiable<ObjectId>
    {
        string StringId { get; }
    }
    public interface IIdentifiable<T>
    {
        T Id { get; }
    }

}

// The Project repo
using System;

namespace LinqExpressionsHashSet
{
    public class Project:IIdentifiable
    {
        public int projectId { get; set; }
        public string name{get;set;}
        public DateTime createdOn { get; set; }
        public string createdBy { get; set; }
    }
}

More to follow...:)

Wednesday, 1 October 2014

The de-generations

It must be a serious set of nutcases that are going around asking "Do you write your own blog?". The level of intelligence has de-generated to such an extent that it is not even worth mentioning to look at the paradox in the question - how can it be my blog if I do not write it ? :D

Go, bite elsewhere or go get your brains or head examined and if there is no cure, just take the damn thing off your shoulders (or have it taken off you there are many obliging psycho morons around nowadays), it is not worth carrying that much weight...:D

Monday, 25 August 2014

What to Unit test?

Going by the book is not really encouraged in today's wildly advancing technology world. If you, do, it will most probably leave you staring at the behinds of many. :)

Best way to keep yourself ahead is to be really good in your skill set; if it is a programming language, be on top by participating in discussion forums and communities. This will help knowing about scenarios that may never appear in your own project or lifetime.

Close to what I am getting at is this definition of unit tests by Martin Fowler -
Like most software development terminology, however, it's very ill-defined, and I see confusion can often occur when people think that it's more tightly defined than it actually is. 
First and foremost,  what to unit test depends on your commitment to testing. If you are very committed then chances are that your code coverage will be good. But over and above this,  you also need the skill with the unit test tool. For instance, the most common statement possibly used by almost all programmers across the world is the if (!fileExists) or within a try...catch block.

Is this statement testable? Should it be tested? To answer the second question first, the reason the answer is 'Yes' is because the line of code is expected to throw many exceptions -  DirectoryNotFoundException, FileNotFoundException and (yes) etc...So, the first question becomes important - is it testable?

Again, the answer is 'Yes' (left out italicizing it to make it more interesting! :)) but if you really do not know any syntax of existing unit test tool, how to write the test?

This question has always seemed important to me (to make a programmer move from the "Code-first syndrome" to a "Test-first-and-let-the-test-tell-you-what-code-to-write avatar") and so, I decided to list (well, sharing just one technique now (how to get and test a list of files from the file system in a unit test)...will share more later) here some of these common scenarios.

Aside from helping the code achieve the necessary quality benchmarks, unit tests should also be efficient and lean so that it does not weigh down the overall build or CI framework.

So, to write a unit test in the 'file exists' scenario,

1. If not file exists

Scenarios - as many as there are files (wild possibilities! :)) in a specified directory.

So, the unit test will want a list of files against which to test for some criteria.

The code will be something like this -

using System.Collections.Generic;
using System.Linq;
using System.IO;
namespace NUnit_Tests
{
    public class ClassUnderTest
    {            
        public List getListOfFiles()
        {
            string[] filePaths=null;
            filePaths = Directory.GetFiles(@"c:\cambridge\");
            return filePaths.ToList();
        }
    }
}

In  NUnit, here is an example of how to test the method most efficiently and ALSO in such a way that maximum scenarios can be tested with this approach !!

using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
namespace NUnit_Tests
{
    [TestFixture]
    public class TestClass
    {
        ClassUnderTest objInstance;
        string[] filesToExist = { "abc.jpg", "a.htm" };
        [Category("LongRunning")]
        [Test]
        public void LongRunningTest([ValueSource("Files")] string filePath)
        {
            Assert.That(Files.ToList().LastOrDefault(),Is.EqualTo("zzz.jpg"));
        }

        private IEnumerable Files
        {          
            get
            {
                objInstance = new ClassUnderTest();
                return objInstance.getListOfFiles();
            }
        }
    }
}

The snapshot below (before refactoring class names) shows the number of scenarios (equivalent of unit tests / asserts in NUnit or unit testing tools) that are now possible with this approach.



For example, you could test if the path is returned correctly with Is.SamePath or can test the number of files existing or if a particular file has a required extension and the possibilities are endless.