.NetTiers使用技巧
.NetTiers是.Net下面一款出色的自动生成模版,能够非常快速地生成数据访问层并且可以方便地进行扩展
支持的特性也相当多,除常规的CRUD+分页查询外,还包含以下特性:
1. 自定义存储过程的自动封装, 这样就不用额外写代码就可以完成自定义存储过程的调用了, 但是存储过程要以它规定的命名规则进行命名这样它才会识别出来.
2. 对索引列生成查询方法, 比如在用户表中经常给定用户名和密码这两个条件, 在这两个列上建立非聚集索引,那么.NetTiers将自动生成包含这两个条件的查询方法.
3.支持Deepload级联查询, 这个虽然效率不是很高,但在中小型的应用上还是相当方便地.
4. 使用Processor机制来完成一系列流程的连续调用,进行事务处理.
具体的使用方法可以参见园子里面其它人写的文章或官方文档,这里主要要讨论的是如果使用.NetTiers生成的东西.
通常,我们的项目一般会使用一些源代码管理工具,这样的话在编辑一份代码文件之前需要将它签出来,但是在实际工具中,数据库的结构可能随时发生变化,在这种情况下就需要重新使用.NetTiers进行代码生成,如果我们直接使用它生成出来的解决方案进行开发的话就会遇到一些麻烦,这时要手动将它要生成的文件一个一个签出来几乎太困难了,那么怎么办呢?
我的办法是不直接使用它生成的解决方案,而是引用它编译之后的程序集,扩展部分的代码写在自己的工程中,从它生成的基类继承过来. 这样每将即使是要改库只需要将它生成的几个程序集签出来,重新运行CodeSmith生成代码后把编译出来的程序集拷贝过来覆盖掉再提交就OK了.
这时我们的Service层看起来可能是这个样子:
public class UsersFacade : UsersServiceBase
{
public UsersFacade()
: base() { }
public Users ValidateUser(string username, string password)
{
SqlFilterBuilder<UsersColumn> filter = new SqlFilterBuilder<UsersColumn>();
filter.Junction = SqlUtil.AND;
filter.AppendEquals(UsersColumn.Name, username);
filter.AppendEquals(UsersColumn.Password, password);
int count = 0;
TList<Users> users = DataRepository.UsersProvider.GetPaged(filter.ToString(), null, 0, 20, out count);
if (users.Count > 0)
return users[0];
else
return null;
}
public Users ValidateUserAlter(string username, string password)
{
TList<Users> users = Find(string.Format("Name = '{0}' AND Password = '{1}'", username, password));
if (users != null && users.Count > 0)
return users[0];
else
return null;
}
public Users ValidateUserThird(string username, string password)
{
GB.Data.ParameterizedSqlFilterBuilder<Users> filter = new ParameterizedSqlFilterBuilder<Users>();
filter.AppendEquals("Name", "Darkangel");
filter.AppendEquals("Password", "5364241");
TList<Users> users = GB.Data.DataRepository.UsersProvider.Find(filter.GetParameters());
if (users != null && users.Count > 0)
return users[0];
else
return null;
}
}
再看看单元测试的代码:{
public UsersFacade()
: base() { }
public Users ValidateUser(string username, string password)
{
SqlFilterBuilder<UsersColumn> filter = new SqlFilterBuilder<UsersColumn>();
filter.Junction = SqlUtil.AND;
filter.AppendEquals(UsersColumn.Name, username);
filter.AppendEquals(UsersColumn.Password, password);
int count = 0;
TList<Users> users = DataRepository.UsersProvider.GetPaged(filter.ToString(), null, 0, 20, out count);
if (users.Count > 0)
return users[0];
else
return null;
}
public Users ValidateUserAlter(string username, string password)
{
TList<Users> users = Find(string.Format("Name = '{0}' AND Password = '{1}'", username, password));
if (users != null && users.Count > 0)
return users[0];
else
return null;
}
public Users ValidateUserThird(string username, string password)
{
GB.Data.ParameterizedSqlFilterBuilder<Users> filter = new ParameterizedSqlFilterBuilder<Users>();
filter.AppendEquals("Name", "Darkangel");
filter.AppendEquals("Password", "5364241");
TList<Users> users = GB.Data.DataRepository.UsersProvider.Find(filter.GetParameters());
if (users != null && users.Count > 0)
return users[0];
else
return null;
}
}
[TestFixture]
public class SampleFixture
{
Users testUser = new Users();
[TestFixtureSetUp]
public void Setup()
{
Users u = new Users();
u.Name = "Darkangel";
u.Password = "5364241";
u.UserId = IdentityGenerator.Instance.NextIdentity();
testUser.UserId = u.UserId;
UsersFacade fa = new UsersFacade();
fa.Insert(u);
}
[TestFixtureTearDown]
public void Clearup()
{
UsersFacade f = new UsersFacade();
f.Delete(testUser.UserId);
}
[Test]
public void GetAllUserTest()
{
UsersFacade facade = new UsersFacade();
//TList<Users> list = facade.Find("Name='Darkangel'");
TList<Users> list = facade.GetAll();
Console.WriteLine("Toatal user acount is : " + list.Count);
Assert.IsNotNull(list);
Assert.IsTrue(list.Count > 0);
}
[Test]
public void ValidateUserTest()
{
UsersFacade facade = new UsersFacade();
Users u = facade.ValidateUser("Darkangel", "5364241");
Assert.IsNotNull(u);
Assert.IsTrue(u.UserId.Length > 0);
}
[Test]
public void ValidateUserAlterTest()
{
UsersFacade facade = new UsersFacade();
Users u = facade.ValidateUserAlter("Darkangel", "5364241");
Assert.IsNotNull(u);
Assert.IsTrue(u.UserId.Length > 0);
}
[Test]
public void ValidateuserThirdTest()
{
UsersFacade facade = new UsersFacade();
Users u = facade.ValidateUserThird("Darkangel", "5364241");
Assert.IsNotNull(u);
Assert.IsTrue(u.UserId.Length > 0);
}
}
这样我们就只需要管理我们的生成配置文件就可以了.public class SampleFixture
{
Users testUser = new Users();
[TestFixtureSetUp]
public void Setup()
{
Users u = new Users();
u.Name = "Darkangel";
u.Password = "5364241";
u.UserId = IdentityGenerator.Instance.NextIdentity();
testUser.UserId = u.UserId;
UsersFacade fa = new UsersFacade();
fa.Insert(u);
}
[TestFixtureTearDown]
public void Clearup()
{
UsersFacade f = new UsersFacade();
f.Delete(testUser.UserId);
}
[Test]
public void GetAllUserTest()
{
UsersFacade facade = new UsersFacade();
//TList<Users> list = facade.Find("Name='Darkangel'");
TList<Users> list = facade.GetAll();
Console.WriteLine("Toatal user acount is : " + list.Count);
Assert.IsNotNull(list);
Assert.IsTrue(list.Count > 0);
}
[Test]
public void ValidateUserTest()
{
UsersFacade facade = new UsersFacade();
Users u = facade.ValidateUser("Darkangel", "5364241");
Assert.IsNotNull(u);
Assert.IsTrue(u.UserId.Length > 0);
}
[Test]
public void ValidateUserAlterTest()
{
UsersFacade facade = new UsersFacade();
Users u = facade.ValidateUserAlter("Darkangel", "5364241");
Assert.IsNotNull(u);
Assert.IsTrue(u.UserId.Length > 0);
}
[Test]
public void ValidateuserThirdTest()
{
UsersFacade facade = new UsersFacade();
Users u = facade.ValidateUserThird("Darkangel", "5364241");
Assert.IsNotNull(u);
Assert.IsTrue(u.UserId.Length > 0);
}
}
还在等啥,快去试试吧.