Tuesday, July 19, 2011

Windows Azure Table Storage in detail

Windows Azure table storage provides two kind of storage 1) SQL Azure and other one is 2) Table,queue,blob and other storage like drives etc. In this blog post I am going to explain Table storage in details.

What is Windows Azure Table Storage:


Windows Azure table storage is a service provided by Azure platform to store data in form relational tables in windows azure storage instead of normal RDBMS System. It is a structured storage provided by Azure platform. The table storage which you create with windows azure is Unique. An account of windows azure can have any number of table storage.

Table Storage in details:


There are three important aspects when you deal with windows azure table services. Table,Entities and Properties. Here table is similar to storing data in spread sheet or storing in table with relational databases. Entity represents particular row in the table and property represents a particular column or cell in row. There are three system properties of each entity.
  1. Partition Key
  2. Row Key
  3. Time Stamp

Partition Key:


The partition key is a unique key value that is used to group, or partition, related data on same storage node. As we all know that internally data will stored in hard disks in table storage so whenever your data grows then at that time your data may be partitioned into multiple hard drives. At that time this partition key will keep all the data on the same storage nodes. So partition key is used to organize data in storage.

Row Key:


Row key uniquely identifies a specific entities on a partition. Different partition keys may share the same row key, but the same partition may not have two entities with the same row key.The Row Key property is optional and may also be an empty string if all that is needed for uniqueness is the Partition Key value
The PartitionKey and RowKey properties both have String values (up to 1KB in size) and together they form a unique primary key for an entity in a table.

TimeStamp:


Time stamp property has a DateTime value maintained by the Windows Azure Table System to facilitate optimistic concurrency.

Column Types in Table Storage:


Table storage in windows azure support following types of columns.
  1. byte[]
  2. bool
  3. DateTime
  4. double
  5. Guid
  6. Int32 or Int
  7. Int64 or long
  8. String.
In next post I will create application for table storage. That’s it. Hope you like it. Stay tuned for more.. Till then happy programming..

Shout it

kick it on DotNetKicks.com
Share:
Sunday, July 17, 2011

New Improved IntelliSense with Visual Studio 2010

I have posted lot many things about Visual Studio 2010 features because its a great IDE(Integrated Development Environment).Today I am going to write about IntelliSense improvement in Visual Studio 2010. Today when I was working with Visual Studio 2008 for a old project(Nowadays I am working with Visual Studio 2010), I have found new improve IntelliSense feature in Visual Studio 2010.


Filtering:


Visual Studio 2010 IntelliSense is filtered better than Visual Studio 2008. I have created a sample application and I am writing Console.ReadLine that you can see in following image its filtering with read only.

Intellisense

In earlier version it was not possible.Hope you like it. Stay tuned for more.. Happy Programming..

Shout it

kick it on DotNetKicks.com
Share:
Saturday, July 16, 2011

Introduction to Windows Azure

Recently I have got chance to work with SQL Azure and I loved it. So that’s why Here I am going to start series of post on Windows Azure and SQL Server Azure. In first post of this series I am going to introduce you windows azure platform. I know there are many peoples that may find this post as very beginner level but We all have started from beginner level at some point of time. So It’s better to start at beginner level and then go through step by step overview.

What is Windows Azure:


Well simplest answer in this simple language to above question is “ It’s a operating system which hosts number of services on cloud”. Microsoft has developed this platform to leverage services into the cloud. In azure world you can have application and databases running on cloud. You don’t have to worry about the infrastructure of servers and all other stuff. They will be automatically done by windows azure plate form itself. All you have need to do buy a windows azure services and then developed application with the SDK provided by Microsoft and deploy it. Your application will be ready to serve. You can add multiple instances of server as per your application requirement.

Developing windows azure application is slightly different from normal traditional applications. But developer can still use visual studio 2010 for that. It can also host PHP,Java and other applications also. It does not required to have proprietary software to write application. In fact you can use windows azure with Visual Studio 2010 express edition also.

Roles:

In windows azure everything is based on Roles. There are two kind of roles available for windows azure.
  1. Web Role
  2. Worker Role
Web Role:
You can think web role as web server where we can host our web application or web site. They provide public computers to connect your application like a simple website url i.e. myapplication.cloudapp.net etc. You can run the application with standard HTTP and HTTPS ports also. Even you can have multiple web roles for your single application as per your requirement.
Worker Role:
You can defined Worker Role as some kind of services that works behind the web application. i.e. A window service to create thumbnail of images to display it in web application or a caching service etc. Worker roles are also available outside the world they can talk to any application.

