Wednesday, November 30, 2016

Repository Pattern - Example

This is the continuation of the previous post. Here I will be discussing the approach I have taken to implement repository pattern in the same 2 page bank application, we have discussing in couple of previous posts.


Except the Presentation Layer all the other projects are .net class library projects. Lets discuss each, one by one:

  • Entities Layer source code:
    • Define a base class Entity.
    • Make all the entity classes inherit from the calss Entity.
      • AccounHolder
      • Account
      • Transaction

    • Create context for all the entities
  • Repository Layer source code:
    • Create a generic interface called IGenericRepository

    • Create a generic repository called GenericRepository which implements IGenericRepository interface.

    • Make following repositories classes (which inherit from BaseRepository class) for each of the database entities.
      • AccoutHolderRepository
      • AccountRepository
      • TransactionRepository

  • Business Layer source code:
    • Create generic interface IBaseService Centralises Data Access logic or Web Service Access logic.

    • Create a generic base class which implements IBaseService interface created above and takes two generic parameter:
      • "T" for the entity type
      • "R" for the repository type
    • Create service class for each for database entities, and make them inherit the BaseService class created above.
      • AccountHoldersService
      • AccountsService
      • TransactionsService
  • Presentation Layer source code:
    • Below is the example of how the business layer is being called from Presentation layer 
In current application the database context is being moved to Repository Layer from Business Layer, now Business Layer can be unit tested by mocking repository. So now, we can confidently say, introducing Repository Layer between DAL and BL, removes the tight coupling between them. 



There is still an issue with this approach, if I want to introduce Unit of Work with Repository, it will be really hard to implement that. In my next post I will be refactoring this to use only the Generic Repository and will be covering, how Unit Of Work pattern can be used with Repository pattern to get rid of concurrency issues, that comes when multiple repositories are being used in a single transaction. 

PS: Repository pattern can be used with or without the Unit of Work pattern.

Repository Pattern

A mediator between Data Access Layer (a Database, a SharePoint list, or a web service) and Business Logic Layer of the application. It has following benefits:
  • Centralises Data Access logic or Web Service Access logic.
  • Repository introduces an abstraction layer between the DAL and BL, which makes BL unit testable by mocking the repository, which was difficult before due to tight coupling between the DAL and BL.


Implementation Approach: Repository pattern can be implemented using two approaches:

  • One Repository per entity– each entity of the database will have its own repository.
  • Generic Repository – One generic repository which will be used by all the entities of the database. 
Lets take the same bank application example from previous post and introduce repository pattern in it. I have used the mix of above two approaches, where 

  • I create an abstract generic repository that has the common behaviours which all the database entities support.
  • I also create one repository (which inherits from generic repository created above) for each entity of database.

The next post is going to show the source code details of this example.

PS: Repository pattern can be used with or without the Unit of Work pattern. 


Tuesday, November 29, 2016

Example: N-Tier Application

Problem Statement: It has two parts: 
  • Create a two page Bank Application where first page has two button one to add data into the database second to take you to the second page which shows the account details.
  • Each account holder has two accounts and each account has minimum 5 transactions.
 Solution Approach: We will be needing following items:
  • Two ASP.NET web pages.
  • Entities: AccountHolder, Account, Transaction. 
  • A way to do CRUD operation for items in Database.
Lets create n-tier application and divide above items into the different layers based on the previous post:
  • Entities Layer
    • Define POCOs 
      • AccountHolder.cs
      • Account.cs
      • Transaction.cs 
    • These classes represent Database tables as well as model for Presentation layer.
  • Data Access Layer
    • Use Entity Framework and define context to access database. (we are using Entity Framework code first approach) 
  • Business Components Layer
    • Contains core operations for the data access layer methods, using following contracts (interfaces) and classes. The interfaces defines the different core operations as methods and classes has the implementation of those methods. 
      • IBaseService
      • BaseService
      • IAccountHoldersService.cs
      • IAccountsService.cs
      • ITrsancationsService.cs
      • AccountHoldersService.cs
      • AccountsService.cs
      • TrasactionsService.cs
  • Services Layer 
    • As of now for this application, the Services layer is not required since we are not sharing any data with other systems. In case the data needs to be shared  with other systems, we will create either a Web Service or WCF service or ASP.NET Web API. In case we are using ASP.NET Web APIs to create this layer, it will have following controller classes:
      • AccountHoldersController.cs
      • AccountsController,cs
      • TransactiosController.cs
  • Presentation Layer
    • Use ASP.NET WebForms
    • The MainPage.aspx and TransactionDetails.aspx






