Get started with ASP.NET MVC and PetaPoco

Micro ORM are having their pro and cons and right now its a buzz in the Microsoft.NET developers buzz. I personally like a Micro ORM Concept and I already posted some blog posts with asp.net mvc and dapper Micro ORM. Today I am going to explain how we can use PetaPoco in asp.net mvc application.  First lets get little bit introduction about PetaPoco.

PetaPoco is developed by Top Ten software and It’s Micro ORM inspired by Masive and Dapper. Following is link where you can get more information about that.

http://www.toptensoftware.com/petapoco/.

To install PetaPoco in our application first we need to add a library reference as Its already available there. so Let’s add library reference like following.

AddlibraryReference 

Once click on add library reference a dialog box appears to find the the library package like following. I have written PetaPoco in search box and it will be appear in list like following.

PetaPocoNugetPackage

Select first one then click Install and It will install the PetaPoco in your application. Once installation done you will have one t4 teamplte and one PetaPoco.cs file in your model section of ASP.NET application like following.

SolutionExplorer

Now we are ready use the PetaPoco in our application. Now let’s use same DB which we have use for dapper demonstration let’s take same customer table like following.Following is a script to create table.

/****** Object:  Table [dbo].[Customer]    Script Date: 06/17/2011 02:27:34 ******/
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 I have inserted some sample data in the table and now its ready to use. Now I have added a Model Entity class which contains same properties as Table columns. Just like following.
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 we are ready with our class I have create a new class called CustomerDB to fetch customer from the database. In that class I have created method called GetCustomers which will fetch all the customer from database. Here I have created a database object of PetaPoco and then I have using Its query method to query database. Following is a code for that.

using System.Collections.Generic;
namespace CodeSimplified.Models
{
    public class CustomerDB
    {
        public IEnumerable<Customer> GetCustomers()
        {
            var databaseContext = new PetaPoco.Database("MyConnectionString");
            return databaseContext.Query<Customer>("Select * from Customer");
        }
    }
}

Now as this is for demo purpose only lets create a method in Home Controller class to get customers like following.

using System.Web.Mvc;
namespace CodeSimplified.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";
            return View();
        }
        public ActionResult About()
        {
            return View();
        }
        public ActionResult Customer()
        {
            var customerDb = new Models.CustomerDB();
            return View(customerDb.GetCustomers());
        }
    }
}

Now once done we with Customer Method in Home Controller let’s create view for that. Here I have created a strongly typed view like following for customers.

View

Now we are done with our view also So let’s run the project and let’s see output in browser. You can see output in browser as expected like following.

Output

So it’s very easy to use PetaPoco with ASP.NET MVC. Hope you liked it. Stay tuned for more..

Shout it
Published Friday, June 17, 2011 4:17 PM by Jalpesh P. Vadgama

Comments

# re: Get started with ASP.NET MVC and PetaPoco

Friday, June 17, 2011 10:40 AM by Guy Harwood

How is it any different from Dapper? and why should i use it instead of dapper/massive?

# re: Get started with ASP.NET MVC and PetaPoco

Saturday, June 18, 2011 8:42 AM by Jalpesh P. Vadgama

@Guy Harwood-Both are different in way. It's your choice for which you selected based on requirement.

see the following link

-stackoverflow.com/.../which-micro-orm-to-use

# PetaPoco with stored procedures

Sunday, June 19, 2011 4:28 PM by DotNetJalps

In previous post I have written that How we can use PetaPoco with the asp.net MVC. One of my dear friend

# PetaPoco with stored procedures

Monday, June 20, 2011 4:20 PM by PetaPoco with stored procedures

Pingback from  PetaPoco with stored procedures

# PetaPoco with parameterised stored procedure and Asp.Net MVC

Pingback from  PetaPoco with parameterised stored procedure and Asp.Net MVC

# re: Get started with ASP.NET MVC and PetaPoco

Thursday, July 28, 2011 7:15 AM by moncler jackets

How we can use PetaPoco with the asp.net MVC. One of my dear friend

Leave a Comment

(required) 
(required) 
(optional)
(required)