Storage Resources available in Windows Azure:


Azure provide two kind of storage facility In built storage in Azure where we can store data in blobs,queues,tables, drives which are used for following purposes.
  1. Blob- Used to store large amount of data.
  2. Queues-Background work processing
  3. Table- Storage for fast retrieval
  4. Drives-NTFS Formatted page blobs
Windows Azure also provides storage facility with SQL Azure.SQL Azure is a database in cloud where you can almost have all functionalities of SQL Server in cloud. I will cover about above topic in details in forthcoming posts.

All types of roles comes in four verities: Small,Medium, Large and Extra large. All roles are virtual machines. Each represents an increase in pricing and has a different set of specifications that govern how much RAM, local storage space and CPU cores are available to the role as described below:


Size CPU Cores Memory Disk Space for Local Storage Resources
Small 1 1.7 GB 250 GB
Medium 2 3.5 GB 500 GB
Large 4 7 GB 1000 GB
Extra-large 8 14 GB 2000 GB


That’s it. Hope you liked this post. In forthcoming post I am going to explain you can use windows azure in your application with example. Till that stay tuned for more.. Happy programming..


Shout it

kick it on DotNetKicks.com
Share:
Friday, July 15, 2011

Some important attributes in EFCodeFirst

In today’s post I am going to explain some of the important attributes in EFCodeFirst. Whey you do scaffolding with ASP.NET MVC this all attributes are where much important. So Let’s explore all the attributes.

Key Attribute:

When you put this attribute in the class it tell that this property is part of the primary key and If you use code first when its create table based on this entity then it will be a part of primary key of table. Here is the sample example of that.
[Key]

public int ArticleId { get; set; }
Here article ID will act as primary key.

Foreign Key Attribute:

This attribute is very useful when you deal with interrelated tables You can specify the column in entity which is foreign key for that Like following.
[ForeignKey("CategoryId")]

public ICollection<Category> Categories { get; set; }
In above example CategoryId is a foreign key of category table to article table.

ScaffoldColumn Attribute:

When you Scaffold your application with ASP.NET MVC scaffold feature or dynamic data with asp.net webforms this attribute will tell that this column need to have field for that. You can write that column like following.
[ScaffoldColumn(false)]

public int ArticleId { get; set; }
Here it will tell article id will not have UI in scaffoling.

StringLength Attribute:

This attribute is used to specify the maximum length of a string. This attribute is very usefull in validation and other stuff. This attribute only applied to the Stringlength property like following.
[StringLength(512)]

public string Title { get; set; }
Here in the above code it tell that title could not have more then 512 character string.

There lots of other attributes also. I will blog about it in future posts. Hope you liked this. Stay tuned for more..Happy Programming.

Shout it

kick it on DotNetKicks.com
Share:
Tuesday, July 12, 2011

Parallel for loop in C# 4.0

Now days we are getting our computer equipped with more and more power even now we are getting quad core processor is also at lower price. Since we have multicore processors are now so we can take advantages of multicore processor with parallel execution in C# 4.0. There are some time consuming task for the computer for example a long for loop or similar kind of things. This kind of task can be done parallel with parallel class in C# 4.0.

Now in C# 4.0 you have the Parallel static class with the help of this you can perform task parallel very easily. In this post I am going to explain Parallel for loop. Let’s take a very simple example. First lets create a example with simple for loop. Following is code for that.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Parallel
{
class Program
{
    static void Main(string[] args)
    {
        for (int i = 0; i <=100000; i++)
        {
            Console.WriteLine(i);
        }
       
    }
}
}

In the above its a normal loop which increments I and then print the that number into the console screen and Here is the output.

Ouput1

Here in the output you can see its sequentially process one by one now lets change this code to parallel for loop like following.

using System;

namespace Parallel
{
class Program
{
    static void Main(string[] args)
    {
        System.Threading.Tasks.Parallel.For(0, 100000, i =>
        {
            Console.WriteLine(i);
        }
        );
        Console.ReadLine();
    }
}
}

Here in the above code you can see that I have used parallel for which also prints the Incremented integer in console application. Now let’s run that code and see the output as following.

Output2

If you see the output very carefully then you can see that its not processing sequentially one by one but its processing it parallel and its will be much faster if you have multicore processor in your computer. But beware if you are going to run in simple processor computer its going to take more time then normal computer. That’s it. Hope you liked it.. Stay tuned for more.. Till that happy programming..



Share:
Wednesday, July 6, 2011

CRUD Operation with ASP.NET MVC and EFCodeFirst Part-2

In the previous post I have already explained How we can list data from database easily with the help of EFCodeFirst . In this post I am going to explain How we can complete Create,Edit,Delete and Details operations within 10 minutes. So let’s first Start with Create a new Customer.I am going use same example of customer from previous post. So we have customer controller there so Let’s first create a new view for Create via Selecting View in Create() method of Customer Controller and Clicking on Add View and dialog box will open for add view like following.

CreateView

As you can see in the above template I have selected Customer Model Class for strongly typed view and Selected Scaffold template as Create once you click Add your view will be ready. Now its time to write for code in customer controller to Add Customer. So I have modified Create Method of customer controller which we have created like following.

[HttpPost]
public ActionResult Create(Models.Customer customer)
{
  try
  {
      using (var databaseContext = new Models.MyDataContext())
      {
          databaseContext.Customer.Add(customer);
          databaseContext.SaveChanges();
      }
    
      return RedirectToAction("Index");
  }
  catch
  {
      return View();
  }
}

That's it. We have done with Create Customer now. In the above databasecontext add method will add customer and SaveChanges method will save changed to database.
Now once we are done with Add its time to create edit and update functionality. Let’s first Add a view via Selecting view with Edit Method in clicking on Add view. Once you click a dialogbox for add View will open for that like following.

EditView

As you can see you I have selected scaffold template as Edit and I have Created Strongly typed view with Customer class. Once you click add it will create a new view for Edit. Now our Edit View is ready so let’s write code for Edit/Update in database. So first we have to modify Edit(int Id) method like following which will return specific customer with View. Following is a code for that.

public ActionResult Edit(int id)
{
  using (var databaseContext = new Models.MyDataContext())
  {
      return View(databaseContext.Customer.Find(id));
  }
}

Now let write code to update the changes to database. So for that I have modified another ActionResult Edit of customer controller like following.

[HttpPost]
public ActionResult Edit(int id, Models.Customer customer)
{
  try
  {
      using (var databaseContext = new Models.MyDataContext())
      {
          databaseContext.Entry(customer).State= System.Data.EntityState.Modified;
          databaseContext.SaveChanges();
      }
      return RedirectToAction("Index");
  }
  catch
  {
      return View();
  }
}

That's it we are done with the edit stuff.In above code the state modified will tell databasecontext that customer details is modified and savechanges will save changed to database. Now its time to create view for delete functionality. So I have clicked on Add View and Created a view for delete like following.

DeleteView

Here I have create Strongly typed view with Delete Scaffold Template. Now let’s modified both Delete Action Result in Customer Controller class. First Action result will return customer which we are going to delete and another delete action result with id will delete the customer from database and then it will return to main customer page. I have modified the code for both as following.

// GET: /Customer/Delete/5

public ActionResult Delete(int id)
{
  using (var databaseContext = new Models.MyDataContext())
  {
      return View(databaseContext.Customer.Find(id));
  }

}

//
// POST: /Customer/Delete/5

[HttpPost]
public ActionResult Delete(int id,Models.Customer customer)
{
  try
  {
      using (var databaseContext = new Models.MyDataContext())
      {
          databaseContext.Entry(databaseContext.Customer.Find(id)).State = System.Data.EntityState.Deleted;
          databaseContext.SaveChanges();
      }

      return RedirectToAction("Index");
  }
  catch
  {
      return View();
  }
}

Here I have done something different then editing stuff to demonstrate the feature of Entity framework. You can also use Id for finding the current customer and then change its state to delete and SaveChanges will save that to in database. So now we are done with delete also. It’s now time to create details part. So same as above I have create a view with scaffold template details with customer model like following.

DetailView

Once we are done with creating view . It’s time to change the code for Details Action Result like following to return current customer detail.

public ActionResult Details(int id)
{
  using (var databaseContext = new Models.MyDataContext())
  {
      return View(databaseContext.Customer.Find(id));
  }
}

So that's it. We are done with all the stuff. So with Entity Framework code first. You can create basic CRUD application within 30 minutes without writing so much code. Hope you liked it. Stay tuned for more.. Till that Happy programming.

Shout it

kick it on DotNetKicks.com
Share:
Tuesday, July 5, 2011

CRUD Operation with ASP.NET MVC and EFCodeFirst Part-1

I have been playing with EFCodeFirst now and I found it very interesting with that you could write your application fast and easily. So I have decided to write series of blog posts for CRUD Operations using ASP.NET MVC 3 and EFCodeFirstCTP5.0 . You can very easily create CRUD within some minutes of code. So let’s start first thing is to create an ASP.NET MVC 3 application like following.

ASP.NETMVCApplication

Once you click OK. It will ask for the which type of View your are going to use. I have used Razor and HTML5 Semantic like following.

RazorView

Now once we have create a application Now its time to add EFCodeFirst reference via NuGet. So Go to the Library Package Manager –> Manage Nuget Package Manager. It will open up a dialog like following. Search EFCodeFirst and then it will fine EFCodeFirst package like following.

EFCodeFirst

Once you click Install it will add reference to your ASP.NET MVC Application like following.

EntityFrameworkRerence

So now all the things are set Its time to code now. So first I have Create my Table which is very simple table called Customer like I have used in previous post. For your reference I am putting the create table script below.

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Customer](
   [CustomerId] [int] IDENTITY(1,1) NOT NULL,
   [FirstName] [nvarchar](50) NULL,
   [LastName] [nvarchar](50) NULL,
   [Address] [nvarchar](256) NULL,
   [City] [nvarchar](50) NULL,
CONSTRAINT [PK_Customer] PRIMARY KEY CLUSTERED
(
   [CustomerId] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY

Now let's create our entity class called customer like following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;

namespace CodeSimplified.Models
{
   public class Customer
   {
       public int CustomerId { get; set; }
       public string FirstName { get; set; }
       public string LastName { get; set; }
       public string Address { get; set; }
       public string City { get; set; }
   }
}

Now Its time to create a our datacontext class So I have created MyDataContext class inherited from the DBContextClass . In that I have defined my customer dbset and Also I override the default plural behaviour in ModelCreating Table. DBSet will tell datacontext which table we need to refer. So following is code for that.

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Data.Entity.ModelConfiguration.Conventions.Edm.Db;

namespace CodeSimplified.Models
{
   public class MyDataContext:DbContext
   {
       public DbSet<Customer> Customer { get; set; }

       protected override void OnModelCreating(System.Data.Entity.ModelConfiguration.ModelBuilder modelBuilder)
       {
           modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
       }
   }
}

Now our database access stuff is ready now It’s time to create controller. So I have clicked controller folder and click on add new controller so add controller dialog box will appear like following.

AddController

It will create a controller class like following.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CodeSimplified.Controllers
{
   public class CustomerController : Controller
   {
       //
       // GET: /Customer/

       public ActionResult Index()
       {
           return View();
       }

       //
       // GET: /Customer/Details/5

       public ActionResult Details(int id)
       {
           return View();
       }

       //
       // GET: /Customer/Create

       public ActionResult Create()
       {
           return View();
       }

       //
       // POST: /Customer/Create

       [HttpPost]
       public ActionResult Create(FormCollection collection)
       {
           try
           {
               // TODO: Add insert logic here

               return RedirectToAction("Index");
           }
           catch
           {
               return View();
           }
       }
      
       //
       // GET: /Customer/Edit/5

       public ActionResult Edit(int id)
       {
           return View();
       }

       //
       // POST: /Customer/Edit/5

       [HttpPost]
       public ActionResult Edit(int id, FormCollection collection)
       {
           try
           {
               // TODO: Add update logic here

               return RedirectToAction("Index");
           }
           catch
           {
               return View();
           }
       }

       //
       // GET: /Customer/Delete/5

       public ActionResult Delete(int id)
       {
           return View();
       }

       //
       // POST: /Customer/Delete/5

       [HttpPost]
       public ActionResult Delete(int id, FormCollection collection)
       {
           try
           {
               // TODO: Add delete logic here

               return RedirectToAction("Index");
           }
           catch
           {
               return View();
           }
       }
   }
}

Now once our controller is ready now its time to write some code for Index Action Result which we are going to have a list of customer. So I have changed Index Action Result like following to return customer view.

public ActionResult Index()
{
   using (var databaseContext=new Models.MyDataContext())
   {
       return View(databaseContext.Customer.ToList());
   }
}

Now let create a view from the controller. So Selected a view in right click Add View from Index Action Result. Here I have selected strongly typed view with Customer Model and selected list Template like following.

AddView

That’s it we are done with listing of the customer. Now its time to run our application and following is the output as expected.

Output

Hoped you like this…Stay tuned for more.. Till that happy programming.

Shout it

kick it on DotNetKicks.com
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