Saturday, December 31, 2016

C # Interfaces



To those are working in IT may be technical or not , in their life they will come across term interface,
and almost every one know what does it mean i.e definition syntax and how to and when to use. Most of the coding guys say it  "as classes do not support multiple inheritance and it will be possible in interfaces"  and we say they don't have implementation only definition and no declaration.


So lets learn after what we know,


Interfaces are considered as a contract consisting of properties and methods  and events, when a class implementing the interface it makes a commitment i.e fulfilling contract by implementing ALL(properties also) its interface members 
By default all  members are public , we cannot specify access modifiers to interface members 

An interface is similar to PURELY abstract class i.e class containing all abstract members (i.e no implementation)



Benefits of using interface is that code can be easily Maintainable

When your GetPeople implementation changes i.e if it returns List<Person> then again you need to change your implementation in 1st case as we get compilation error
but we did not have to change the interface implemented code, thats because Person[] and List<Person> both Implement IEnumerable

so when we are coding to the abstraction we don't care about the specific class coming back.All we do is its is a class that fulfills the contract , which makes our code more resistant .As every collection in .NET implements IEnumerable our repository may be stack, queue or whatever ...everything will work exactly without any change.. SO thats thing about programming to abstraction if the underlying implementation changes our code doesn't care 

We can also strongly type the collection like IEnumerable<Person>

So when we are programming to abstraction , we're adhering to a contract , but we don't care about implementation details i.e Get the functionality you need and not bothering about the specific type.

How:

we want code that is extensible that can respond quickly to new requirements , if we come up with a contract that the core application can use then we can plug in any number of different implementations that adhere to that contract 

Improving extensibility :
 For  example say you are getting data from repository using Service...
Generally we will use repository pattern in coding i.e BL repository (May be using Services, or CSV or SQL Connection).. this may vary so what we do is.. create an interface with what is required i.e functionalities so we create an interface with CRUD operations we want and then we use it in presentation layer .. so that ur UI doesn't care about actual implementation whether using services or csv or what ever no change in using that ()

i.e what ever the class obeying the interface contract can be used

Sample service repo :


Without using interfaces: we have to create objects for each type and use them ..redundant code ... so when we care  only about operations(features provided) and don't bother about underlying implementation it really helps in extending your code with out actually disturbing the usage/Consuming in your  presentation layer


** When an interface is implemented in a class , its access specifier should be public only otherwise it'll throw error
** IF you have Interface I1 with Method M1 and Interface I2 with M2 and when you inherit I2 in your class then you have to provide implementation for both M1 and M2.

Explicit Interface Implementation 





so why explicit implementation? 

If you have 2 interfaces to be implemented you can have it with single method also but why explicity defining / implementing them as shown above?
Because when two interfaces you have inherit have same method name but different return types then this explicit implementation is mandatory

for example when you inherit IEnumerable<T> then you can see as (internally IEnumerable<T> inherits IEnumerable) then you have to provide implementation for both GetEnumerator and GetEnumerator<T>

Explicit implementation of interface member should not have Access modifier  

Program to an abstraction rather than a concrete type i.e -->Program to an interface rather than a concrete class


Actual Usage:

So as we have seen above when we use interface we actually bother about the functionality provided and not the type which is serving .. so we have minimal change in presentation layer but we have to pass the type we want to connect and get data like in prev example either csv or sql or whatever

So we have to pass from UI which type you would like to and your factory class will instantiate that class and return that object (Interface )

1.So how can we make zero impact on UI , not even passing type
2.In the example what ever may be the type either from service / csv / sql we have to load all three assemblies as wen instantiate them in factory class

Suppose if we  get new type then we have to change UI and also add reference to our repo factory class project build it and then change the repo factory ...


We can avoid all above using Dynamic Loading

i.e no need to pass type from UI or load assemblies

1.We wont pass from UI we'll pick from configuration key of web config
2.We manually put the dll in bin folder of project to pick them which will actually happen when we add a reference (we can have a post build event for that or if you dont want to give new build you can directly place them in bin folder )


like wise for others also we can enable what ever configuration you want to use
To dynamically load the dlls or assemblies



So now no need to change any code level implementation nor give any new build...
Just Change the web.config with new app settings and place the dlls of your new implementation type in bin folder... bang!! ... done with ZERO code level impact or change


Conclusion: So, on a final note what we all to remember is interface is commitment and contract :-)

Sunday, October 2, 2016

LINQ - Method Syntax

It is a technology that give you the ability to query data from C# i.e  data imply object that are in memory on heap , objects from database or xml or any data-source.

Provides almost 50+ query operators which include Sorting, Filtering , joining , grouping, partitioning, projecting , aggregating etc ..

