Castle.ActiveRecord的分页示例:
分页结果泛型类:

Code
1
namespace EasyNet.Core
2

{
3
/**//// <summary>
4
/// 分页结果
5
/// </summary>
6
/// <typeparam name="T"></typeparam>
7
public class PagedResult<T>
8
{
9
/**//// <summary>
10
/// 记录总数
11
/// </summary>
12
public int Total
{ get; set; }
13
14
/**//// <summary>
15
/// 结果数据
16
/// </summary>
17
public T Data
{ get; set; }
18
}
19
}
20
文章分类(典型树形结构):

Code
1
namespace EasyNet.Model
2

{
3
using System.Collections.Generic;
4
5
using NHibernate.Criterion;
6
7
using Castle.ActiveRecord;
8
9
[ActiveRecord("site_articlecategory")]
10
public class ArticleCategory : ActiveRecordBase<ArticleCategory>
11
{
12
[PrimaryKey(PrimaryKeyType.Native)]
13
public int Id
{ get; set; }
14
15
[Property]
16
public string Name
{ get; set; }
17
18
[Property]
19
public bool ShowInDefault
{ get; set; }
20
21
[BelongsTo("ParentId")]
22
public ArticleCategory Parent
{ get; set; }
23
24
[HasMany(typeof(ArticleCategory), "ParentID", "site_articlecategory", Cascade = ManyRelationCascadeEnum.All)]
25
public IList<ArticleCategory> Children
{ get; set; }
26
27
[Newtonsoft.Json.JsonIgnore]
28
[HasAndBelongsToMany(Table = "site_articleinarticlecategory", ColumnRef = "ArticleId", ColumnKey = "CategoryId", Lazy = true)]
29
public IList<Article> Articles
{ get; set; }
30
31
}
32
}
33
文章实体(定义了一个根据文章分类分页查询,文章与文章分类是多对多的关系):

Code
1
namespace EasyNet.Model
2

{
3
using System.Collections.Generic;
4
5
using NHibernate.Criterion;
6
7
using Castle.ActiveRecord;
8
using Castle.ActiveRecord.Queries;
9
10
using Core;
11
12
[ActiveRecord("site_article")]
13
public class Article : ActiveRecordBase<Article>
14
{
15
[PrimaryKey(PrimaryKeyType.Native)]
16
public int Id
{ get; set; }
17
18
[Property]
19
public string Title
{ get; set; }
20
21
[Property]
22
public string Content
{ get; set; }
23
24
[Property]
25
public string Summary
{ get; set; }
26
27
[Property]
28
public string Author
{ get; set; }
29
30
[Property]
31
public bool IsNewsPhotos
{ get; set; }
32
33
[Property]
34
public string PhotoUrl
{ get; set; }
35
36
[BelongsTo("TemplateId")]
37
public ArticleTemplate Template
{ get; set; }
38
39
[BelongsTo("CreatorId")]
40
public User Creator
{ get; set; }
41
42
[BelongsTo("ModifierId")]
43
public User Modifier
{ get; set; }
44
45
[Property]
46
public bool Published
{ get; set; }
47
48
[Property]
49
public bool Deleted
{ get; set; }
50
51
[Property]
52
public long CreatedDatetime
{ get; set; }
53
54
[Property]
55
public long UpdatedDatetime
{ get; set; }
56
57
[Newtonsoft.Json.JsonIgnore]
58
[HasAndBelongsToMany(Table = "site_articleinarticlecategory", ColumnRef = "CategoryId", ColumnKey = "ArticleId", Lazy = true)]
59
public IList<ArticleCategory> Categories
{ get; set; }
60
61
public static PagedResult<Article[]> FindByCategory(int categoryId, int start, int limit)
62
{
63
CountQuery countQuery = new CountQuery(typeof(Article));
64
65
countQuery.SetParameter("Id", categoryId);
66
67
countQuery.Query = "select count(*) from Article as article left join article.Categories as category where category.Id in(:Id)";
68
69
int total = (int)ExecuteQuery(countQuery);
70
71
if (total == 0)
72
{
73
return null;
74
}
75
76
SimpleQuery<Article> query = new SimpleQuery<Article>("select article from Article as article left join article.Categories as category where category.Id in(:Id)");
77
78
query.SetParameter("Id", categoryId);
79
query.SetQueryRange(start, limit);
80
81
Article[] articles = query.Execute();
82
83
PagedResult<Article[]> result = new PagedResult<Article[]>();
84
85
result.Data = articles;
86
result.Total = total;
87
88
return result;
89
}
90
}
91
}
92