Tier Generator 1.0






4.89/5 (133投票s)
一个用于快速应用程序开发的强大工具。
引言
Tier Generator是一个强大的工具,用于在C#中生成业务和数据层。它是一个代码生成工具,可以帮助用户快速生成和部署应用程序的业务和数据层。其背后的想法是为开发人员提供一个实用程序(工具),该工具能够快速生成一致且经过测试的源代码,这将有助于更快地启动和完成项目。
Tier Generator连接到Microsoft SQL Server数据库服务器,并在C#中生成业务和数据层。它还为DML操作生成存储过程。
业务层
Tier Generator在两个层(业务层和数据层)中生成代码。它为数据库中的每个表在业务层中生成一些类。例如,我们的数据库包含表Employee。Tier Generator将生成以下文件:
- 员工
- EmployeeKeys
- EmployeeFactory
Employee
(业务对象)类包含所有实例字段的声明以及属性。它还重写了AddValidationRules
方法,以将验证规则与业务对象的属性关联起来。它还包含所有字段的枚举。
public class Employee: BusinessObjectBase
{
#region InnerClass
public enum EmployeeFields
{
EmployeeID, Name, Password, Email, TeamID,DepartmentID, IsAdmin
}
#endregion
#region Data Members
int _employeeID;
string _name;
string _password;
string _email;
int _teamID;
int _departmentID;
bool _isAdmin;
#endregion
#region Properties
public int EmployeeID
{
get { return _employeeID; }
set
{
if (_employeeID != value)
{
_employeeID = value;
PropertyHasChanged("EmployeeID");
}
}
}
public string Name
{
get { return _name; }
set
{
if (_name != value)
{
_name = value;
PropertyHasChanged("Name");
}
}
}
.
.
.
#endregion
#region Validation
internal override void AddValidationRules()
{
ValidationRules.AddRules(new Validation.ValidateRuleNotNull("EmployeeID",
"EmployeeID"));
ValidationRules.AddRules(new Validation.ValidateRuleNotNull("Name",
"Name"));
ValidationRules.AddRules(new Validation.ValidateRuleStringMaxLength("Name",
"Name",50));
ValidationRules.AddRules(new Validation.ValidateRuleStringMaxLength("Password",
"Password",50));
ValidationRules.AddRules(new Validation.ValidateRuleStringMaxLength("Email",
"Email",100));
ValidationRules.AddRules(new Validation.ValidateRuleNotNull("TeamID",
"TeamID"));
ValidationRules.AddRules(new Validation.ValidateRuleNotNull("DepartmentID",
"DepartmentID"));
ValidationRules.AddRules(new Validation.ValidateRuleNotNull("IsAdmin",
"IsAdmin"));
}
#endregion
}
EmpolyeesKeys
(业务对象键)类包含表的主键列表。
public class EmployeeKeys
{
#region Data Members
int _employeeID;
#endregion
#region Constructor
public EmployeeKeys(int employeeID)
{
_employeeID = employeeID;
}
#endregion
#region Properties
public int EmployeeID
{
get { return _employeeID; }
}
#endregion
}
EmployeeFactory
(业务工厂)类包含
、Insert
、Delete
和Update
操作的方法。它为DML操作提供了以下方法:Select
public bool Insert(Employee businessObject)
public bool Update(Employee businessObject)
public Employee GetByPrimaryKey(EmployeeKeys keys)
public List<Employee> GetAll()
public List<Employee> GetAllBy(Employee.EmployeeFields fieldName, object value)
public bool Delete(EmployeeKeys keys)
public bool Delete(Employee.EmployeeFields fieldName, object value)
工厂类在数据层的帮助下执行DML操作。
public class EmployeeFactory
{
#region data Members
EmployeeSql _dataObject = null;
#endregion
#region Constructor
public EmployeeFactory()
{
_dataObject = new EmployeeSql();
}
#endregion
#region Public Methods
public bool Insert(Employee businessObject)
{
if (!businessObject.IsValid)
{
throw new InvalidBusinessObjectException(
businessObject.BrokenRulesList.ToString());
}
return _dataObject.Insert(businessObject);
}
public bool Update(Employee businessObject)
{
if (!businessObject.IsValid)
{
throw new InvalidBusinessObjectException(
businessObject.BrokenRulesList.ToString());
}
return _dataObject.Update(businessObject);
}
public Employee GetByPrimaryKey(EmployeeKeys keys)
{
return _dataObject.SelectByPrimaryKey(keys);
}
public List<Employee> GetAll()
{
return _dataObject.SelectAll();
}
public List<Employee> GetAllBy(Employee.EmployeeFields fieldName,
object value)
{
return _dataObject.SelectByField(fieldName.ToString(), value);
}
public bool Delete(EmployeeKeys keys)
{
return _dataObject.Delete(keys);
}
public bool Delete(Employee.EmployeeFields fieldName, object value)
{
return _dataObject.DeleteByField(fieldName.ToString(), value);
}
#endregion
}
数据层
Tier Generator生成的数据访问文件包含用于DML操作的方法。它使用存储过程进行DML操作。工厂类方法调用数据层方法进行插入和删除。
class EmployeeSql : DataLayerBase
{
#region Public Methods
/// <summary>
/// insert new row in the table
/// </summary>
/// <param name="businessObject">business object</param>
/// <returns>true of successfully insert</returns>
public bool Insert(Employee businessObject)
{
SqlCommand sqlCommand = new SqlCommand();
sqlCommand.CommandText = "dbo.[sp_Employee_Insert]";
sqlCommand.CommandType = CommandType.StoredProcedure;
// Use base class' connection object
sqlCommand.Connection = MainConnection;
try
{
sqlCommand.Parameters.Add(new SqlParameter("@EmployeeID", SqlDbType.Int, 4,
ParameterDirection.Output,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.EmployeeID));
sqlCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.NVarChar,
50, ParameterDirection.Input,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.Name));
sqlCommand.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar,
50, ParameterDirection.Input,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.Password));
sqlCommand.Parameters.Add(new SqlParameter("@Email", SqlDbType.NVarChar,
100, ParameterDirection.Input,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.Email));
sqlCommand.Parameters.Add(new SqlParameter("@TeamID", SqlDbType.Int,
4, ParameterDirection.Input,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.TeamID));
sqlCommand.Parameters.Add(new SqlParameter("@DepartmentID", SqlDbType.Int,
4, ParameterDirection.Input,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.DepartmentID));
sqlCommand.Parameters.Add(new SqlParameter("@IsAdmin", SqlDbType.Bit,
1, ParameterDirection.Input,
false, 0, 0, "",
DataRowVersion.Proposed,
businessObject.IsAdmin));
MainConnection.Open();
sqlCommand.ExecuteNonQuery();
businessObject.EmployeeID =
(int)sqlCommand.Parameters["@EmployeeID"].Value;
return true;
}
catch(Exception ex)
{
throw new Exception("Employee::Insert::Error occured.", ex);
}
finally
{
MainConnection.Close();
sqlCommand.Dispose();
}
}
#endregion
}
如何使用
Tier Generator生成的代码易于使用。在Visual Studio 2005中打开生成的项目并编译它。在Tier Generator生成的数据库中运行存储过程脚本。您可以在生成的文件夹中找到SQL脚本文件。
在现有项目中添加一个新的Windows/web项目,并将生成的代码的DLL添加到其中。为Windows应用程序添加app.config,为web应用程序添加web.config。从生成的app.config文件中获取连接字符串。您将在生成的文件夹中找到此文件。
<appSettings>
<add key="Main.ConnectionString"
value="Data Source=localhost;Initial Catalog=School;
User Id=sa;Password=sa" />
</appSettings>
以下是插入新记录的代码示例
public void AddNewRecord()
{
Employee emp = new Employee();
emp.EmployeeID = 1;
emp.FirstName = "Shakeel";
emp.LastName = "Iqbal";
.
.
.
.
EmployeeFactory empFact = new EmployeeFactory();
empFact.Insert(emp);
}
以下是选择所有记录的代码示例
public void SelectAll()
{
EmployeeFactory empFact = new EmployeeFactory();
List<Employee> list = empFact.GetAll();
dataGrid1.DataSource = list;
}
未来增强功能
我计划对Tier Generator进行一些未来的增强,并且我计划发布下一个版本的Tier Generator。在这个版本中,我将改进我的业务和数据层,并且我还将提供以下功能
- 生成Windows应用程序。
- 生成Web应用程序。
- 生成Web服务。