简体   繁体   中英

NHibernate Optimistic Concurrency

I'm investigating optimistic concurrency in NHibernate. I have a scenario that is very similar to what is being described here:

http://weblogs.asp.net/stefansedich/archive/2008/10/01/set-the-value-of-a-version-column-in-nhibernate-manually.aspx

Would you recommend going with the proposed solution in this blog post?

Thanks

The blog suggests using an interceptor to re-load the current version number from the database in order to perform a manual version check with the version passed in through the entity from a DTO object. This would certainly work, but as described in the article, it adds an extra DB hit to load the current version number.

The better solution, which seems pretty obvious since it's actually what's described in the documentation for "Application version checking" as described and quoted in that blog entry. That is, perform the version check on the initially loaded entity using the DTO's version. More specifically, using the code from the article (changes to the article's code are bold ):

public void Update(MyDTO dto) {

        // Select the item.
        var item = this.repository.SelectById(dto.Id);



        // Map values from DTO to model.
        item.Name = dto.Name;
        

        // Call update
        this.repository.Update(item);   

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM