Products

Solutions

Resources

Partners

Community

About

New Community Website

Ordinarily, you'd be at the right spot, but we've recently launched a brand new community website... For the community, by the community.

Yay... Take Me to the Community!

The Community Blog is a personal opinion of community members and by no means the official standpoint of DNN Corp or DNN Platform. This is a place to express personal thoughts about DNNPlatform, the community and its ecosystem. Do you have useful information that you would like to share with the DNN Community in a featured article or blog? If so, please contact .

The use of the Community Blog is covered by our Community Blog Guidelines - please read before commenting or posting.


DAL 2 - Repository Deep Dive (1)

I continue my deep dive into DotNetNuke 7’s new DAL 2 with a look at the features that make up the repository component.

The IRepository of T interface can be considered the core of the new DAL 2. While the Execute methods of the IDataContext interface provide a DAL + like API, the benefits of the new API are really found when using the repository. The Repository Pattern is a common design pattern in modern data access layers. Martin Fowler’s definition of the pattern [1] is:

Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.

The pattern at its simplest provides simple CRUD (Create, Retrieve, Update and Delete) methods. In the DotNetNuke implementation there are a few extra overloads to provide flexibility. Listing 1 shows the complete Interface.

Listing 1: The IRepository Interface

DAL2_Repository_1

The Get methods

There are six methods that get (or retrieve) items from the database. Four of these methods return collections and 2 return single items.

  • Collection methods
    • Get
    • Get of TScopeType
    • GetPage
    • GetPage of TScopeType
  • Object methods
    • GetById of TProperty
    • GetById of TProperty, TScopeType

These six methods are in pairs – 2 Get methods which return a collection, 2 GetPage methods that return a “page” of items and 2 GetById methods that return a single item. The 2 methods differ in each case by the addition of a TScopeType type parameter and parameter.

Let’s first take a look at the two Get methods. Listing 2 shows the use of the parameter-less overload to retrieve all of the tasks from the database.

Listing 2: Using the Get method to Retrieve a List of Tasks

DAL2_Repository_2

The pattern should be familiar by now. The repository is created by the data context and then the Get method is executed. This returns an IEnumerable which is then converted to a List and returned.

Under the covers the repository together with PetaPoco generates the SQL necessary to fetch all the tasks. PetaPoco generates a SQL statement to return every column in the table.

Listing 3: The Generated SQL from the Get method

DAL2_Repository_3

The other overload is used in conjunction with the Scope Attribute. In fact if the type of the repository does not have the Scope attribute then an exception is thrown. We can see how this works if we add a ModuleID property to the TaskInfo class (Listing 4) and a ModuleID column to the Tasks table. We can then use the Get overload as shown in Listing 5 resulting in the generated SQL shown in Listing 6.

Listing 4: Adding Module Scoping to the TaskInfo class

DAL2_Repository_4

Listing 5: Using the Get (by scope) Overload

DAL2_Repository_5

Listing 6: The Generated SQL for the Get (by scope) method

DAL2_Repository_6

As can be seen the generated query is parameterized reducing the risk of SQL injection attacks.

The GetPage methods

The other two methods (GetPage) that return a collection of items from the database are similar to the Get methods. Both methods return a “page” of data, and have two additional parameters - compared with their Get counterparts - the page index and the page size. Listing 7 shows an example of how to use these methods and Listing 8 shows the generated SQL.

Listing 7: Using the GetPage method

DAL2_Repository_7

Listing 8: The Generated SQL for the GetPage method

DAL2_Repository_8

PetaPoco handles the generation of the appropriate “paging” SQL and the repository returns an IPagedList of T. This collection interface and its associated concrete PagedList of T [2] implementation are new (in DotNetNuke 7) custom collections to support this API.

Essentially the IPagedList of T interface just extends the core .NET IList of T collection to add properties that provide metadata about the page of data, as well as the collection as a whole. For example, there are properties which indicate whether there is a previous page or a next page, and there are properties which indicate the number of pages in the complete data set, as well as the total no of items.

The IPagedList interface is shown in Listing 9. The new API also provides methods which allow developers to create paged lists from any IEnumerable collection. In this case the complete set of data is returned from the database and the paging is done using the LINQ Skip and Take methods. For example the call to GetPage in Listing 7 could be replaced by the following:

DAL2_Repository_8a

In fact the RepositoryBase of T class uses this logic internally to return a page of data from the cached collection – see Listing 10.

Listing 9: The IPagedList of T Interface

DAL2_Repository_9

Listing 10: Implementation of the RepositoryBase of T GetPage method

