J
jason
In the microsoft starter kit Time Tracker application, the data access
layer code consist of three cs files.
DataAccessHelper.cs
DataAcess.cs
SQLDataAccessLayer.cs
DataAcccessHelper appears to be checking that the correct data type is
used
DataAcess sets an abstract class and methods
SQLDataAccessLayer implements DataAcess
My noob question is why abstract? In Laymans terms please.
Granted, I'm still struggling with OOP concepts, but If you were coding
this application from scratch, and none of this code came from anywhere
else or was expected to be used anywhere would you really need all
three cs files and the inheritance of a baseclass public class
SQLDataAccess : DataAccess ?
Time Tracker can be found here:
http://msdn.microsoft.com/vstudio/express/vwd/starterkit/
The three sources in question:
====================
DataAccesshelper.cs:
using System;
using System.Configuration;
namespace ASPNET.StarterKit.DataAccessLayer {
public class DataAccessHelper {
public static DataAccess GetDataAccess() {
string dataAccessStringType =
ConfigurationManager.AppSettings["aspnet_staterKits_TimeTracker_DataAccessLayerType"];
if (String.IsNullOrEmpty(dataAccessStringType)) {
throw (new NullReferenceException("ConnectionString
configuration is missing from you web.config. It should contain
<connectionStrings> <add key=\"aspnet_staterKits_TimeTracker\"
value=\"Server=(local);Integrated
Security=True;Database=Issue_Tracker\" </connectionStrings>"));
}
else {
Type dataAccessType = Type.GetType(dataAccessStringType);
if (dataAccessType == null) {
throw (new NullReferenceException("DataAccessType can not be
found"));
}
Type tp =
Type.GetType("ASPNET.StarterKit.DataAccessLayer.DataAccess");
if (!tp.IsAssignableFrom(dataAccessType)) {
throw (new ArgumentException("DataAccessType does not
inherits from ASPNET.StarterKit.DataAccessLayer.DataAccess "));
}
DataAccess dc =
(DataAccess)Activator.CreateInstance(dataAccessType);
return (dc);
}
}
}
}
============================
DataAcess.cs:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using ASPNET.StarterKit.BusinessLogicLayer;
namespace ASPNET.StarterKit.DataAccessLayer {
public abstract class DataAccess {
/*** PROPERTIES ***/
protected string ConnectionString {
get {
if
(ConfigurationManager.ConnectionStrings["aspnet_staterKits_TimeTracker"]
== null)
throw (new NullReferenceException("ConnectionString
configuration is missing from you web.config. It should contain
<connectionStrings> <add key=\"aspnet_staterKits_TimeTracker\"
value=\"Server=(local);Integrated
Security=True;Database=Issue_Tracker\" </connectionStrings>"));
string connectionString =
ConfigurationManager.ConnectionStrings["aspnet_staterKits_TimeTracker"].ConnectionString;
if (String.IsNullOrEmpty(connectionString))
throw (new NullReferenceException("ConnectionString
configuration is missing from you web.config. It should contain
<connectionStrings> <add key=\"aspnet_staterKits_TimeTracker\"
value=\"Server=(local);Integrated
Security=True;Database=Issue_Tracker\" </connectionStrings>"));
else
return (connectionString);
}
}
/*** METHODS ***/
//Category
public abstract int CreateNewCategory(Category newCategory);
public abstract bool DeleteCategory(int categoryId);
public abstract List<Category> GetAllCategories();
public abstract Category GetCategoryByCategoryId(int Id);
public abstract List<Category> GetCategoriesByProjectId(int
projectId);
public abstract Category
GetCategoryByCategoryNameandProjectId(string categoryName, int
projectId);
public abstract bool UpdateCategory(Category newCategory);
//TimeEntry
public abstract int CreateNewTimeEntry(TimeEntry newTimeEntry);
public abstract bool DeleteTimeEntry(int timeEntryId);
public abstract List<TimeEntry> GetAllTimeEntries();
public abstract List<TimeEntry> GetTimeEntries(int projectId,
string userName);
public abstract TimeEntry GetTimeEntryById(int timeEntryId);
public abstract List<TimeEntry>
GetTimeEntriesByUserNameAndDates(string userName,
DateTime startingDate, DateTime endDate);
public abstract bool UpdateTimeEntry(TimeEntry timeEntry);
// Project
public abstract bool AddUserToProject(int projectId, string
userName);
public abstract int CreateNewProject(Project newProject);
public abstract bool DeleteProject(int projectID);
public abstract List<Project> GetAllProjects();
public abstract Project GetProjectById(int projectId);
public abstract List<Project> GetProjectsByManagerUserName(string
userName);
public abstract List<string> GetProjectMembers(int Id);
public abstract List<Project> GetProjectsByUserName(string
userName);
public abstract bool RemoveUserFromProject(int projectId, string
userName);
public abstract bool UpdateProject(Project projectToUpdate);
//User report
public abstract List<UserReport> GetUserReportsByProjectId(int
projectId);
public abstract List<UserReport> GetUserReportsByCategoryId(int
categoryId);
// UserTotalDurationReport
public abstract List<UserTotalDurationReport>
GetUserReportsByUserName(string userName);
}//class end
}//namespace
============================
sqldataaccesslayer.cs:
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using ASPNET.StarterKit.BusinessLogicLayer;
namespace ASPNET.StarterKit.DataAccessLayer {
public class SQLDataAccess : DataAccess {
/*** DELEGATE ***/
private delegate void TGenerateListFromReader<T>(SqlDataReader
returnData, ref List<T> tempList);
/***************************** BASE CLASS IMPLEMENTATION
*****************************/
/*** CATEGORY ***/
private const string SP_CATEGORY_CREATE =
"aspnet_starterkits_CreateNewCategory";
private const string SP_CATEGORY_DELETE =
"aspnet_starterkits_DeleteCategory";
private const string SP_CATEGORY_GETALLCATEGORIES =
"aspnet_starterkits_GetAllCategories";
private const string SP_CATEGORY_GETCATEGORYBYPROJECTID =
"aspnet_starterkits_GetCategoriesByProjectId";
private const string SP_CATEGORY_GETCATEGORYBYID =
"aspnet_starterkits_GetCategoryById";
private const string SP_CATEGORY_GETCATEGORYBYNAMEANDPROJECT =
"aspnet_starterkits_GetCategoryByNameAndProjectId";
private const string SP_CATEGORY_UPDATE =
"aspnet_starterkits_UpdateCategories";
public override int CreateNewCategory(Category newCategory) {
if (newCategory == null)
throw (new ArgumentNullException("newCategory"));
SqlCommand sqlCmd = new SqlCommand();
AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0,
ParameterDirection.ReturnValue, null);
AddParamToSQLCmd(sqlCmd, "@CategoryAbbreviation",
SqlDbType.NText, 255, ParameterDirection.Input,
newCategory.Abbreviation);
AddParamToSQLCmd(sqlCmd, "@CategoryEstimateDuration",
SqlDbType.Decimal, 0, ParameterDirection.Input,
newCategory.EstimateDuration);
AddParamToSQLCmd(sqlCmd, "@CategoryName", SqlDbType.NText, 255,
ParameterDirection.Input, newCategory.Name);
AddParamToSQLCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0,
ParameterDirection.Input, newCategory.ProjectId);
SetCommandType(sqlCmd, CommandType.StoredProcedure,
SP_CATEGORY_CREATE);
ExecuteScalarCmd(sqlCmd);
return ((int)sqlCmd.Parameters["@ReturnValue"].Value);
}
.... code continues on
============================
layer code consist of three cs files.
DataAccessHelper.cs
DataAcess.cs
SQLDataAccessLayer.cs
DataAcccessHelper appears to be checking that the correct data type is
used
DataAcess sets an abstract class and methods
SQLDataAccessLayer implements DataAcess
My noob question is why abstract? In Laymans terms please.
Granted, I'm still struggling with OOP concepts, but If you were coding
this application from scratch, and none of this code came from anywhere
else or was expected to be used anywhere would you really need all
three cs files and the inheritance of a baseclass public class
SQLDataAccess : DataAccess ?
Time Tracker can be found here:
http://msdn.microsoft.com/vstudio/express/vwd/starterkit/
The three sources in question:
====================
DataAccesshelper.cs:
using System;
using System.Configuration;
namespace ASPNET.StarterKit.DataAccessLayer {
public class DataAccessHelper {
public static DataAccess GetDataAccess() {
string dataAccessStringType =
ConfigurationManager.AppSettings["aspnet_staterKits_TimeTracker_DataAccessLayerType"];
if (String.IsNullOrEmpty(dataAccessStringType)) {
throw (new NullReferenceException("ConnectionString
configuration is missing from you web.config. It should contain
<connectionStrings> <add key=\"aspnet_staterKits_TimeTracker\"
value=\"Server=(local);Integrated
Security=True;Database=Issue_Tracker\" </connectionStrings>"));
}
else {
Type dataAccessType = Type.GetType(dataAccessStringType);
if (dataAccessType == null) {
throw (new NullReferenceException("DataAccessType can not be
found"));
}
Type tp =
Type.GetType("ASPNET.StarterKit.DataAccessLayer.DataAccess");
if (!tp.IsAssignableFrom(dataAccessType)) {
throw (new ArgumentException("DataAccessType does not
inherits from ASPNET.StarterKit.DataAccessLayer.DataAccess "));
}
DataAccess dc =
(DataAccess)Activator.CreateInstance(dataAccessType);
return (dc);
}
}
}
}
============================
DataAcess.cs:
using System;
using System.Configuration;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using ASPNET.StarterKit.BusinessLogicLayer;
namespace ASPNET.StarterKit.DataAccessLayer {
public abstract class DataAccess {
/*** PROPERTIES ***/
protected string ConnectionString {
get {
if
(ConfigurationManager.ConnectionStrings["aspnet_staterKits_TimeTracker"]
== null)
throw (new NullReferenceException("ConnectionString
configuration is missing from you web.config. It should contain
<connectionStrings> <add key=\"aspnet_staterKits_TimeTracker\"
value=\"Server=(local);Integrated
Security=True;Database=Issue_Tracker\" </connectionStrings>"));
string connectionString =
ConfigurationManager.ConnectionStrings["aspnet_staterKits_TimeTracker"].ConnectionString;
if (String.IsNullOrEmpty(connectionString))
throw (new NullReferenceException("ConnectionString
configuration is missing from you web.config. It should contain
<connectionStrings> <add key=\"aspnet_staterKits_TimeTracker\"
value=\"Server=(local);Integrated
Security=True;Database=Issue_Tracker\" </connectionStrings>"));
else
return (connectionString);
}
}
/*** METHODS ***/
//Category
public abstract int CreateNewCategory(Category newCategory);
public abstract bool DeleteCategory(int categoryId);
public abstract List<Category> GetAllCategories();
public abstract Category GetCategoryByCategoryId(int Id);
public abstract List<Category> GetCategoriesByProjectId(int
projectId);
public abstract Category
GetCategoryByCategoryNameandProjectId(string categoryName, int
projectId);
public abstract bool UpdateCategory(Category newCategory);
//TimeEntry
public abstract int CreateNewTimeEntry(TimeEntry newTimeEntry);
public abstract bool DeleteTimeEntry(int timeEntryId);
public abstract List<TimeEntry> GetAllTimeEntries();
public abstract List<TimeEntry> GetTimeEntries(int projectId,
string userName);
public abstract TimeEntry GetTimeEntryById(int timeEntryId);
public abstract List<TimeEntry>
GetTimeEntriesByUserNameAndDates(string userName,
DateTime startingDate, DateTime endDate);
public abstract bool UpdateTimeEntry(TimeEntry timeEntry);
// Project
public abstract bool AddUserToProject(int projectId, string
userName);
public abstract int CreateNewProject(Project newProject);
public abstract bool DeleteProject(int projectID);
public abstract List<Project> GetAllProjects();
public abstract Project GetProjectById(int projectId);
public abstract List<Project> GetProjectsByManagerUserName(string
userName);
public abstract List<string> GetProjectMembers(int Id);
public abstract List<Project> GetProjectsByUserName(string
userName);
public abstract bool RemoveUserFromProject(int projectId, string
userName);
public abstract bool UpdateProject(Project projectToUpdate);
//User report
public abstract List<UserReport> GetUserReportsByProjectId(int
projectId);
public abstract List<UserReport> GetUserReportsByCategoryId(int
categoryId);
// UserTotalDurationReport
public abstract List<UserTotalDurationReport>
GetUserReportsByUserName(string userName);
}//class end
}//namespace
============================
sqldataaccesslayer.cs:
using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web.UI.WebControls;
using ASPNET.StarterKit.BusinessLogicLayer;
namespace ASPNET.StarterKit.DataAccessLayer {
public class SQLDataAccess : DataAccess {
/*** DELEGATE ***/
private delegate void TGenerateListFromReader<T>(SqlDataReader
returnData, ref List<T> tempList);
/***************************** BASE CLASS IMPLEMENTATION
*****************************/
/*** CATEGORY ***/
private const string SP_CATEGORY_CREATE =
"aspnet_starterkits_CreateNewCategory";
private const string SP_CATEGORY_DELETE =
"aspnet_starterkits_DeleteCategory";
private const string SP_CATEGORY_GETALLCATEGORIES =
"aspnet_starterkits_GetAllCategories";
private const string SP_CATEGORY_GETCATEGORYBYPROJECTID =
"aspnet_starterkits_GetCategoriesByProjectId";
private const string SP_CATEGORY_GETCATEGORYBYID =
"aspnet_starterkits_GetCategoryById";
private const string SP_CATEGORY_GETCATEGORYBYNAMEANDPROJECT =
"aspnet_starterkits_GetCategoryByNameAndProjectId";
private const string SP_CATEGORY_UPDATE =
"aspnet_starterkits_UpdateCategories";
public override int CreateNewCategory(Category newCategory) {
if (newCategory == null)
throw (new ArgumentNullException("newCategory"));
SqlCommand sqlCmd = new SqlCommand();
AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0,
ParameterDirection.ReturnValue, null);
AddParamToSQLCmd(sqlCmd, "@CategoryAbbreviation",
SqlDbType.NText, 255, ParameterDirection.Input,
newCategory.Abbreviation);
AddParamToSQLCmd(sqlCmd, "@CategoryEstimateDuration",
SqlDbType.Decimal, 0, ParameterDirection.Input,
newCategory.EstimateDuration);
AddParamToSQLCmd(sqlCmd, "@CategoryName", SqlDbType.NText, 255,
ParameterDirection.Input, newCategory.Name);
AddParamToSQLCmd(sqlCmd, "@ProjectId", SqlDbType.Int, 0,
ParameterDirection.Input, newCategory.ProjectId);
SetCommandType(sqlCmd, CommandType.StoredProcedure,
SP_CATEGORY_CREATE);
ExecuteScalarCmd(sqlCmd);
return ((int)sqlCmd.Parameters["@ReturnValue"].Value);
}
.... code continues on
============================