NHibernate Paging

Suppose I have a domain model with a Blog class that has a BlogEntries property (which contains objects of type BlogEntry). If I have a database model with two tables "Blog" and "BlogEntry", it is possible that I have 1000 blog entries for the blog. If I were to show a blog on a website, I would like to show only 20 blog entries at a time, so I would have to use some kind of paging. I obviously do not want 1000 records to be extracted from the database all the time.

How would I do that? Should the BlogEntries property even be in the Blog domain object or possibly in the repository? Nevertheless, I would like to be able to add blog entries, as well as get paginated results from existing ones. What does NHibernate look like?

Blogging / BlogEntry is just an example; it could be an example of a customer / order or any other master / detail script.

Please enlighten me!

0
source share
3 answers

BlogEntry , . BlogEntry , BlogEntry , BlogID. , , ( , , ..). finder .

public class Blog
{
    public int ID {get;set;}
    // other stuff
}

public class BlogEntry
{
    public int ID {get;set;}
    public int BlogID {get;set;}
}

public class BlogEntryRepository
{
    public IEnumerable<BlogEntry> FindByBlogID(
        int blogID, int pageIndex, int pageSize) 
    {
        // implementation
    }
}

( BlogEntry, ) BlogEntryID Blog. , BlogEntry Blog, , Blog. BlogEntry, . , .

public class Blog
{
    public int ID {get;set;}
    public IEnumerable<int> BlogEntryIDs {get;set;}
    // other stuff
}

public class BlogEntry
{
    public int ID {get;set;}
    public int BlogID {get;set;}
}

public class BlogEntryRepository
{
    public IEnumerable<BlogEntry> Get(IEnumerable<int> blogEntryIDs) 
    {
        // implementation
    }
}

// get the second page
var entries = 
  blogEntryRepo.Get(blog.BlogEntryIDs).Skip(1 * PAGE_SIZE).Take(PAGE_SIZE);

, . ( , . SQL 2005 ROW_VERSION .)

, (, ) BlogEntry, . int .

+1

blogentry , .

: downvote? ?

+1

You can use BatchSize to collect blog entries . This will allow you not to select the entire collection from the database, but only the required values.

-1
source

Source: https://habr.com/ru/post/1625070/


All Articles