Saturday, January 25, 2014

What is Scalability

What is Scalability 

Ability of a system, network or a process to handle growing amount of work in a capable manner. Scalability is important to make sure heavy loads that comes to the servers are handled without minimum or no downtime. 

Types of scalability


Horizontal scaling (Scale out)

Vertical scaling (Scale up)

Add resources to a single node of a system.



Guidelines for Scalability in Application Design (checklist)

Partitioning of the workload can be useful especially for database access. Load on the database will be manageable results with less complexity. Separation of concern a coding principle can be used to achieve this. Microservices also comes in handy for partioning of workload


Database Scalability 

http://www.boic.com/scalability.htm
http://msdn.microsoft.com/en-us/library/aa479364.aspx (Scaling out SQL server)

Server Scalability

There are different ways you can setup your servers to achieve high scalability. highscalability explains 10 different ways you can setup your server environment.

Resources

http://www.boic.com/scalability.htm
http://stackoverflow.com/questions/tagged/scalability
http://stackoverflow.com/questions/659970/why-is-it-not-advisable-to-have-the-database-and-web-server-on-the-same-machine

Model Binding in ASP.NET MVC

ASP.NET MVC Model Binding simplifies controller actions by automatically populating controller action parameters, taking care of property mapping and type conversion. 

Model binding consists of collecting values from the request and populating models with those values. These steps are accomplished by value providers and model binders. 


Value Providers

ASP.NET MVC includes value provider implementations that covers most common sources of request values such as Query String parameters, form fields and route data. You can even create your own custom value providers.

Value providers are registered in ValueProviderFactory. Following are some inherited value providers from ValueProviderFactory,
  • CookieValueProviderFactory
  • SessionValueProviderFactory
  • FormValueProviderFactory
  • JQueryFormValueProviderFactory
  • QueryStringValueProviderFactory


JSON Value Provider #
Enables MVC actions to send/receive JSON data and model bind JSON text to parameters of action method. This runs before model binding. JSON value provider serializes request data into a dictionary. Then this data is passed to the model binder.


Model Binders

ASP.NET MVC has default model binder DefaultModelBinder which maps browser request to a data object. It will go through each property in the target model and will check if it is a simple or a complex type. If it is a simple type, It'll try to convert it to a target type. If it is a complex type it will perform a recursive binding.


Resources

http://msdn.microsoft.com/en-us/magazine/hh781022.aspx
http://haacked.com/archive/2011/06/30/whatrsquos-the-difference-between-a-value-provider-and-model-binder.aspx/
http://www.codeproject.com/Tips/806415/Model-Binding-using-IModelBinder-and-DefaultModelB
http://stackoverflow.com/questions/8535802/when-to-use-imodelbinder-versus-defaultmodelbinder
http://chimera.labs.oreilly.com/books/1234000001708/ch13.html#_the_importance_of_models_in_asp_net_web_api

Wednesday, January 8, 2014

Monday, January 6, 2014

All about ASP.NET MVC

Today I thought of categorizing the articles I'm going to write regarding ASP.NET MVC. Wrote them down on my whiteboard so I can see clearly what are they. Here are links to already written articles.







Wednesday, January 1, 2014

Overloading, Overriding and hiding in C#

Overloading and overriding are concepts in polymorphism.

Overloading

Use overloading when multiple methods has same purpose but there is more than one way to do. Overloaded methods have different parameters.

E.g:
void ReadXml(string fileName) {  }
void ReadXml(Stream strm) {  }

An alternative to method overloading is to use optional parameters.
public void Foo(int x = 1)

Also use named parameters for readability.
t.Foo(x: 12);

You can also use params keyword to have unlimited no. of arguments of a specified type like below,\

        public int Foo(params int[] x)
        {
            return x.Sum();
        }

call it like,
t.Foo(1, 2, 3);

Overriding

Concept in which subclasses provides specific implementation of a method provided by the super class.

    class A
    {
        public virtual string Method()
        {
            return "A";
        }
    }

    class B : A
    {
        public override string Method()
        {
            return "B";
        }
    }

    class C : A
    {
        public string Method()
        {
            return "C";
        }
    }

            A a = new A();
            Console.WriteLine(a.Method()); //A

            A a2 = new B();
            Console.WriteLine(a2.Method()); //B

            B b = new B();
            Console.WriteLine(b.Method()); //B

            C c = new C();
            Console.WriteLine(c.Method()); //C

In the last example, c.Method() hides a.Method(). Therefore it gives a warning. To remove the warning append new in front of  public string Method()

http://stackoverflow.com/questions/3838553/overriding-vs-method-hiding

http://stackoverflow.com/questions/25008021/why-does-this-c-sharp-code-return-what-it-does/25008171#25008171

Strategy Pattern

Allows behavior of an algorithm to be selected at run time. The strategy pattern, 
  • defines family of algorithms,
  • encapsulate each algorithm and
  • makes algorithms interchangeable




Following is an example of a implementing strategy pattern.




Therefore the host class (The class that uses strategy, below the Context) class doesn't have to implement the details of the strategy. It will only use the needed strategy.

Difference with State Pattern

The strategy pattern deals with how an object performs a certain task. Encapsulating the implementation. Where as The state pattern deal with what state the object is in, wrapping state behavior. Other than that strategy pattern and state pattern are very similar.

Difference with Factory Pattern (DZone)

Strategy pattern may look very similar to factory but they are different. When using strategy if you take above example, Context just have a strategy property which gets injected. But if you use Factory, specifically a ContextFactory, The type is not generic anymore. Your implementation would a Either BusContext or CaraContext. 


Resources

Using IComparer, Comparer and IComparer in C#

IComparer

IComparer interface exposes a method to compare two objects. This interface comes under System.Collections namespace. You can use this with Array.Sort() method. To use with Generic classes you must use IComparer<T> interface.

You can check the example in MSDN documentation to see how to use IComparer with an Array.

IComparer<T>

This interface comes under System.Collections.Generic namespace. This is similar to IComparer. This interface is used with List<T>.Sort and List<T>.BinarySearch methods and provides a way to customize the sort order of a collection.

The StringComparer class implements this for String. 

IEqualityComparer<T>

Comparer<T>

This interface provides a base class for implementations of the IComparer<T> interface. 

It is recommended to use Comparer<T> instead of IComparer<T> 


Resources

Using IComparable in C#

In case you want to compare 2 objects for sorting purpose you can use this interface. IComparable defines a type specific comparison method. Value types and classes can implement this interface. 

Lets say you have a list of people and you want to sort it by age. You'll use Sort method comes with the list. Usually the Sort() method will use default comparer for the type specified by the generic argument, Here default comparer for Person. 


But it'll throw an error when trying to sort because Person does not have a comparer and it does not know how to sort them. Therefore you need to implement IComparable and its CompareTo method like below.


Now the sorting will happen successfully. Note that implementation of CompareTo(Object) must return a Int32. All numeric types such as Int32 and Double implements IComparable.


Resources

SQL Overview

Table, Fields and Rows

Table is a collection of data. It consists of fields. Field is a column in a table that contains data of the table. Record or row is individual entries containing data of a table. 

SQL Constraints

Constraints are rules enforced on columns or tables. The idea is to increase accuracy and reliability of data. 

  • Not null
  • Default
  • Unique
  • Primary Key
  • Foreign Key
  • Check
  • Index

Different types of SQL

SQL : Query language operates on sets
PL/SQL : proprietary procedural language used by Oracle
PL/pgSQL : proprietary procedural language used on PostgreSQL
TSQL : propritetary procedural language used by Microsoft in SQL Server

SQL Server vs. Oracle

In comparison SQL is much easier to use than Oracle mainly because oracle has lot more configurations comparatively. Also SQL Server integrates much easily with .NET Framework.

References

Unit Testing

What is Unit Testing?
Testing smallest testable parts of an application. Unit testing only consider about the unit under test. Later you can do integration testing to verify other parts of the application. Unit testing can be very time consuming thing and you need to understand not all flows might not get covered here. Unit testing can be done manually as well as can be automated. 

Using unit testing you can make sure the work you have done has not broken by running the tests. (Definition of Unit Testing).

Benefits of unit testing
  • Reduces bugs in production code (by identifying before)
  • Automated tests can be run frequently
  • Form of documentation
  • Inspire confidence (You know your code is working)


Disadvantages/ When not to use

Unit Testing Terminology
Mocks : Stand in object used to check results of a test
Fakes : Acts like the real object
Stubs : Provides valid response
Dummies : Contains no implementation 


Unit testing in JavaScript


Unit testing KnockoutJS applications


Some Unit Testing tools
For .NET. Not free. 

Resources

Factory Pattern

Factory pattern is a creational design pattern. It allows creation of objects without exposing internal logic to client. It allows subclasses to decide which class to instantiate. 

