问题描述:

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);


        // Verify there hasn't been a concurrent change
        if(item.Version != dto.Version)
        {
            throw new StaleObjectStateException();
        }


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

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

    }
相关阅读:
Top