惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
DataBreaches.Net
IT之家
IT之家
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
WordPress大学
WordPress大学
N
Netflix TechBlog - Medium
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
L
LangChain Blog
博客园 - Franky
美团技术团队
J
Java Code Geeks
Microsoft Security Blog
Microsoft Security Blog
博客园 - 叶小钗
小众软件
小众软件
Y
Y Combinator Blog
B
Blog RSS Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
D
Docker
Hugging Face - Blog
Hugging Face - Blog
Jina AI
Jina AI
罗磊的独立博客
大猫的无限游戏
大猫的无限游戏
Vercel News
Vercel News

博客园 - JerryZhao

[转载]jQuery对象VS DOM对象 [Reference] Navigation on Matrix Subtotal [Refereces]Creating an .ini File for the Application Manager [Refereces] Developing and Deploying Pocket PC Setup Applications [References]More on device app installs - Installing a Windows Mobile device application from a desktop MSI [References]Deploying .NET Compact Framework 2.0 Applications with .cab and .msi Files [References]Installing Multiple CAB Files [Reference]Wix Restart IIS code snippet [Reference] Windows Installer XML (WiX) 3.0 Snippets [Reference] Casle Demo App: Midway Summary [Reference][ActiveRecord] 之四:Cascade [Reference][ActiveRecord] 之六:继承 [Referece][ActiveRecord] 之七:多数据库配置 [Reference] [Castle AR] 5. Base Relations [Reference][Castle AR] 3. Validate [Reference][Castle AR] 2. ActiveRecord [Reference][Castle AR] 1. Starter [reference][Castle AR] 4. CRUD [Reference]NHibernateDataSource: A DataSourceControl for ASP.NET 2.0
[ActiveRecord] 之五:ActiveRecordMediator
JerryZhao · 2008-01-23 · via 博客园 - JerryZhao

http://www.rainsts.net/article.asp?id=243
有意无意的,很多文章都没有提及 ActiveRecordMediator 。

ActiveRecordMediator最大的用途是:即便你的实体类不是继承自 ActiveRecordBase (不推荐这么做),它也可以进行同等的创建、查找等操作;另外这个类实现了很多常用的方法,我们直接使用它就没必要为每个实体类写一堆相同的操作方法了(ActiveRecordBase的静态方法干吗声明为 protected internal?)。

[ActiveRecord]
public class User
{
  private int id;

  [PrimaryKey(PrimaryKeyType.Identity)]
  public int Id
  {
    get { return id; }
    set { id = value; }
  }
}

public static void Test()
{
  ActiveRecordMediator.Create(new User());
}

看看下面的类型定义,方法是不是很多?

public class ActiveRecordMediator
{
  public ActiveRecordMediator();
  public static void Create(object instance);
  public static void Delete(object instance);
  public static void DeleteAll(Type type);
  public static void DeleteAll(Type type, string where);
  public static object Execute(Type targetType, NHibernateDelegate call, object instance);
  public static object ExecuteQuery(IActiveRecordQuery q);
  public static Array FindAll(Type targetType);
  public static Array FindAll(Type targetType, params ICriterion[] criterias);
  public static Array FindAll(Type targetType, Order[] orders, params ICriterion[] criterias);
  public static object FindByPrimaryKey(Type targetType, object id);
  public static object FindByPrimaryKey(Type targetType, object id, bool throwOnNotFound);
  public static ISessionFactoryHolder GetSessionFactoryHolder();
  public static void Save(object instance);
  public static Array SlicedFindAll(Type targetType, int firstResult, int maxresults, params ICriterion[] criterias);
  public static Array SlicedFindAll(Type targetType, int firstResult, int maxresults, Order[] orders, params ICriterion[] criterias);
  public static void Update(object instance);
}

当然,Castle 还提供了ActiveRecordMediator<T> 和ActiveRecordBase<T>这两个泛型实现,但不知道为什么没有直接继承自ActiveRecordMediator和ActiveRecordBase,而且还少了一些实用的方法。看来鱼和熊掌不能兼得,当然你可以自己修改代码来的兼而并之。

在ActiveRecordMediator的多个方法中都使用了ICriterion接口,其真正实现是NHibernate.Expression名字空间。我们可以Expression静态类生成许多查询条件。

// 分页查询 (查询100<=id<200 的用户对象集合,返回第1页,每页10条记录)

ActiveRecordMediator.SlicedFindAll(typeof(User), 0, 10, Expression.Between("id", 100, 200));

-----------------

附:本文所有演示代码使用 2006-01-01 发布的 Castle ActiveRecord Beta3 版本。
Castle ActiveRecord 在发布 1.0 版本前可能有很多较大的变化,如演示代码无法编译,建议您参考最新版本的相关文档。