Let's look at how we can implement factory pattern.


This is one of the simplest ways to implement factory pattern (noob implementation). Problem is once we add a new employee type we should modify the factory method which violates open/closed principle. We can subclass factory class but factory classes are often used as singleton.

Here's a practical example on database connection.

Using Reflection

We can use reflection to avoid modification to factory class

But reflection comes with the cost of performance. You can use class registration mechanism without using reflection even.

Factory pattern In JavaScript

Factory in JavaScript : method1 (addyosmani) and method2 (carldanley)

When to use Factory

Before implementing factory method in your code think if it is really necessary to implement one because it might add unnecessary complexity to your code.

Windows Identity Foundation

http://dotnetcodr.com/tag/claims/page/2/

How to create a Claim
      Claim claim1 = new Claim("Name", "User1");

You can also use enumeration ClaimTypes offered by WIF to give name for the claim (This is not actually a enum. It is a static class with const fields).

     Claim claim2 = new Claim(ClaimTypes.Country, "Sri Lanka");

You can create IIdentity object using set of claims easily.

            Claim claim1 = new Claim(ClaimTypes.Name, "User");
            Claim claim2 = new Claim(ClaimTypes.Country, "Sri Lanka");
            IList<Claim> claims = new List<Claim> { claim1, claim2 };

            ClaimsIdentity claimsIdentity = new ClaimsIdentity(claims);

If you look at the source of ClaimsIdentity class, you can see that DefaultIssuer of this class is "LOCAL AUTHORITY". You need to give authentication type to make ClaimsIdentity authenticated (see here). 

After creating ClaimsIdentity you can create ClaimsPrincipal which wraps Identity object. 

     ClaimsPrincipal principal = new ClaimsPrincipal(claimsIdentity);

Since ClaimsPrincipal implements IPrincipal, you can set Thread.CurrentPrincipal or HttpContext.User to principal object (check this, HttpContext User vs Thread CurrentPrincipal and Thread CurrentPrincipal Identity vs HttpContext User Identity). 

Claims Transformation


Membership.ValidateUser 

validates against Membership provider defines inside web.config


Also you can go through this wondeful 7 hour workshop about WIF.



Resources

Relationships in Entity Framework

In Entity Framework, an entity can be related to other entities through an association (relationship). The relationship may be governed by a referential constraint, which describes which end in the relationship is a principal role and which is a dependent role. Navigation properties provide a way to navigate an association between two entity types.

Every object can have a navigation property for every relationship in which it participates. Navigation properties allow you to navigate and manage relationships in both directions, returning either a reference object (if the multiplicity is either one or zero-or-one) or a collection (if the multiplicity is many). You may also choose to have one-way navigation, in which case you define the navigation property on only one of the types that participates in the relationship and not on both.

when working with 1-to-1 or 1-to-0..1 relationships, there is no separate foreign key column, the primary key property acts as the foreign key and is always included in the model. When foreign key columns are not included in the model, the association information is managed as an independent object. Relationships are tracked through object references instead of foreign key properties. This type of association is called an independent association.

Difference between Navigation property and an association
Association acts as a foreign key while, navigation property allows you to navigate between entities.


Overview of .NET Framework


Above diagram shows a high level overview of .NET framework

Managed code vs Un-managed code
Managed code : code that is executed directly by the CLR. 

Un-managed code : Code that is directly executed by the operating system.

Covariance and contra-variance
http://www.indiabix.com/technical/dotnet/dot-net-framework/5 
https://www.google.lk/search?q=covariance%20and%20contravariance


Resources
http://msdn.microsoft.com/en-us/library/vstudio/zw4w595w%28v=vs.110%29.aspx


Image credit : codeguru

SignalR Performance and Scalability



ASP.NET Core SignalR is a library by Microsoft (now opensource) which allows server to communicate with client side web apps asynchronously. Performance and scalabality is a major consideration developers needs to consider when developing applications.

Performance tips

Reduce SignalR message size
You can reduce this by reducing size of your serialized objects. In Server, if you're sending an object that contains properties that don't need to be transmitted to client, use JsonIgnore attribute to make them not pass through. 

You can also reduce property name sizes using JsonProperty attribute. To make things readable in client side you can remap them to a meaningful name. 

Since messages are stored in the message bus in server memory, reducing the size of messages can also address server memory issues.

SignalR 
DefaultMessageBufferSize : By default SignalR keeps 1000 messages in memory per hub per connection. If large messages are transferred, this can create memory issues. This setting can be set in Application_Start event. Reducing this make server utilize memory in a better way (If using large messages)

IIS Settings
Max concurrent requests per application: Increasing the number of concurrent IIS requests will increase server resources available for serving requests.

ApplicationPool QueueLength: This is the maximum number of requests that Http.sys queues for the application pool. When the queue is full, new requests receive a 503 “Service Unavailable” response. The default value is 1000.

Shortening the queue length for the worker process in the application pool hosting your application will conserve memory resources.

Troubleshooting tips

You can use SignalR performance counter to measure no. of events since last application pool or server restart.

To measure performance, SignalR team uses Crank.

Scalability
You can scale up (larger server) or scale out (multiple servers). When scaling out since we have multiple servers one server might not be up to date with messages recieved by the other server. Therefore you need to use a backplane.

http://www.asp.net/signalr/overview/getting-started/real-time-web-applications-with-signalr This article also includes how to create BackPlane using SQL Server

Resources

Command Pattern

In command pattern an object is used to represent and encapsulate (hide information) all the information needed to call a method at a later time. 



We are not directly interact with the event receiver. Instead we use Command Objects to interact



Receiver : Has operations to be executed
Invoker : Calls command to carryout operations. Command object is passed to an invoker object which invokes the command. 
Command : Declares an interface for executing operations. It has Receiver object.
ConcreteCommand : Invoke operations defined on Receiver (links Receiver and action)
Client : Creates ConcreteCommand and sets its Receiver. Holds both invoker objects and command objects. It passes command objects to the invoker  

Example for C#

Usage
  • GUI buttons
  • Macro recording
  • Multi level undo
  • Progressbars
  • Wizards
wikipedia
http://stackoverflow.com/questions/tagged/command-pattern

Common Stored Procedures in SQL Server

sp_executesql (link)
Executes a T-SQL statement or batch. This can contain embedded parameters
Difference between exec and sp_executesql 

sp_getapplock (link)
Locking tables or stored procedure at the same time

MSSQL Tips - Prevent Multiple users from running same SQL Server Stored Procedure at the same time. Also look at Transactions. 

There are many other database engine stored procedures you can use

NoSQL databases


NoSQL provides an alternative way for storage and retrieval of data for relational databases. 

NoSQL has,

  • a massive write performance compared to relational DBs
  • faster Key-value access
  • Due to its lack of structure, flexible schema and data types
  • Can use right data model for right problem with different data models and many.

 Usually NoSQL databases can be categorized into, 

  • Document databases
  • Graph stores
  • Key-value stores
  • Wide-column stores

The intention to use NoSQL databases is things like horizontal scalability, simplicity of design and fine control over availability. 


Practical use cases for NoSQL databases

  • Syncing online-offline data
  • Fast response time under loads
  • Avoids heavy joins
  • Dynamic table creation
  • Hierarchical data like threaded discussions and parts explosion
  • Voting systems
  • Archiving and many

Guide to selecting the No SQL database



Concepts you need to know in NoSQL

MapReduce (link)
Programming model and an associated implementation for processing and generating large data sets. 

NoSQL databases and technologies


Apache CouchDB (link)

Uses JSON for documents, JavaScript for MapReduce indexes and regular HTTP for its API. 

Couchbase (link)

MongoDB (link)


Amazon DynamoDB - NoSQL in AWS (link)

DynamoDB is a key-value and document database in AWS. 

Cosmos DB - Implementing No SQL in Azure (link)


Built with global distribution and horizontal scale in focus. Key capabilities include,
  • Global distribution: Distribute data into any number of Azure regions ensuring lowest possible latency
  • Supports multiple data models with APIs for,
    • SQL
    • MongoDB
    • Cassandra 
    • Table etc.
  • Elastically and indepedently scale throughput and storage on demand and worldwide
  • High responsive and mission-critical support
    • Guarantees end-to-end low latency at 99th percentile to its customers

NoSQL resources

http://nosql-database.org/

Updated : June-2019

Data Compression

Data compression is the process of reducing size of a data file. Compression can be either lossy or lossless. In lossless the bits are reduced by identifying and eliminating statistical redundancy. Lossy compression removes unnecessary information. 

HTTP compression (wiki)
HTTP data is compressed before it is sent from the server: compliant browsers will announce what methods are supported to the server before downloading the correct format. The most common compression schemes include gzip and Deflate

