0

我需要一些帮助来理解这个问题。我正在使用ActiveRecordMediator存储库 模式。我已启用会话范围http 模块,用ActiveRecord标记我的类(Lazy = true)。Castle ActiveRecord懒惰加载不工作

问题是,我每次执行FindAll或SlicedFindAll, 调解器都会返回一组初始化元素而不是 代理。有人能指出我在正确的方向吗?

这是我的仓库:

public interface IEntityRepository<TEntity> 
{ 
    IList<TEntity> FindAll(int page, int pageSize, out int resultCount); 
} 

public class EntityRepository<TEntity> : IEntityRepository<TEntity> 
{ 
    public virtual IList<TEntity> FindAll(int page, int pageSize) 
    { 
     return (IList<TEntity>)ActiveRecordMediator.SlicedFindAll(typeof(TEntity), (page * pageSize), pageSize); 
    } 
} 

[ActiveRecord(Lazy = true)] 
public class DocumentEntity 
{ 
    private Guid _id; 
    private IList<DocumentVersionEntity> _versions; 

    [PrimaryKey(PrimaryKeyType.GuidComb, "Id")] 
    public virtual Guid Id 
    { 
     get { return _id; } 
     set { _id = value; } 
    } 

    [HasAndBelongsToMany(typeof(DocumentVersionEntity), RelationType.Bag, Table = "DocumentEntriesToDocumentVersions", ColumnKey = "DocumentEntryId", ColumnRef = "DocumentVersionId", Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, Lazy = true)] 
    public virtual IList<DocumentVersionEntity> Versions 
    { 
     get { return _versions; } 
     set { _versions = value; } 
    } 
} 

[ActiveRecord(Lazy = true)] 
public class DocumentVersionEntity 
{ 
    private Guid _id; 

    [PrimaryKey(PrimaryKeyType.GuidComb, "Id")] 
    public virtual Guid Id 
    { 
     get { return _id; } 
     set { _id = value; } 
     } 
    } 
} 

当我执行的FindAll方法,在版本中的所有对象 阵列的DocumentEntity是DocumentVersionEntity代替 DocumentVersionEntityProxy和都intialized。

我在做什么错?

+0

交叉发布(附答案):http://groups.google.com/group/castle-project-users/browse_thread/thread/863f02fbc6ba52e8 – 2010-12-15 20:24:31

+1

是的,我想我也可以在城堡项目组中提问。尽管如此,我的情况仍然没有回答。 – 2010-12-15 22:24:00

回答

0

在下面的代码你有你的级联行为设置为ManyRelationCascadeEnum.AllDeleteOrphan:

[HasAndBelongsToMany(typeof(DocumentVersionEntity), 
        RelationType.Bag, 
        Table = "DocumentEntriesToDocumentVersions", 
        ColumnKey = "DocumentEntryId", 
        ColumnRef = "DocumentVersionId", 
        Cascade = ManyRelationCascadeEnum.AllDeleteOrphan, 
        Lazy = true)] 
    public virtual IList<DocumentVersionEntity> Versions 
    { 
     get { return _versions; } 
     set { _versions = value; } 
    } 

这迫使NHibernate的,是好还是坏,加载整个集合为了做孤儿清理。你必须摆脱级联行为才能工作。 NotFoundBehavior被设置为忽略也是如此,[阅读] [1]:http://blog.agilejedi.com/2010/12/nhibernateactiverecord-lazy-loading.html [1]。

+0

那么,我试过你的解决方案。似乎是有道理的..但设置完所有并删除级联行为后,整个实体仍在加载。此外,这只发生在像findAll(),SlicedFindAll()等查找方法,当我做一个Get()它懒加载正确。 – 2010-12-22 22:01:45

+0

如果使用得到它然后休眠工作正常。这是一个网络应用程序?如果是这样,您需要确保ActiveRecord的isweb配置设置设置为true,并且您应该使用每个请求的会话:http://using.castleproject.org/display/AR/Enable+Session+per+Request – 2010-12-23 12:33:19