DAL2_Repository_10

As the IPagedList inherits from IList and PagedList inherits from List - both can essentially be used wherever their base class is used and both implement IEnumerable so they can be used with LINQ.

The GetById methods

In addition to the methods that return collections there are two methods that return single objects. Again these methods only differ as to whether they are scoped or not. By default the convention is that the column name for the property used as an “id” is “ID”, but this can be controlled by the PrimaryKey attribute.

Listing 11 shows an example of using the GetById method and Listing 12 shows the generated SQL.

Listing 11: Using the GetById method

DAL2_Repository_11

Listing 12: The Generated SQL for the GetById method

DAL2_Repository_12

[1] For more on the repository pattern see - http://www.martinfowler.com/eaaCatalog/repository.html

[2] IPagedList of T and PagedList of T are heavily based on the ASP.NET MVC Paged List Helper created by Rob Conery - http://blog.wekeroad.com/2007/12/10/aspnet-mvc-pagedlistt/

Comments

Yehuda Tiram
The sql code that is following each method here is very helpful for the concept understanding. I wish you put some more of it for the rest of the methods.
Yehuda
Yehuda Tiram Monday, September 16, 2013 12:28 PM (link)

Comment Form

Only registered users may post comments.

NewsArchives


Aderson Oliveira (22)
Alec Whittington (11)
Alessandra Daniels (3)
Alex Shirley (10)
Andrew Hoefling (3)
Andrew Nurse (30)
Andy Tryba (1)
Anthony Glenwright (5)
Antonio Chagoury (28)
Ash Prasad (37)
Ben Schmidt (1)
Benjamin Hermann (25)
Benoit Sarton (9)
Beth Firebaugh (12)
Bill Walker (36)
Bob Kruger (5)
Bogdan Litescu (1)
Brian Dukes (2)
Brice Snow (1)
Bruce Chapman (20)
Bryan Andrews (1)
cathal connolly (55)
Charles Nurse (163)
Chris Hammond (213)
Chris Paterra (55)
Clint Patterson (108)
Cuong Dang (21)
Daniel Bartholomew (2)
Daniel Mettler (181)
Daniel Valadas (48)
Dave Buckner (2)
David Poindexter (12)
David Rodriguez (3)
Dennis Shiao (1)
Doug Howell (11)
Erik van Ballegoij (30)
Ernst Peter Tamminga (80)
Francisco Perez Andres (17)
Geoff Barlow (12)
George Alatrash (12)
Gifford Watkins (3)
Gilles Le Pigocher (3)
Ian Robinson (7)
Israel Martinez (17)
Jan Blomquist (2)
Jan Jonas (3)
Jaspreet Bhatia (1)
Jenni Merrifield (6)
Joe Brinkman (274)
John Mitchell (1)
Jon Henning (14)
Jonathan Sheely (4)
Jordan Coopersmith (1)
Joseph Craig (2)
Kan Ma (1)
Keivan Beigi (3)
Kelly Ford (4)
Ken Grierson (10)
Kevin Schreiner (6)
Leigh Pointer (31)
Lorraine Young (60)
Malik Khan (1)
Matt Rutledge (2)
Matthias Schlomann (16)
Mauricio Márquez (5)
Michael Doxsey (7)
Michael Tobisch (3)
Michael Washington (202)
Miguel Gatmaytan (3)
Mike Horton (19)
Mitchel Sellers (40)
Nathan Rover (3)
Navin V Nagiah (14)
Néstor Sánchez (31)
Nik Kalyani (14)
Oliver Hine (1)
Patricio F. Salinas (1)
Patrick Ryan (1)
Peter Donker (54)
Philip Beadle (135)
Philipp Becker (4)
Richard Dumas (22)
Robert J Collins (5)
Roger Selwyn (8)
Ruben Lopez (1)
Ryan Martinez (1)
Sacha Trauwaen (1)
Salar Golestanian (4)
Sanjay Mehrotra (9)
Scott McCulloch (1)
Scott Schlesier (11)
Scott Wilkinson (3)
Scott Willhite (97)
Sebastian Leupold (80)
Shaun Walker (237)
Shawn Mehaffie (17)
Stefan Cullmann (12)
Stefan Kamphuis (12)
Steve Fabian (31)
Steven Fisher (1)
Tony Henrich (3)
Torsten Weggen (3)
Tycho de Waard (4)
Vicenç Masanas (27)
Vincent Nguyen (3)
Vitaly Kozadayev (6)
Will Morgenweck (40)
Will Strohl (180)
William Severance (5)
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out
What is Liquid Content?
Find Out