You can download the Visual Studio solution for the above Bank Application from here: Bank Application using N-Tier Architecture.

Monday, November 28, 2016

Blocks in N-Tier Application

Typically any n-tier application has following stacks:
  • Entities Layer  
    • This layer is always a single or a bunch of "Class Library"project type in Visual Studio. 
    • Contains all the entities used in all the other projects of the application. 
    • The classes in this layer represents table mapping from database (Entity framework LINQ to SQL), Data Transfer Objects (DTOs) or ASP.NET MVC Models. 
    • Most of the classes in this layer are POCO (Plain Old C# Objects) 
  • Data Access Layer
    • Some times referred as Persistence Layer.
    • This layer is represented by "Class Library" project type in Visual Studio.
    • Contains CRUD operations for items in database. 
    • You can use technologies like ADO.NET, LINQ to SQL, Entity Framework etc in this layer. 
    • The entities, database mapping classes needed for the CRUD operations will be referenced from the Entities Layer. 
    • Only Business Layer should have access to this layer. 
  • Business Components Layer
    • Sometimes also referred as Domain Layer.
    • This layer is represented by "Class Library" project type in Visual Studio.
    • Contains core functionality (converting arrays to lists, mathematical calculations or variable conversions) of the application. Basically holds the custom logic that is applied on the methods that are exposed from DAL before they are referenced in the Service Layer. In simple words this layer contains Repositories & UnitOfWork which are responsible of converting the Data objects into POCO Entities.
    • Entities needed for operations will be referenced from the Entities Layer and database operation methods will be referenced from  DAL.
    • Only Service Layer or Presentation Layer (In  case Service Layer is not present) should have access to this layer.
  • Services Layer
    • Also referred as  Application Layer.
    • This Layer provides ways to expose Business Components Layer as an API to third part systems.
    • This layer is either self hosted or hosted in web servers like IIS.
    • You can use technologies like WCF Service, Web Services, ASP.NET Web API in this layer.
    • This layer can only be shared with Presentation Layer or any other Third Party Systems.
    • This layer is required only when your application is sharing data with another system, else its not required.
  • Presentation Layer
    • This layer represents the user interface of the application.
    • This is hosted in web server like IIS.
    • To get the data from database, reference either Business Layer or Service Layer. Never ever make a direct call to database or DAL from this layer. 
    • You can use technologies like ASP.NET WebForms, ASP.NET MVC, AngularJS etc in this layer.
  • Common Components Layer
    • This layer is represented by "Class Library" project type in Visual Studio.
    • This is the optional layer, and cannot have dependencies in any of the above defined layers because this layer can be compiled and shared with multiple applications.  
    • Contains common libraries or functionalities that can be used in any of the above layers.
    • Example of some of the common functionalities are validation functionality, security libraries, encryption tools or exception logging classes.


My next post, shows all the above blocks in the form of a simple 2 page Bank Application using .NET Framework.

N-tier vs N-Layer

N-tier Architecture


N-tier (multi-tier) architecture is a client – server architecture in which presentation, application processing, and data management functions are physically separated. The most widespread use of multi-tier architecture is the three-tier architecture. A three-tier architecture is typically composed of
  • Presentation Tier
  • Domain Logic Tier (Business Tier) 
  • Data Storage tier (Data Tier)
Example: In web development field, an eCommerce website is often referred to as built in using three-tiers:
  • A front-end web server serving static content, and potentially some cached dynamic content. In web based application, Front End is the content rendered by the browser.
  • A dynamic content processing and generation level application server (e.g., ASP.NET)
  • A back-end database or data store – comprising both data sets and the database management system software that manages and provides access to the data.
N-layer Architecture

N-Layer (Multi-Layer) Architecture – A software architecture that uses many layers for allocating the different responsibilities of a software product.
  • Presentation layer (a.k.a. UI layer, view layer, presentation tier in multitier architecture)
  • Application layer (a.k.a. service layer or Controller Layer)
  • Business layer (a.k.a. business logic layer (BLL), domain layer)
    •  The part of the program that encodes the real-world business rules that determine how data can be created, displayed, stored, and changed.
  • Data access layer (a.k.a. persistence layer, logging, networking, and other services which are required to support a particular business layer.
    • Any software layer that makes it easier for a program to persist its state is generically called a persistence layer. Most persistence layers will not achieve persistence directly but will use an underlying database management system.

N-tier vs N-layer

Tier indicates a physical separation of components, which may mean different assemblies such as DLL, EXE, etc. on the same server or multiple servers. Layer indicates logical separation of components, such as having distinct namespaces and classes for the Database Access Layer, Business Logic Layer and User Interface Layer. 

Thursday, November 24, 2016

LINQ to Objects

LINQ to Objects - Refers to the use of LINQ queries with any IEnumerable or IEnumerable<T> collection directly without the use of an intermediate LINQ provider such as LINQ to SQL or LINQ to XML. You can use LINQ to query any enumerable collections such as List<T>, Array or Dictionary<Tkey, TValue>. The collection can be user defined or returned by a .NET framework API.

LINQ queries advantages over traditional foreach loops:
  • In LINQ queries, you write declarative code that describe what you want to retrieve unlike old ways where you had to write complex foreach loops that specified how to retrieve data from collection. 
  • Filtering data using LINQ queries is more concise and readable, especially when filtering multiple conditions.
  • LINQ queries provide powerful filtering, ordering, and grouping capabilities with a minimum of application code.
  • LINQ queries can be ported to other data sources with little or no modification.
In general, the more complex the operation you want to perform on the data, the more benefits you will realize using LINQ compare to traditional iteration technique.

Below are the few examples where LINQ to Objects can be applied.
  • LINQ and String LINQ can be used to query and transform strings and collections of strings. It can be especially useful with semi-structured data in text files. LINQ queries can be combined with traditional string functions and regular expressions. Example:
To see more examples of "LINQ and String"  refer MSDN.
  • LINQ and Non-generic Collection When using LINQ to query non-generic IEnumerable collections such as ArrayList, you must explicitly declare the type of the range variable to reflect the specific type of the objects in the collection. Example:
  • LINQ and Custom Methods - We can add custom methods for the LINQ queries by adding extension methods to IEnumerable<T> interface. Few example of such existing methods are Distinct<TSource>, Skip<TSource> and Reverse<TSource> etc. Below is the example of new custom method (Extension Method) and this method is only available for IEnumerable<double> not for any IEnumerable<T>. 

To make the above method available for any type, make a generic overload of same method by making source as IEnumerable<T> and adding a new parameter of type delegate Func<T, TResult>, this delegate takes an object of type T and returns double.
  • LINQ and File Directories - Many file system operations are essentially the queries and are therefore well-suited to the LINQ approach. To view the various examples of  "LINQ & File Directories" refer MSDN. 

Wednesday, November 23, 2016

LINQ - Query Syntax, Method Syntax and Lamda Expressions

There are two basic ways to write a LINQ query to IEnumerable collection or IQueryable data sources.

  • Query Syntax - aka Query Expression Syntax 
    • This is similar to SQL (Structured Query Language) for the database.
    • The LINQ query starts with the From clause and ends with the Select or GroupBy clause.
    • Implicitly typed variable - var can be used to hold the result of the LINQ query.
    • You can use various query operators (filtering, joining, grouping. sorting etc.) to construct the desired result.
  • Method Syntax  - aka Method extension syntax or Fluent
    • Uses extension method included in the Enumerable and Queryable static class.
 
    • Method syntax comprises of extension methods and Lamda expressions.
    • Implicitly typed variable - var can be used to hold the result of the LINQ query. 
    Lamda Expressions - A shorter way of representing anonymous methods using special syntax. In the above example. 

    • Syntax: parameter => body expression

    In below example i is the parameter , => is the Lamda operator, and (i%2) == 0 is the body expression.
    • Can have zero parameters.
    • Can have multiple parameters.
    • Can have multiple statements in body expression.
    • Can be assigned to generic delegates Func, Acton or Predicate.
    • Can be invoked same way as delegate.

    LINQ - Introduction

    LINQ (Language Integrated Query) - It was introduced with .NET framework 3.5 & visual studio 2008. It bridges the gap between the worlds of objects and the words of data. Time to time various languages have been introduced to query (an expression which retrieves data from data source) data from various data sources like SQL for Relational Database, XQuery for XML and so on. Hence, developers had to learn a new query language for each type of data sources or data formats, that they will be willing to use in their application. LINQ simplifies this situation by offering consistent model for working with data across various kinds of data sources or formats. 


    Architecture  -  LINQ has 3 layer architecture:

    1. Language extension for LINQ in .NET
    2. LINQ Providers - (Example: LINQ to Objects, LINQ to SQL, LINQ to XML etc)
    3. LINQ enabled Data Source - Objects implementing IEnumerable<T> or IQueryable<T> 
    Query Operation  - All LINQ query consists of three distinct actions:

    1. Obtain the data source.
    2. Create the query.
    3. Execute the query

    Example: querying an integer array to get the numbers which are divisible by 2. Here the query returns the IEnumerable<T> object.
    • The Data SourceThe basic rule: A LINQ data source is any object that supports the generic IEnumerable<T> interface, or an interface that inherits from it.
    • The Query - In LINQ, the query variable itself takes no action and returns no data. It just stores the information that is required to produce the results when the query is executed at some later point.
    • Query Execution
      • The query variable only stores the query commands, the actual execution of the query is deferred until you iterate over the query variable in a foreach statement. This concept is referred to as deferred execution (refer the above code for example). Since the query object doesn't hold the query results, you can execute it as often as you like
      • Queries that perform aggregation functions (Count, Max, Average, and First) over a range of source elements execute without an explicit foreach statement (the foreach is already been used internally to obtain the result). This is called the force immediate execution and these types of queries return a single value, not an IEnumerable collection. You can also force immediate execution to the query in above example by calling the ToList or ToArray, this also caches all the data in a single collection object.
    Syntax - There are two basic ways to write LINQ quereis:


    • Query Syntax - Aka Comprehension Syntax or Query Expression
    • Method Syntax - Aka Method Extension Syntax or Fluent
    LINQ Providers 
    • LINQ to Objects (Not a provider actually, it refres to the use of LINQ queries with any IEnumerable or IEumerable<T> collection directly) 
    • LINQ to XML (Formally known as XLINQ)
    • ADO.NET LINQ Technologies
      • LINQ to DataSet
      • LINQ to SQL (formally known as DLINQ)
      • LINQ to Entities
    There are many third party LINQ providers are also available like LINQ to Parallel (PLINQ) , LINQ to Excel, and LINQ to JavaScript, etc. 


    Advantages: 
    • Integrate
      • LINQ offers IntelliSense which means writing more accurate queries easily.
      • LINQ offers syntax highlighting that proves helpful to find out mistakes during design time.
      • Increases development time, as the syntax highlighting and IntelliSense support allow you to get more work done in less time.
      • LINQ code is shorter and cleaner than traditional techniques for querying data and, therefore, is much easier to maintain.
      • Debugging is easy due to its integration in the C# language.
    • Unitive 
      • Before LINQ, developers who queried data frequently needed to master multiple technologies. For example: 
        • SQL to query a database
        • XPath, Dom, XSLT, or XQuery to query and transform XML data
        • Web services to access some forms of remote data source
        • Looping and branching to query the collections in their own programs
        LINQ simplifies these tasks by providing a single, unified method for querying diverse types of data. Developers don’t have to master a new technology simply because they want to query a new data source. Now using LINQ, they can call on their knowledge of querying local collections when they query relational data, and vice versa.



      • The code becomes easier to maintain, because you are using the same syntax regardless of the type of data you query.
    • Extensible - It is possible to use knowledge of LINQ to querying new data source types.
    • Declarative
      • Because LINQ is declarative, it usually allows you to write concise code that is easy to understand and maintain.
      • It allows developers to simply state what they want to do without worrying about how it is done.
    • Hierarchical - Viewing relationship between two tables is easy with LINQ due to its hierarchical feature and this enables composing queries joining multiple tables in less time.
    • Composable
      • LINQ offers the facility of joining several data sources in a single query or in a series of related query. 
      • It's easy to break complex problems into a set of short comprehensible queries that are easy to debug.
    • Transformative - LINQ make it easy to convert data of one type into a second type. For instance, you can easily transform SQL data into XML data using LINQ.

    Tuesday, November 22, 2016

    IEnumerable vs IQueryable

    You might have seen IEnumerable, IEnumerable<T> and IQueryable, IQueryable<T> being used in lot of LINQ queries. Today we are going to discuss these items in details.
    • IEnumerable - Exposes an enumerator, which supports a simple iteration over a non-generic collection. (what it means in code is iterating a collection using foreach loop)
      • Extensively used in .NET non-generic collections.
    • IEnumerable<T> - Exposes an enumerator, which supports a simple iteration over a collection of a specified type. "T" represents the type of data to enumerate. 
      • Derived from IEnumerable interface.
      • Extensively used in .NET generic collections.
      • Since interfaces can't be extend using extension methods, the static class Enumerable is used for extending it by adding the Extension Methods. Example: Select, Any, Join, Count etc.
    • Enumerable - Provides a set of static methods for querying objects that implements System.Collections.Generic.IEnumerable<T>
    • IQueryable - Provide functionality to evaluate queries against a specific data source wherein the type of data is not specified.
      • Derived from IEnumerable interface.
      • Third party data providers must implement IQueryable or IQueryable<T> interface, in order to support LINQ.
    • IQuerable<T>Provide functionality to evaluate queries against a specific data source wherein the type of data is know. "T" represents the type of data in the data source.  
      • Derived from IEnumerable, IEnumerable<T>, and IQueryable interfaces.
      • Since interfaces can't be extended using extension methods, the static class Queryable is used for extending it by adding the Extension Methods. Example: Select, Any, Join, Count etc.
    • Queryable - Provides a set of static methods for querying objects that implements System.Linq.IQueryable<T>
    Similarity between IEnumerable and IQuerable
    • Both can move forward only over a collection, they can’t move backward and between the items.
      Difference between IEnumerable and IQuerable
      • Namespace - IEnumerable is in System.Collections, where as IQuerable is in System.Linq.
      • Derived from - No base interface for IEnumerable where as IQueryable is derived from IEnumerable.
      • How does it work - While querying data from database, IEnumerable executes select query on the server, load data in-memory on client side and then filters data, where as IQuerable executes query on server side with all filters. Since IEnumerable does more work, it is slow compare to IQuerable.
      • Suitable for - IEneumerable is suitable for LINQ to Object and LINQ to XML queries, where as IQuerable is suitable for LINQ to SQL queries.
      • When to use
        • Use IEnumerable while querying data in-memory collections, example:List, Array etc. Use IQuerable data from out-memory (like remote databse or services) collections. 
        • Use IQuerable when you want to apply filter before the query is being executed. 
        • Use IQuerable, when lazy loading is needed (you get what you need, not the entire list of records).
      • Extension method parameter - IEnumerable uses Func, where as IQuerable uses Expression Tree.
      • Custom query - IEnumerable doesn't support where as IQueryable supports using CreateQuery and Execute methods.
      • Deferred Execution - Supported in both.
      • Lazy loading - IEnemerable doesn't support lazy loading hence it is not suitable for paging like scenarios where as IQuerable supports lazy loading hence suitable for paging like scenarios.
      • Best uses - IEnumerable is best used in in-memory traversal. IQueryable is best used in paging  like scenarios. 
      Example: Lets say we have a need to get the first 10 employees from the database and the database contains 1000 employees.
      • If we use IEnumerable to get the first 10 employees, we will be end up loading all the 1000 employees and then selecting first 10.
                 IEnumerable<Employee> employee = dbContext.Employees.Take(10).ToList();
      •  If we use IQuerable, we get only first 10 emplyees from the query which saves a lot of resource.
                 IQuerable<Employee> employee = dbContext.Employees.Take(10).ToList();

        Saturday, November 19, 2016

        Brain Teaser - Lamda Expressions

        In this post, I will be showing how we can we replace Delegate with lambda expression from the code in the previous post .
        • Keep the car class as it was in previous post.

        • Inside Audi and Benz classes, use delegate with anonymous methods using Lamda expressions as below.   
           


        • Now make use of the above classes and see the output

                    

        Brain Teaser - Delegates

        In this post, I will be showing how we can further optimise the code from previous post by replacing Generics with Delegate.
        • Define two delegates in Car class one for method Move() and another for method ApplyBreak() and invoke both of them inside Drive method as shown below.

        • Make classes Benz and Audi use Car class as shown below.       



           
        • Now make use of the above classes and see the output

                    

        Friday, November 18, 2016

        Brain Teaser - Generics

        In the previous post we had optimised the code using Inheritance, in current post we will see how we can optimise previous solution using Generics. Lets first understand what is Generics in .Net. Generics are introduced in C# 2.0 and it allows us to create data structures which are decoupled from data types. In simple words you can say Generics is a technique of writing the code for a class without specifying the data type(s) that the class works on. The data type is specified only when you declare an instance of a generic class. This allows a generic class to be specialised for many different data types while only having to write the class once. The best example of Generics is .NET collection classes (System.Collections.Generic) where Generics is being used heavily. You can also create your own generic interfaces, classes , methods, events and delegates. Well Generics can be applied to:
        • Interface
        • Abstract class
        • Class
        • Method
        • Static method
        • Property
        • Event
        • Delegates
        • Operator
        Syntax for defining a Generic Class or Method: Use angular brackets  <>  to define the generic classes, methods, properties, events, delegates with type parameter. You can use any alphabet to specify the type parameter, but the best practice says you should use T for specifying the type parameter.. If you have more than one type parameter prefix descriptive type parameter names with "T".
        class ClassName<T> {}
        void MethodName<T>(T parameter) {}
        class ClassName<TKey, TValue> {}

        Advantages of using Generics:
        • Maximises code reuse
          • Generic makes the code (classes, methods, interface, delegate) independent of data types. So the same piece of code can be used for N numbers of types which poses the same behaviours. Example below method can be used both with string as well as int.
                                 bool AreEqual<T>(T value1, T value2)
                                 {
                                        return value1.Equals(value2);
                                  }
          •  Like methods, classes, interfaces and delegates can be made generic
                                    public class MyGenericClass<T>
                                   {
                                         public bool AreEqual(T value1, T value2)
                                        {
                                               return value1.Equals(value2);
                                        }
                                    }
        • Type safety
          • Generics are strongly typed, you are notified during compile time if you try to use different data type of data than the specified in the definition. See the below code which uses the Generic class used in the above example.
                                  MyGenericClass<int> c = new  MyGenericClass<int> (); //definition
                                  c.AreEqual(1, "test"); // this line of code is gonna give compile time error.
        • Performance Improvement
          • Boxing (converting value type to reference type) and un-boxing (converting reference type to value type) overhead is no longer there. 

        I will be covering Generics in detail in my future posts. So for now lets move on to our code optimisation task. 
        • Move the methods which has same name but different implementation in Audi and Benz classes as below:

                     public interface ICar
                    {
                          void Move();
                          void ApplyBreak();
                    }  
        • Leave the common code in Vehicle class and make it a generic class as below:
                    public class Car<T> where T: class, ICar, new()
                   {
                         private T car;
                         public Vehicle()
                        {
                            car = new T();
                        }

                         protected void Start()
                        {
                               Console.WriteLine("Car Started");
                         }

                        public void Drive()
                       {
                             Start();
                             car.Move();
                             car.ApplyBreak();
                       }    
                   }
        • Make Benz and Audi class implement the interface created above IVehicle as below:
                     public class Benz :ICar
                    {
                     
                         //Benz specific functionality
                         public  void Move()
                        {
                                 Console.WriteLine("Moving with the power of a 3000 CC Diesel engine");
                        }

                        //Benz specific functionality
                      public void ApplyBreak()
                     {
                           Console.WriteLine("Applied Power break with ABS");
                     }
                  }

                public class Audi: ICar
               {
                      //Audi specific functionality
                     public void Move()
                    {
                        Console.WriteLine("Moving with the power of a 2000 CC Pterol engine");
                    }

                     //Audi specific functionality
                     public void ApplyBreak()
                    {
                         Console.WriteLine("Applied Power break with sensors");
                    }
               }

        • Now view the output:
                   class Program
                   {
                        static void Main(string[] args)
                       {
                           Car<Audi>  a = new Car<Audi>();
                           a.Drive();

                           var b= new Car<Benz>();
                           b.Drive();

                          Console.Read();
                       }
                   }