开发者

Castle ActiveRecord Lazy-Loading NOT Working

I need some help in understanding this issue. I'm using the repository pattern with ActiveRecordMediator. I've enabled the session scope http module, marked my classes with the ActiveRecord(Lazy = true).

The problem is that each time I perform a FindAll or SlicedFindAll, the mediator returns a collection of initialized elements instead of proxies. Could someone point me out in the right direction?

This is my repository:

public i开发者_StackOverflow社区nterface 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; }
        }
    }
}

When I execute the FindAll method, all the objects in the Versions array of the DocumentEntity are DocumentVersionEntity instead of DocumentVersionEntityProxy and are all intialized.

What am I doing wrong?


In the following code you have your cascade behavior set to 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; }
    }

This forces Nhibernate, for better or worse, to load the entire collection in order to do orphan cleanup. You'll have to get rid of the cascade behavior for it to work. This is also true of the NotFoundBehavior being set to ignore, [read here][1]: http://blog.agilejedi.com/2010/12/nhibernateactiverecord-lazy-loading.html [1].

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