So on what types of data we can apply querying ? 

Lowest level data type that was chosen is IEnumerable<T>

But we don't have  interesting methods to query data we only have methods to loop through ,so to define new methods to IEnumerable<T>, Microsoft took extension methods i.e by extending it.. extension method is a method that looks like a member of a given type but in reality its a STATIC method on a different type

It may Seem Why Extension methods ... when we have inheritance for example we have a class called DateTime which cannot be inherited because of its sealed property so what to do if I have to have additional functionality like some calculation or custom logic through out the code ....

What LINQ can do?

We can easily get the things like 
what is the average invoice  amount customer,list of all customers with greater than 35 or starting with A etc , all these SQL type of querying can be done on data sources.

On What DataSources we can apply this is which have LINQ Providers,
DataSouces which have Linq providers are 

SQL DataSources,Entities(EF), Objects(In Memory Data), XML

We have 2 diff types of LINQ syntax  Query Syntax and Method Syntax



This Code looks like query iterates over the list of customers and finds all the customers that matches id and uses first method to return first element of sequence. BUT it is not Happening



Linq uses something called deferred execution  i.e code defining the linq statement is doing just that i.e defining statement only ...the Linq query itself is not executed until the result is required.

Calling any operator or method on query that must iterate over the result will cause query to execute.. here above when written .First() method the result is executed which is performance level issue.

The above is linq query syntax i,e built in to language i.e C# or VB and not Dot NEt CLR , therefore the query syntax should be translated in method calls for CLR when code is compiled. Instead of using linq query syntax we can call those methods directly using Linq Method syntax , since those methods are implemented as extension methods 

Extension Methods : An extension method is a method on class that extends its functionality without modifying or recompiling th original class .
An extension method is a static method on a static class , the first parameter should be the type that should be extended and also should be prefixed with this keyword.


















Usage:













Extension methods have a small down arrow indication.

Sorting:

Ordering Operators:
 OrderBy
 OrderByDescending,
 ThenBy,
 ThenByDescending,
 Reverse.

the execution of query expression using these operators is deferred until the code requests an item from resulting sequence  

return customerList.OrderBy(c => c.LastName)
  .ThenBy(c=> c.FirstName);

If you want to order by two columns then we have to use thenby i.e if you use orderby two times then it gets sort by last orderby column only.. Such Cases we have to use ThenBy

Same if you want descending then  .. we have to use OrderByDescending 

We can handle nulls by making the datatype nullable and we can make where these null values can appear i.e either starting or at the ending i.e 


return customerList.OrderByDescending(c => c.CustomerTypeId.HasValue)   //This will make nulls to come at end
  .ThenBy(c=>c.CustomerTypeId); 

  Creating:

We may get a scenario to create enumerable sequence, creating seq of vlaues, Linq Generation operators are 
Range, -- Creates sequence of integral numbers within a specified range 
Repeat, -- Generates a sequence of one repeated value 
These Operators are static methods in Enumerable Class but not Extension Methods and Not Deferred i.e Sequence is immediately built. Using these we can create Sequential series or random series 

var seq1 = Enumerable.Range(0, 10);  // We get an integer array of size 10 with values from 0 to 9
  var seq2 = Enumerable.Range(0, 10)
  .Select(i => i * i); // We can Have some custom logic implemented inside 

We can write multiple statements inside a Lambda Expression as below
Enumerable.Range(0, 10)
  .Select(i =>
  {
  var k = 0;
  return i * i + k;
  });

var integers = Enumerable.Repeat(-1, 10);  //  Creates integer array of value '-1 ' ten times.

Random rand = new Random();
rand.Next(0,26)  creates a new Random number ranging between 0 and 25 
var strings = Enumerable.Range(0, 10)
  .Select(i => ((char)('A' + rand.Next(0, 26))).ToString());

Comparing:

comparing sequence of objects, like comparing processed list to original list  determining the difference of records or performing some data synchronization  (All are extension methods)

Intresect,  --This operator produces set intersection of two sequences ; basically defining elements seq1.Intersect(seq2)
 Except, seq1.Except(seq2)
 Concat,-- Merging  , seq1.Concat(seq2);
 Distinct , seq1.Concat(seq2).Distinct();
 Union .- By defining he unique item for merging sequences   seq1.Union(seq2);

Projection : Refers to operation of Transforming an object in to new form , i.e using SELECT or SELCTMany operators 
If we donot provide projection operators then the type returned from query will be sequence of items of type defined in original sequence 

If we provide Projection operators , the result will be a sequence that is shaped differently from sequence we have passed 

Select
SlectMany ,  projects multiple sequences based on transform function and flatens in to one sequence


Examples of Select:

public IEnumerable <string > GetNames(List<Customer> customerList)
        {
            var query = customerList.Select(c => c.LastName + ", " + c.FirstName);
            return query;
        }

Defining anonymous types in Linq using new keyword.

public dynamic GetNamesAndEmail(List<Customer> customerList)
        {
            var query = customerList.Select(c => new
                            {
                                Name = c.LastName + ", " + c.FirstName,
                                c.EmailAddress
                            });
           foreach (var item in query)
            {
                Console.WriteLine(item.Name + ":" + item.EmailAddress);
            }
            return query;
        }

Using JOINS

var query = customerList.Join(customerTypeList,
                                c => c.CustomerTypeId,
                                ct => ct.CustomerTypeId,
                                (c, ct) => new
                                {
                                    Name = c.LastName + ", " + c.FirstName,
                                    CustomerTypeName = ct.TypeName
                                });

types When a collection have fields in which one of the child is again a collection and you want to filter the Parent / Master collection using child (i.e inner filed values) we can use SelectMany

Parent Object has as collection of related or child objects

eg: A customer(Parent object )as set of invoices(which is also a collection ) or shoppping cart as items purchased
WE MAY NEED TO FIND INFORMATION ON THE PARENT OBJECT USING CHILD DATA PROPERTIES 

eg: Total price of items in the cart , customers based on invoice info(i.e not paid etc)

We can also do with select ..

For Example we have Customers and we have to find out customers with OverDue Invoice i.e not paid then we have to check the child object condition..
doing so will return an enumerable of enumerable of invoices and not customers as follows

public IEnumerable <IEnumerable <Invoice >> GetOverdueCustomers1(List<Customer> customerList)
        {
            var query = customerList
                        .Select(c => c.InvoiceList
                                    .Where(i => (i.IsPaid ?? false) == false));
            return query;
        }

but we need customers and not invoices and that too only IEnumerable , we can write above as follows

public IEnumerable <Invoice > GetOverdueCustomers1(List<Customer> customerList)
        {
            var query = customerList
                        .SelectMany(c => c.InvoiceList
                                    .Where(i => (i.IsPaid ?? false) == false));
            return query;
        }

But to get List of customers we have to use SELECT MANY so that result selector is type of customer

public IEnumerable <Customer > GetOverdueCustomers(List<Customer> customerList)
        {
            var query = customerList
                        .SelectMany(c => c.InvoiceList
                                    .Where(i => (i.IsPaid ?? false) == false),
                                    (c,i)=> c).Distinct();
            return query;
        }

Imp: As Linq is Deferred execution i.e not gets executed until we use i.e IEnumerable is Lazy Loading when we directly assign to a datasource we will not get any data , so We have to use .ToList()

CustomerGridView.DataSource = customerRepository.SortByName(customerList).ToList();

Imp: When we have anonymous type then after retrieving when we assign directly to a datasource it throws error as cannot convert object to IEnumerable so we need to convert the query itself to .ToList() before passing.

.ToList() must be used only at the time of assigning because that will cause the query to execute.


GroupBy :

public dynamic GetInvoiceTotalByIsPaid(List<Invoice> invoiceList)
        {
            var query = invoiceList.GroupBy(inv =>inv.IsPaid ?? false,
                                            inv => inv.TotalAmount,
                                            (groupKey, invTotal) => new
                                            {
                                                Key= groupKey,
                                                InvoicedAmount = invTotal.Sum()
                                            });
            foreach ( var item in query)
            {
                Console.WriteLine(item.Key + ": " + item.InvoicedAmount);
            }

            return query;
        }
Here we want to get total amount of invoices based on the invoices that got paid and not paid... i.e group invoices by IsPaid property ... so 
1st param is on which prop you would like to group by and then what result  you would like to see i.e what column aggregation you want and then the result set ...

NOT MUCH IN RESULT SET generally for join like statement you will take 1st datasource and 2nd datasource here the parameters are GROUP BY  KEY AND AGGREGATE COLUMN WE mentioned ..

If we want to group by 2 columns then the 1st parameter is the object (using new keyword)

 var query = invoiceList.GroupBy(inv => new
                            {
                                IsPaid = inv.IsPaid ?? false,
                                InvoiceMonth = inv.InvoiceDate.ToString( "MMMM")
                            },
                                            inv => inv.TotalAmount,
                                            (groupKey, invTotal) => new
                                            {
                                                Key = groupKey,
                                                InvoicedAmount = invTotal.Sum()
                                            });
            foreach ( var item in query)
            {
                Console.WriteLine(item.Key.IsPaid + "/" +
                                    item.Key.InvoiceMonth + ": " + item.InvoicedAmount);
            }


            return query;