In this article I am attempting to illustrate how to use Microsoft Enterprise Library data using the MVC 2 pattern.
Model, Views Controller (MVC) separates the modelling of the domain, the presentation and the actions based on user input into three classes.
- Model: The model manages the behaviour and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).
- Controller: The controller interprets the mouse and keyboard inputs from the user, informing the model and/or the view to change as appropriate.
- View: The view manages the display of information.
You can download DAAB from here>>
Getting Started
Make a new project using Visual Studio 2010 and select "ASP.NET MVC 2 Web Application".

Image 1.
You can create a unit test project if you want to.
Note: If you are using the Standard or Express editions of Visual Studio then the Create Unit Test Project dialog box is not displayed. Instead, the new MVC application project is generated without a test project.

Image 2.

Image 3.
The Unit test project has already added the reference of the web project.

Image 4.
The folder structure of an MVC project differs from that of an ASP.NET Web site project. The MVC project contains the following folders:
- Content for content support files. This folder contains the Cascading Style Sheet (.css file) for the application.
- Controllers for the controller files. This folder contains the application's sample controllers, which are named AccountController and HomeController. The AccountController class contains login logic for the application. The HomeController class contains logic that is called by default when the application starts.
- Models for the data-model files such as LINQ-to-SQL .dbml files or data-entity files.
- Scripts for the script files, such as those that support ASP.NET AJAX and jQuery.
- Views for the view page files. This folder contains three subfolders: Account, Home, and Shared. The Account folder contains views that are used as the UI for logging in and changing passwords. The Home folder contains an Index view (the default starting page for the application) and an about page view. The Shared folder contains the master-page view for the application.
First of all you need to add a reference for "Microsoft.Practices.EnterpriseLibrary.Data.dll".
Then add a config section in the configuration file.
- <configSections>
- <sectionname="dataConfiguration"
- type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data" />
- </configSections>
- <connectionStrings>
- <addname="NorthWNDConnectionString" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|NORTHWND.MDF;User Instance=true"
- providerName="System.Data.SqlClient" />
- <addname="ApplicationServices"
- connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
- providerName="System.Data.SqlClient" />
- </connectionStrings>

Image 5.
CustomerController.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- namespace MVCUsingDAAB.Controllers
- {
- public class Default1Controller : Controller
- {
- //
- // GET: /Default1/
- public ActionResult Index()
- {
- return View();
- }
- }
- }
- <li><%:Html.ActionLink("Customers", "Index", "Customer")%></li>

Image 6.
Let me copy my models classes code first:
Models/Customer.cs
- using System;
- using System.Diagnostics;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.ComponentModel;
- using System.ComponentModel.DataAnnotations;
- namespace MVCUsingDAAB.Models
- {
- public class Customers
- {
- ///<summary>
- /// Gets the CustomerID.
- ///</summary>
- public string CustomerID { get; set; }
- ///<summary>
- /// Gets the CompanyName.
- ///</summary>
- public string CompanyName { get; set; }
- ///<summary>
- /// Gets the ContactName.
- ///</summary>
- public string ContactName { get; set; }
- ///<summary>
- /// Gets the Address.
- ///</summary>
- public string Address { get; set; }
- ///<summary>
- /// Gets the City.
- ///</summary>
- public string City { get; set; }
- public Customers(string id, stringcompanyname, stringcontactname, string address, string city)
- {
- CustomerID = id;
- CompanyName = companyname;
- ContactName = contactname;
- Address = address;
- City = city;
- }
- public Customers()
- {
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MVCUsingDAAB.Models
- {
- interface ICustomerModel
- {
- List<Customers> GetCustomers();
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using MvcContrib.Pagination;
- using MvcContrib.UI.Grid;
- using System.Data;
- namespace MVCUsingDAAB.Models
- {
- public class CustomerListContainerViewModel
- {
- public IPagination<Customers> CustomerPagedList { get; set; }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace MVCUsingDAAB.Models
- {
- interface ICustomerModel
- {
- List<Customers> GetCustomers();
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- usingSystem.Text;
- namespace MVCUsingDAAB.Models
- {
- interface ICustomerModel
- {
- List<Customers> GetCustomers();
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using Microsoft.Practices.EnterpriseLibrary.Data;
- using System.Configuration;
- using System.Data;
- using System.Data.Common;
- using MVCUsingDAAB.Models;
- using MvcContrib.Pagination;
- namespace MVCUsingDAAB.Controllers
- {
- public class CustomerController : Controller
- {
- //
- // GET: /Customer/
- ICustomerModel customerRepository;
- public CustomerController()
- {
- customerRepository = newCustomerModel();
- }
- public ActionResult Index(int? page)
- {
- var customerPagedList = customerRepository.GetCustomers().AsPagination(page ?? 1, 10);
- var customerListContainer = new CustomerListContainerViewModel
- {
- CustomerPagedList = customerPagedList,
- };
- return View(customerListContainer);
- }
- }
- }
- <%@ControlLanguage="C#" Inherits="System.Web.Mvc.ViewUserControl<MVCUsingDAAB.Models.CustomerListContainerViewModel>"%>
- <%@ImportNamespace="MvcContrib.UI.Grid"%>
- <%@ImportNamespace="MVCUsingDAAB.Models"%>
- <%@ImportNamespace="MvcContrib.UI.Grid.ActionSyntax"%>
- <%=Html.Grid(Model.CustomerPagedList).AutoGenerateColumns()
- .Columns(column => {
- column.For(a =>Html.ActionLink("Details", "Details", new { id = a.CustomerID })).InsertAt(0).Encode(false);
- })
- .Attributes(@class =>"table-list")
- %>
- <%@ControlLanguage="C#"Inherits="System.Web.Mvc.ViewUserControl<MvcContrib.Pagination.IPagination>"%>
- <%@ImportNamespace="MvcContrib.UI.Pager"%>
- <p/>
- <%=Html.Pager(Model)
- .First("First")
- .Last("Last")
- .Next("Next")
- .Previous("Previous") %>
- <p/>
- <%@PageTitle=""Language="C#" MasterPageFile="~/Views/Shared/Site.Master"
- Inherits="System.Web.Mvc.ViewPage<MVCUsingDAAB.Models.CustomerListContainerViewModel>"%>
- <asp:ContentID="Content1"ContentPlaceHolderID="TitleContent"runat="server">
- Index
- </asp:Content>
- <asp:ContentID="Content2"ContentPlaceHolderID="MainContent"runat="server">
- <h2>Customers</h2>
- <%Html.RenderPartial("AllCustomers", Model); %>
- <%Html.RenderPartial("Pager", Model.CustomerPagedList); %>
- </asp:Content>

Summary
In this article I tried to show how to use Data Access Application Block using MVC 2 Pattern. If you have any questions or comments then drop me a line in C# Corner comments section or if you have another good solution then share with us.

Enrique MolinaPosted Jun 13, 2014, 4:25 PM
Hi Raj, it was a really helpful the example about a mixture between MVC and Enterprise Library 6, Now i’m trying to build a website using MVC 5 and Enterprise Library 6 but I have no idea what would be the best way to do that, in terms of paging, sorting, searching, CRUD operations and so on. Please could you give me some advices about that or sources of information on how to set an architecture up base on this requirement, I’ve already looked for this information but there is a little or nothing about it.
Raj KumarPosted May 19, 2013, 3:42 AM
Guys i can not find the source code of this article because it's too old. But i can solve the issues please tell me wat errors are you facing.
Sen MathewPosted May 19, 2011, 11:00 PM
I know that this is very old post. But still I has importance. Like What James said ,I am also saying this code is really very confusing. I am getting error after error. I am soorry to say this. When you post codes, Please consider others are beginers. The advantage is that All persons who read the code will understand. Please please do not try to post with errors. I tried my best to correct it. I am getting error after error. This may due to my lack of understanding. Some one post code to make others think of him that he is an expert. Please do not follwo their path. Here we all are just students. Just by looking into you code anyone will find out even some syntax error. Please help us. We spending a big part of our time to understand the code first. If you had commented it porperly we could save time. Please post a working code Thanks Sen
moneem razaPosted Sep 28, 2010, 6:03 AM
After looking deep into your code. I got few questions. Well I can see it says implementation of DAAB but I am bit confused in figuring out where actually have you implemented functionality of DAAB. Secondly Models/ICustomerModel.cs and Models/CustomerModel.cs both files seems to be the same I think CustomerModel.cs is the file with an implementation of GetCustomers() function where you got the real use of DAAB. Please Guide me and also provide a downloadable copy of your code if you can. Thanks.
James LawrencePosted Jul 30, 2010, 3:49 AM
Hi Raj, I love these articles - MVC is a powerful tool, but there are many concepts that I cannot get my head around. Taking your example above, how would you add a search facility to the app searching for say company name or City? I find this very confusing, and have reverted to using a jQuery datatable tool. Do you have suggestions? regards, James