Data Access Layer - AC Gold

From AbleCommerce Wiki
Revision as of 13:12, 19 November 2012 by Mazhar (Talk | contribs)

Jump to: navigation, search

The Database

AC Gold database design is probably one of the best among the available shopping cart solutions. If you know a bit of databases, AC Gold database design is very easy to understand and follow. There are a few areas (e.g. Category and Catalog tables) that are slightly complex but in general you can just look at the database tables/fields and get a fair idea of what they are meant for.

Accessing Database

Ablecommerce Gold makes use of NHibernate for data access. For the most part we just have to deal with the 'Objects' in C#. The code that we have written complies with ASP.NET Data Source Object Model. This helps a great deal in using these objects directly in ASP.NET. Here I will take the example of Affiliates and explain how they are represented/accessed in the database, in the C# code and in the ASP.NET code.

Affiliates in Database

Here is how Affiliates are defined in database

CREATE TABLE ac_Affiliates ( 
	AffiliateId INT IDENTITY NOT NULL,
	StoreId INT NOT NULL,
	Name NVARCHAR(100) NOT NULL,
	PayeeName NVARCHAR(100) NULL,
	FirstName NVARCHAR(30) NULL,
	LastName NVARCHAR(50) NULL,
	Company NVARCHAR(50) NULL,
	Address1 NVARCHAR(100) NULL,
	Address2 NVARCHAR(100) NULL,
	City NVARCHAR(50) NULL,
	Province NVARCHAR(50) NULL,
	PostalCode NVARCHAR(15) NULL,
	CountryCode CHAR(2) NULL,
	PhoneNumber NVARCHAR(50) NULL,
	FaxNumber NVARCHAR(50) NULL,
	MobileNumber NVARCHAR(50) NULL,
	WebsiteUrl NVARCHAR(255) NULL,
	Email NVARCHAR(255) NULL,
	CommissionRate DECIMAL(9,4) NOT NULL,
	CommissionIsPercent BIT NOT NULL,
	CommissionOnTotal BIT NOT NULL,
	ReferralPeriodId TINYINT NOT NULL,
	ReferralDays SMALLINT NOT NULL,
	GroupId INT NULL,
	PRIMARY KEY(AffiliateId))

Affiliates in Code

In Ablecommerce Gold .NET code there are a total of 9 C# files related to affiliates. This is almost always the case for any database object represented in AC Gold.

Affiliate.cs
Affiliate.Generated.cs
Affiliate.hbm.xml
IAffiliateRepository.cs
IAffiliateRepository.Generated.cs
AffiliateRepository.cs
AffiliateRepository.Generated.cs
AffiliateDataSource.cs
AffiliateDataSource.Generated.cs

Five of the above files are generated. Four of them are custom coded. Although there are 9 files there are only 3 classes and one interface(thanks to C# partial classes). The 'Affiliate' class that represents an affiliate and 'Affiliate.hbm' represents the nhibernate mappings. The 'IAffiliateRepository', and 'AffiliateRepository' class that provide repository implementation for Affiliate object for example load/save methods. AffiliateDataSource class is simply a wrapper around AffiliateRepository and does have same methods as repository class does but instead of using different logic for the methods it just uses repository class in its methods. The major purpose of these DataSource classes is to provide backward code compatibility plus to comply with ASP.NET Object Data Source model.