Saturday, March 16, 2013

All operator in linq

Few days back I came across “All” operator in Linq. I thought it will be good idea to write a blog post about it and share it with community.

All operator in linq:

It’s almost similar to select it returns all the element in the input sequence with matching condition in given predicate. Following is syntax for All.
public static bool All<TSource>( 
this IEnumerable<TSource> source, 
Func<TSource, bool> predicate)

It check the condition whether all the elements on collection matches given criteria and based on that it will return bool value.  It can be useful in scenario where we need to do some kind of validation whether all the elements of collection matches with certain condition or not.

So Let’s take a simple example. Following is a code for that.
using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] intArray = {1,2,3,4,5};
            bool result = intArray.All(i => i > 2);
            Console.WriteLine(result);
            result = intArray.All(i => i < 6);
            Console.WriteLine(result);
        }
    }
}

Share:
Thursday, March 14, 2013

CRUD operations with PetaPoco and ASP.NET MVC

In this post we are going to see how we can do CRUD operations with ASP.NET MVC and PetaPoco Micro ORM.

What is PetaPoco?


Petapoco is a tiny ORM developed by topten software. As per them it’s a tiny, fast, single-file micro-ORM for .NET and Mono.
  • Like Massive it's a single file that you easily add to any project
  • Unlike Massive it works with strongly typed POCO's
  • Like Massive, it now also supports dynamic Expandos too - read more
  • Like ActiveRecord, it supports a close relationship between object and database table
  • Like SubSonic, it supports generation of poco classes with T4 templates
  • Like Dapper, it's fast because it uses dynamic method generation (MSIL) to assign column values to properties

Features of PetaPoco:


As per topten software it contains following features.
  • Tiny, no dependencies... a single C# file you can easily add to any project.
  • Works with strictly undecorated POCOs, or attributed almost-POCOs.
  • Helper methods for Insert/Delete/Update/Save and IsNew
  • Paged requests automatically work out total record count and fetch a specific page.
  • Easy transaction support.
  • Better parameter replacement support, including grabbing named parameters from object properties.
  • Great performance by eliminating Linq and fast property assignment with DynamicMethod generation.
  • Includes T4 templates to automatically generate POCO classes for you.
  • The query language is SQL... no weird fluent or Linq syntaxes (yes, matter of opinion)
  • Includes a low friction SQL builder class that makes writing inline SQL much easier.
  • Hooks for logging exceptions, installing value converters and mapping columns to properties without attributes.
  • Works with SQL Server, SQL Server CE, MySQL, PostgreSQL and Oracle.
  • Works under .NET 3.5 or Mono 2.6 and later.
  • Experimental support for dynamic under .NET 4.0 and Mono 2.8
  • NUnit unit tests.
  • OpenSource (Apache License)
  • All of this in about 1,500 lines of code
Share:
Wednesday, March 13, 2013

Default focus and default button in asp.net

Sometimes ago a friend ask me how to put default focus and default button in ASP.NET. He was not knowing that there is property for that in form tag in asp.net. I thought it will be good idea to write a blog post about that.So those who are not aware of this feature can get an idea for that.

In some cases we need to have default button and default focus. For example take a example of a shopping cart registration form. Where you need to have default focus into the first textbox if you contain list of textboxes over there. Same way you can have more then one button on asp.net forms for example save and cancel so once user press enter which button should be pressed. On above both scenario this Default button and default features can come very handy.

From asp.net 2.0, The form tag has this two properties DefaultButton and DefaultFoucs where you can set the Id of control that you need as default button and same way you need to put Id of a control for default focus.
Share:
Sunday, March 10, 2013

How to consume ASP.NET WebAPI from jQuery

Before some months Microsoft has launched WebAPI framework for building and consuming HTTP services. This WebAPI can be called from anything like from mobile sites to JavaScript anything. I thought it will be a good idea to call a WebAPI from jQuery.

In this post I am going to explain how to consume ASP.NET WebAPI from jQuery.

Creating a basic WebAPI:

To create a web API you need to install ASP.NET MVC 4.0 and then you create a new project for MVC .

How to Create Webp API in Visual studio 2012

Now once you click Ok. It will ask for type of project. You want to create you need to select Web API there.
Share:
Friday, March 8, 2013

Dependency graph in visual studio 2012- Understand your code better.

Today I came across a new feature of visual studio 2012 called Dependency Graph in visual studio 2012. I thought it will be good Idea to write a blog post about this.

I have written list of visual studio 2012 features post. You can find this at my Visual Studio 2012 page.

What is Dependency Graph?

Dependency graph help you visualize how your code is written and organized and provide view on code dependencies. This is very useful when you are working with a code that you have not written. With the dependency graph you can easily get an idea of code written by others in a application.

How to create Dependency Graph:

You can easily create dependency graph via architecture menu –>Generate Dependency graph. You will also presented to two options like below.
  1. For Solution
  2. For Include file.
Share:
Thursday, March 7, 2013

SelectMethod in ASP.NET 4.5 Model binding

New version of ASP.NET provides lots of way of binding data to the data controls like Grid View, Repeater etc. You can now bind this controls with strong type. I have earlier written a blog post about How we can use Model binding with ASP.NET 4.5 and Visual Studio 2012.

SelectMethod in ASP.NET 4.5:


In asp.net 4.5, It is also possible to bind controls with SelectMethod attribute. This method specify the data that you want to bind with data controls like grid view and repeater controls.

So what we are waiting for?? Let’s take an simple example. I have create a basic model class “Employee” like below.
public class Employee
{
    public int EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
This class contains three properties EmployeeId,FirstName and LastName. Now I have created one method to get a generic list of employee model like following.
Share:
Wednesday, March 6, 2013

Telerik JustCode review

Visual Studio is a great IDE and  allow developers to design and code anything from windows application to web application. But it does not have a much code refactoring features so You need to have third party add in to make this happen. Telerik JustCode is one of them.

Recently I was contacted by Telerik team to review Telerik JustCode. So I installed Telerik Just code in my machine and I have used it for few days and Here is the my first impressions.

Things that I like about Telerik JustCode:

  1. It does not have impact on the Visual Studio load time.
  2. It does not change any theme or UI for the Visual Studio.
  3. Performance is much better then it’s competitors. It does not slow down visual studio while you were using it.
  4. Its not overwriting all things in Visual Studio like visual studio shortcuts.
  5. Another things its competitively priced then its competitors.
  6. Cloud Sync of your settings so you don’t have to worry about loosing your settings.
  7. Almost got all the features of code refactoring and code creation which are available with tools.
  8. Does not have annoying popups for suggestions or code refactoring.
Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews