很久没有更新blog了。这几天在研究一个开源电子商务源码NopCommerce。
先来一张工程的图案片把
基于3层的结构。所以很好理解。
用到了2个实体层。一个在DataAccess里,于数据库字段一一对应。还有一个在Common里,与业务相关的实体类。这2个实体层之间的互传在DataAccessSql里实现的。
下面是税率的源码:
public class DBTaxByCountry : BaseDBEntity
{
public DBTaxByCountry()
{
}
#endregion
Properties#region Properties
/**//// <summary>
/// Gets or sets the TaxByCountry identifier
/// </summary>
public int TaxByCountryID { get; set; }
/**//// <summary>
/// Gets or sets the tax category identifier
/// </summary>
public int TaxCategoryID { get; set; }
/**//// <summary>
/// Gets or sets the country identifier
/// </summary>
public int CountryID { get; set; }
/**//// <summary>
/// Gets or sets the percentage
/// </summary>
public decimal Percentage { get; set; }
#endregion
}
public abstract class DBTaxByCountryProvider : ProviderBase
{
Fields#region Fields
private static bool s_Initialized;
private static Exception s_InitializeException;
private static object s_lock;
private static DBTaxByCountryProvider s_Provider;
private static DBTaxByCountryProviderCollection s_Providers;
#endregion
Ctor#region Ctor
static DBTaxByCountryProvider()
{
s_lock = new object();
s_Initialized = false;
s_InitializeException = null;
}
#endregion
Methods#region Methods
public abstract void DeleteTaxByCountry(int TaxByCountryID);
public abstract DBTaxByCountry GetByTaxByCountryID(int TaxByCountryID);
public abstract DBTaxByCountryCollection GetAllByCountryID(int CountryID);
public abstract DBTaxByCountry InsertTaxByCountry(int TaxCategoryID, int CountryID, decimal Percentage);
public abstract DBTaxByCountry UpdateTaxByCountry(int TaxByCountryID, int TaxCategoryID, int CountryID, decimal Percentage);
#endregion
Utilities#region Utilities
private static DBTaxByCountryProviderSection GetSection()
{
return (DBTaxByCountryProviderSection)ConfigurationManager.GetSection("nopDataProviders/TaxByCountryProvider");
}
private static void Initialize()
{
if (s_Initialized)
{
if (s_InitializeException != null)
{
throw s_InitializeException;
}
}
else
{
if (s_InitializeException != null)
{
throw s_InitializeException;
}
lock (s_lock)
{
if (s_Initialized)
{
if (s_InitializeException != null)
{
throw s_InitializeException;
}
}
else
{
try
{
DBTaxByCountryProviderSection section = GetSection();
if (((section.DefaultProvider == null) || (section.Providers == null)) || (section.Providers.Count < 1))
{
throw new ProviderException("Default provider not specified");
}
s_Providers = new DBTaxByCountryProviderCollection();
if (HostingEnvironment.IsHosted)
{
ProvidersHelper.InstantiateProviders(section.Providers, s_Providers, typeof(DBTaxByCountryProvider));
}
else
{
foreach (ProviderSettings settings in section.Providers)
{
Type c = Type.GetType(settings.Type, true, true);
if (!typeof(DBTaxByCountryProvider).IsAssignableFrom(c))
{
throw new ArgumentException(string.Format("Provider must implement {0} type", new object[] { typeof(DBTaxByCountryProvider).ToString() }));
}
DBTaxByCountryProvider provider = (DBTaxByCountryProvider)Activator.CreateInstance(c);
NameValueCollection parameters = settings.Parameters;
NameValueCollection config = new NameValueCollection(parameters.Count, StringComparer.Ordinal);
foreach (string str2 in parameters)
{
config[str2] = parameters[str2];
}
provider.Initialize(settings.Name, config);
s_Providers.Add(provider);
}
}
s_Provider = s_Providers[section.DefaultProvider];
if (s_Provider == null)
{
throw new ConfigurationErrorsException(string.Format("Default provider not found. {0}. Line number: {1}", section.ElementInformation.Properties["defaultProvider"].Source, section.ElementInformation.Properties["defaultProvider"].LineNumber));
}
s_Providers.SetReadOnly();
}
catch (Exception exception)
{
s_InitializeException = exception;
throw;
}
s_Initialized = true;
}
}
}
}
#endregion
Properties#region Properties
public static DBTaxByCountryProvider Provider
{
get
{
Initialize();
return s_Provider;
}
}
public static DBTaxByCountryProviderCollection Providers
{
get
{
Initialize();
return s_Providers;
}
}
#endregion
}
public class SQLTaxByCountryProvider : DBTaxByCountryProvider
{
Fields#region Fields
private string _sqlConnectionString;
#endregion
Utilities#region Utilities
private DBTaxByCountry GetTaxByCountryFromReader(IDataReader dataReader)
{
DBTaxByCountry taxByCountry = new DBTaxByCountry();
taxByCountry.TaxByCountryID = NopSqlDataHelper.GetInt(dataReader, "TaxByCountryID");
taxByCountry.TaxCategoryID = NopSqlDataHelper.GetInt(dataReader, "TaxCategoryID");
taxByCountry.CountryID = NopSqlDataHelper.GetInt(dataReader, "CountryID");
taxByCountry.Percentage = NopSqlDataHelper.GetDecimal(dataReader, "Percentage");
return taxByCountry;
}
#endregion
Methods#region Methods
public override void Initialize(string name, NameValueCollection config)
{
if (config == null)
{
throw new ArgumentNullException("config");
}
base.Initialize(name, config);
string connectionStringName = config["connectionStringName"];
if (String.IsNullOrEmpty(connectionStringName))
throw new ProviderException("Connection name not specified");
this._sqlConnectionString = NopSqlDataHelper.GetConnectionString(connectionStringName);
if ((this._sqlConnectionString == null) || (this._sqlConnectionString.Length < 1))
{
throw new ProviderException(string.Format("Connection string not found. {0}", connectionStringName));
}
config.Remove("connectionStringName");
if (config.Count > 0)
{
string key = config.GetKey(0);
if (!string.IsNullOrEmpty(key))
{
throw new ProviderException(string.Format("Provider unrecognized attribute. {0}", new object[] { key }));
}
}
}
public override void DeleteTaxByCountry(int TaxByCountryID)
{
Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryDelete");
db.AddInParameter(dbCommand, "TaxByCountryID", DbType.Int32, TaxByCountryID);
int retValue = db.ExecuteNonQuery(dbCommand);
}
public override DBTaxByCountry GetByTaxByCountryID(int TaxByCountryID)
{
DBTaxByCountry taxByCountry = null;
if (TaxByCountryID == 0)
return taxByCountry;
Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryLoadByPrimaryKey");
db.AddInParameter(dbCommand, "TaxByCountryID", DbType.Int32, TaxByCountryID);
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
if (dataReader.Read())
{
taxByCountry = GetTaxByCountryFromReader(dataReader);
}
}
return taxByCountry;
}
public override DBTaxByCountryCollection GetAllByCountryID(int CountryID)
{
DBTaxByCountryCollection taxByCountryCollection = new DBTaxByCountryCollection();
if (CountryID == 0)
return taxByCountryCollection;
Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryLoadAllByCountryID");
db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
using (IDataReader dataReader = db.ExecuteReader(dbCommand))
{
while (dataReader.Read())
{
DBTaxByCountry taxByCountry = GetTaxByCountryFromReader(dataReader);
taxByCountryCollection.Add(taxByCountry);
}
}
return taxByCountryCollection;
}
public override DBTaxByCountry InsertTaxByCountry(int TaxCategoryID, int CountryID, decimal Percentage)
{
DBTaxByCountry taxByCountry = null;
Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryInsert");
db.AddOutParameter(dbCommand, "TaxByCountryID", DbType.Int32, 0);
db.AddInParameter(dbCommand, "TaxCategoryID", DbType.Int32, TaxCategoryID);
db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
db.AddInParameter(dbCommand, "Percentage", DbType.Decimal, Percentage);
if (db.ExecuteNonQuery(dbCommand) > 0)
{
int TaxByCountryID = Convert.ToInt32(db.GetParameterValue(dbCommand, "@TaxByCountryID"));
taxByCountry = GetByTaxByCountryID(TaxByCountryID);
}
return taxByCountry;
}
public override DBTaxByCountry UpdateTaxByCountry(int TaxByCountryID, int TaxCategoryID, int CountryID, decimal Percentage)
{
DBTaxByCountry taxByCountry = null;
Database db = NopSqlDataHelper.CreateConnection(_sqlConnectionString);
DbCommand dbCommand = db.GetStoredProcCommand("Nop_TaxByCountryUpdate");
db.AddInParameter(dbCommand, "TaxByCountryID", DbType.Int32, TaxByCountryID);
db.AddInParameter(dbCommand, "TaxCategoryID", DbType.Int32, TaxCategoryID);
db.AddInParameter(dbCommand, "CountryID", DbType.Int32, CountryID);
db.AddInParameter(dbCommand, "Percentage", DbType.Decimal, Percentage);
if (db.ExecuteNonQuery(dbCommand) > 0)
taxByCountry = GetByTaxByCountryID(TaxByCountryID);
return taxByCountry;
}
#endregion
}
也没用来什么让人特别感兴趣的技术。
例子用到了provider模式,主要目的在于为税率
进行定义和实现分离。
下一篇就用LINQ改写它的客户资料数据访问层。其实主要是为了学习LINQ的。