SetInterval and ClearInterval -Javascript

I had been developing a real time chat system at my work  and the chat system takes some time to load(makes an ajax call to the backend that fetches users details,chat message,chat room id ,chat user role id etc..).and also there are multiple chat rooms based on user roles. The challenge was to wait for all of data to load  and then enable the chat system.

Solution :

The solution  is to wait for x interval of time and then make a call x time to check if the data we require is loaded.

 


var intialloading=false;
waitoperation();
var someinterval =setInterval(function(){

if(intialloading)

{

//clear the interval.

ClearInterval(someinterval);

}

},2000) // call function every 2 secs

function waitoperation()

{

//some call

intialloading=true;

}

 

 

 

 

Layouts in MVC

Layouts are used when there is a need to maintain a consistent look across multiple views.(Example : Header and footer across views).They are similar to master pages in ASP.NET.

Important Methods in Layouts :

@RenderBody():

1.Renders View and all of its components in layout page.

2.There can be only one RenderBody in a layout page.

Capture

@Renderpage():

1.Renders the specified page in layout.It takes an argument that specifies the location of page.


@RenderPage("~/Views/yourpage.cshtml");

2.There can be multiple RenderPage method in layout.

@Sections:

Renders the content of page in specified region  in layout.


@section("nameofsection,false")

1.Requires name of the section.If not specified exception will be thrown.

2.IsRequired attribute in section method has to be set to false,if section is not mandatory for every view,else an exception will be thrown.

rendersection

 

 

 

 

 

 

#CodeKata-Find Position of parenthesis in a balanced Expression

Problem : Given a balanced expression,find the position of matching parenthesis.

Example :

Given an expression “( ( (  ) ) ) ” and an input which points to an open parenthesis,output the corresponding position of closing parenthesis

If the input is 1  ,the output is 6, because the closing parenthesis for the first open parenthesis closes at position 6.

Solution :

1. Start at the given position of parenthesis and  Initialize a counter with 1.

2.Increment the counter ,when ever you see an open parenthesis.

3. Decrement the counter by 1,when ever you see an closed parenthesis.

4.If the counter is zero,then output the current index which is the match for the open parenthesis .

Code  :


int ReturnClosedParanthesisPosition(string input,int openparenthesisposition)
{
var parenthesisarray = input.ToCharArray();
int counter = 1;
for (int i =openparenthesisposition; i < parenthesisarray.Length; i++)
{
if (parenthesisarray[i]=='(')
{
counter++;
}
if (parenthesisarray[i] == ')')
{
counter--;
}
if (counter == 0)
{
return i;
}
}
return -1;
}

 

#ASP.NET CORE-View Components

What is View Components ?

ViewComponents are similar to partial view,that helps you to write reusable components.

Definition According to documentation:

New to ASP.NET Core MVC, view components are similar to partial views, but they are much more powerful. View components don’t use model binding, and only depend on the data you provide when calling into it.

View Components :

ViewComponents consists of two parts

1.Class derived from ViewComponent.

2.Result  returned from ViewComponent typically a view.

Creating a View-Component :

Let’s create a simple view component. They can be created in any of  ways listed below,

1.Derived from View Component class.

2.Decorate with ViewComponent Annotation.

3.class suffixed with ViewComponent.

A sample View Component would look like below:


public class SimpleViewComponent : ViewComponent
{
public IViewComponentResult Invoke()
{
return View();
}

}

Creating a View for ViewComponent :

The runtime searches for the view in the following paths:

  • Views/<controller_name>/Components/<view_component_name>/<view_name>
  • Views/Shared/Components/<view_component_name>/<view_name>

Let’s create a sample view named Default.cshtml which says “Hello Simple View Component” in the shared folder for the viewcomponent.

Capture

Invoking ViewComponent in the view : 

ViewComponent can be be called from view using @Components synchronously/asynchronusly with viewcompanent name.


@await Component.InvokeAsync("Simple")

You can download the sample code from github below

https://github.com/rangesh-/ViewComponents-.NET-CORE

 

 

 

 

#.NET CORE Basics-project.json in MVC

The project.json file is the place where you add the dependencies with respect to your project. By default .NET Core loads only the necessary dependency/dnx, its quite different from the way the previous version of .NET(loads all dependency irrespective of usages).

Structure of project.json :

Capture

Dependencies :

By default .NET Core loads only required dependencies in the project,to add reference or dependencies to project this is the place where you declare the references . It generally contains the dependency name and version.The packages are pulled automatically as Nuget Packages.

Frameworks :

Specify the version of .NET being used,for instance .NET Core /.NET 4 etc..

Capture.JPG

PublishOptions :

This region will let you define what to include/exclude the items that are being published with IIS server.

Capture

Scripts :

This region specifies the build automation for scripts(like installing gulp,trunk etc..).All the commands defined in the scripts will be executed before the site get published in the server(prepublish).For instance “bower install” refers to install bower before publishing the package.

Capture.JPG

 

 

 

 

Getting Started with ASP.NET CORE 1

There has been significant changes in .NET after the RC of ASP.NET 5/CORE 1. In this article,i will introduce the project structure  of ASP.NET CORE Web Application.

Installation :  

1.ASP.NET Core 1 requires VS 2015 – Community and Free Edition

Getting Started :

When you create a new web application,the project structure looks like below.

Capture

The highlighted regions defines the behavior of application.I would be covering each of these in detail in the next article.

Purpose/Definition of Regions Highlighted:

global.json : Configuring the solution as a whole.Contains two section projects and sdk.

wwwroot : All the UI related Components(JS+HTML+CSS).

project.json :All the project dependencies and references to project are added here.

startup.cs : Application start up region where you resolve the dependencies and add services to project.

 

 

#Sql -Transactions Basic

Transactions : A transaction is a set of sql statements that are executed as single sql statement,If the statement executes successfully Commit the transaction else Rollbacks on failure.

Usage :


Begin tran
insert into dbo.sampletable values('',null,GETDATE(),null)
insert into dbo.sampletable values(1,'',null,GETDATE(),null) //Inserting identity value will fail the query.
IF @@ERROR<>0
ROLLBACK
ELSE
COMMIT

In the above code,i am making an insert into an identity column which will fail the query and in turn roll back the transaction and also make a note that none of the rows would be inserted since transaction failed.

 

 

#Sql Basics – joins

Quick Basics of Join.

Types of Join :

1.Inner Join-Intersection of two tables.

2.Left Outer Join-Intersection of two tables + rows in left table.

3.Right Outer  Join-Intersection of two tables +rows in right table.

4. Outer Join-Left join +Right Join ,null values will be substituted when condition is not met.

5.Cross Join – A Cartesian product(No of rows in A * No of rows in B) between two tables,there is no “on” condition.Produces all combination of rows between two tables.

Syntax for cross join :


select * from tablea cross join tableb

(or )

select * from tablea,tableb

6.Self Join : Join among the same table.