Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1014247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:13:30+00:00 2026-05-16T10:13:30+00:00

I’m wondering how to best implement a property (here, LatestRequest ) which is read-only,

  • 0

I’m wondering how to best implement a property (here, LatestRequest) which is read-only, backed by a query.

Here, I have an Export, which can be requested to happen multiple times. I’d like to have a property on the Export to get the latest ExportRequest. At the moment, I’ve got a many-to-one mapping with a formula, like this:

 <class name="Export" table="Exports">
   <id name="Id">
     <generator class="guid" />
   </id>
   <property name="Name" />
   <bag name="Requests" cascade="all,delete-orphan" inverse="true" access="field.camelcase" order-by="CreationDate desc">
     <key column="ExportId"/>
     <one-to-many class="ExportRequest"/>
   </bag>
   <many-to-one name="LatestRequest"
                class="ExportRequest"
                formula="(select top 1 ExportRequests.Id from ExportRequests order by ExportRequests.CreationDate desc)"/>
 </class>

 <class name="ExportRequest" table="ExportRequests">
   <id name="Id">
     <generator class="native" />
   </id>
   <many-to-one name="Export"
       class="Export"
       column="ExportId"
       not-null="true" />
   <property name="CreationDate" />
   <property name="Tag" />
 </class>

However, since the ExportRequests.Id for LatestRequest is fetched when the Export is fetched, this access pattern returns stale data:

var export = session.Get<Export>(exportId);

export.AddRequest(new ExportRequest(DateTime.Now, "Foo"));

Assert.AreEqual("Foo", export.LatestRequest.Tag); // fails due to stale data

session.Update(export);

So, what’s the best way to implement the LatestRequest property?

I could change the getter for LatestRequest so that it executes a Find query, which should be up to date, but I’m not sure how to make the Export aware of the session.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T10:13:31+00:00Added an answer on May 16, 2026 at 10:13 am

    There are several ways to solve this. I would probably not try to make it “automatic” this way. To enhance performance and simplicity, I would just make it a normal reference and manage it in the business logic or the entity like this:

    class Export
    {
      private IList<ExportRequest> requests;
    
      ExportRequest LatestRequest { get; private set; }
    
      public void AddRequest (ExportRequest request)
      { 
        requests.Add(request);
        LatestRequest  = request;
      }
    }
    

    So you don’t need any special mappings nor any additional queries to get the latest request. And, most importantly, the entity is consistent in memory and does not depend on the persistency.

    However you solve it, it is very important that the entity and the business logic is managing the data (in memory). It should be “persistence ignorant”. The database and the data access layer are not responsible to manage relations and logic of the entities.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.