#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.