Concurrency

Posts   
 
    
quentinjs avatar
quentinjs
User
Posts: 110
Joined: 09-Oct-2009
# Posted on: 29-Oct-2009 02:55:53   

From example:[http://adriandev.blogspot.com/2007/12/optmistic-concurrency-withtimestamps.html](http://adriandev.blogspot.com/2007/12/optmistic-concurrency-withtimestamps.html)

derived from http://davidhayden.com/blog/dave/archive/2006/10/24/ConcurrencyTimestampLLBLGenPro.aspx

I have the following but it blows up on Fields property on the CommonEntityBase.


IEntityField lastModifiedDateField = entity.Fields(entity.ConcurrencyCheckFieldName);

Can you suggest what I am missing?

using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Text.RegularExpressions;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace glossaryDB /
{
    /// <summary>
    /// 
    /// </summary>
    public partial class CommonEntityBase
    {

        protected override SD.LLBLGen.Pro.ORMSupportClasses.IConcurrencyPredicateFactory CreateConcurrencyPredicateFactory()
        {
            return new OptimisticConcurrencyFactory_ViaTimestampCheck();
        }

        public string ConcurrencyCheckFieldName
        {
            get { return _concurrencyCheckFieldName; }
            set { _concurrencyCheckFieldName = value; }
        }

        private string _concurrencyCheckFieldName = "ModifiedDate";

    }

    /// <summary>
    ///  
    /// </summary>
    public class OptimisticConcurrencyFactory_ViaTimestampCheck : IConcurrencyPredicateFactory
    {

        public IPredicateExpression CreatePredicate(ConcurrencyPredicateType predicateTypeToCreate, object containingEntity)
        {
            IPredicateExpression toReturn = null;
            CommonEntityBase entity = (CommonEntityBase)containingEntity;

            if (!string.IsNullOrEmpty(entity.ConcurrencyCheckFieldName))
            {
                toReturn = new PredicateExpression();
                IEntityField lastModifiedDateField = entity.Fields(entity.ConcurrencyCheckFieldName);
                switch (predicateTypeToCreate)
                {
                    case ConcurrencyPredicateType.Delete:
                    case ConcurrencyPredicateType.Save:
                        toReturn.Add(new FieldCompareValuePredicate(lastModifiedDateField, ComparisonOperator.Equal, lastModifiedDateField.CurrentValue));
                        break; // TODO: might not be correct. Was : Exit Select

                        break;
                }
            }

            return toReturn;
        }

    }

}

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Oct-2009 03:54:04   

quentinjs wrote:

I have the following but it blows up on Fields property on the CommonEntityBase.

IEntityField lastModifiedDateField = entity.Fields(entity.ConcurrencyCheckFieldName);

Use this (array enumeration):

IEntityField lastModifiedDateField = entity.Fields[entity.ConcurrencyCheckFieldName];

Additional comment. I think this:

toReturn.Add(new FieldCompareValuePredicate(lastModifiedDateField, ComparisonOperator.Equal, lastModifiedDateField.CurrentValue));

should be

toReturn.Add(new FieldCompareValuePredicate(lastModifiedDateField, ComparisonOperator.Equal, lastModifiedDateField.DBValue));

(quoted from docs):

To filter on the original database values fetched into the entity to be saved, you can create for example FieldCompareValuePredicate instances which use the EntityField2's DbValue property. Even though a field is changed in memory through code, the DbValue property of a field will have the original value read from the database. You can use this for optimistic concurrency schemes.

David Elizondo | LLBLGen Support Team
quentinjs avatar
quentinjs
User
Posts: 110
Joined: 09-Oct-2009
# Posted on: 29-Oct-2009 04:02:04   

IEntityField lastModifiedDateField = entity.Fields[entity.ConcurrencyCheckFieldName];

It still doesn't recognize "Fields" Where is this declared as its not surfacing? Is there a descendant class I should be using that has this property?

Thanks for the other pointers, I've made the changes. Can't test them as yet.

There is a line

            CommonEntityBase entity = (CommonEntityBase)containingEntity;

if I replace with

            EntityBase entity = (EntityBase)containingEntity;

Then I get all the properties showing, except... ConcurrencyCheckFieldName

if I use 2 entities, one for each it compiles, but seems odd that CommonEntityBase does not have the properties of EntityBase....

thoughts?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 29-Oct-2009 05:05:11   
David Elizondo | LLBLGen Support Team
quentinjs avatar
quentinjs
User
Posts: 110
Joined: 09-Oct-2009
# Posted on: 29-Oct-2009 05:10:02   

LLBL 2.6

It would not compile. the word "Field" was underlined in red. I guessing I am missing an apropriate using, or the class being casted is the wrong one as the property field is not in it.

After muddling around it seems that the classes have to physically be in the same project as the database LLBL stuff. Once moved there it was happy as a clam!

Since I am using Service, it just plugs into all the entities, I don't have to register anything. DI is very cool :-)