GZip vs Deflate
GZip is simply deflate with checksum and header/footer. Deflate is faster. SO - Why use deflate instead of gzip for text files served by Apache?

Resources
Data compression - wikipedia
http://www.fastly.com/blog/best-practices-for-using-the-vary-header/



How MVC, MVP and MVVM patterns are related


MVC

MVC pattern stands for Model, View and Controller. This pattern is used to separate concerns of the application in to different layers

MVP

MVP pattern stands for Model, View and Presenter. It's a derivative of MVC. It is commonly used for user interfaces. 

MVVM

MVVM pattern stands for Model, View and ViewModel. MVVM is also based on MVC and is mainly targets UI development frameworks which supports event-driven programming.

Resources



ASP.NET Performance

Profiling

You can profile server side using Glimpse. Check the tutorial on ASP.NET website. Other than Glimpse  there are profilers like MiniProfiler. See Hanselman article about Glimpse.

Using Asynchronous Methods

.NET framework maintains a pool of threads that are used to service ASP.NET requests. When a request arrives, a thread from the pool is dispatched to serve the request. If the request is processed synchronously the thread cannot server another request.

Thread pool has a limited number of threads. The default maximum for .NET 4.5 is 5000. (See SO and SO) If a large application runs at high concurrency, all available threads might be busy. This is known as thread starvation. When this condition reaches, web server queues the requests and if the queue is full, server rejects requests with HTTP 503 status (Server Too Busy).

If you make asynchronous calls it'll increase the responsiveness. An asynchronous call takes same amount of  time as synchronous request however thread is not blocked from serving other requests. This prevents request queuing 

There are many benefits of using asynchronous methods to serve data.


Bundling and Minfication

Concepts in Windows Identity Foundation

WIF involves lot of related concepts.


IIdentity
Usually represents the user. This interface is implemented by GenericIdentity and WindowsIdentity classes.
Identity Objects encapsulates information about the user or the entity. It basically contains a name and an authentication type. The name can be user's name or Windows account. .All Identity classes implement IIdentity interface. 

IPrincipal
IPrincipal represents the security context of the user. It has Identity property which returns identity of current principal. 

Here's a class diagram of WIF, extracted from MSDN

Comparison between Principal vs Identity Objects
  • Managed code can discover identity or role of principal through Principal object. Principal object contains reference to an Identity object. 
  • You can compare Identity and Principal objects to similar concept like User and Group Accounts.
  • In .NET, Identity objects represents users and roles represent membership and security context
  • Principal object encapsulates both identity object and a role.
ClaimsIdentity (inherits IIdentity)
Represents a claim based identity. It is a concrete implementation of claim based identity (Identity described by a collection of claims) The Claims contained in a ClaimsIdentity describes a entity that identity represents and can be used for authorization and authentication decisions.

The ClaimsPrincipal has a Claims property as well. In majority of cases you should access users Claims through ClaimPrincipal.Claims collection.

ClaimsPrincipal (inherits IPrincipal)
An IPrincipal implementation that supports multiple claim based identities. It has Claims property (IEnumerable) which has claims from all claim identities associated with the claims principle.



If you're implementing your own identity or principal objects you should implement ClaimsPrincipal or ClaimsIdentity. 

Accessing ClaimsPrincipal from HttpContext
Cast HttpContext.Current.User to a ClaimsPrincipal and access Claims

Claim
Claim is a piece of identity information. Usually you don't look up for claims. User delivers claims to your application. Claims are made by an issuer. Issuer can be your company therefore you trust the claims. Claim class has an issuer property which you can use to see who issued them. 

Security Token
User delivers set of claims to your application with a request. Later this might be cached in a cookie to read for consequent requests. Security token is a serialized set of tokens that is signed by an issuing authority. The signature is important. 

Issuing authority
This can be domain controllers that issue Kerberos tickets, authorities that issue X.509 certificates. Basically issuing authority is responsible to authenticate users for you.

Security Token Service 
Service that builds, signs and issues security tokens (according to WS-Trust and WS-Federation protocols). There are pre-built token services such as ADFS, cloud STS such as Windows Azure Access Control service etc.

You can built your own STS to issue custom tokens using WIF.

Relying Party
When you are building an application that uses claims, you're building a relying party (RP) application. (claims aware application, claim based application etc.). RP uses claims to do identity related activities. WIF fully supports this

Resources
https://msdn.microsoft.com/en-us/library/hh873308(v=vs.110).aspx
Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook