Update cached entity

Mar 12, 2011 at 1:22 PM

Hello

I have a little problem with update cached entities.

Problem is caused by flowing lines:

        private static void UpdateCachedEntity(Type entityType, Guid entityId, object entity)
        {
            if (cachedEntities.Any(x => x.EntityType != entityType))
            {
                return;
            }
What for is this return.

I have multiple entities in cache (Type1, Type2, Type3) so i have always return.

Coordinator
Mar 13, 2011 at 9:08 PM

Hi Biper4x4,

 

The return is to stop the update running if there aren't any entity types in the cache, which is the correct behaviour, what is actually happening in your case?

Sean.

Mar 13, 2011 at 9:48 PM

Hello

My case:

1. Create 3 types of entities.

2. Load  them all by GetAll();

3. Get one element by GetById(...)

4.Update this element.

5. SaveChanges()

6. Get the same  entity by GetById(...)

In my case I receive item without updated value (old value from start)  - it's not correct behavior for me.

The problem for me was on this line. Cache was not updated due to return.

if (cachedEntities.Any(x => x.EntityType != entityType))
{
return;
}

Cache should be updated ?
Coordinator
Mar 13, 2011 at 10:12 PM

Hi,

I'll see if I can reproduce, you should be getting the updated entity, give me 10 mins and I'll create a similar app and see what happens.

Cheers,

Sean.

Coordinator
Mar 13, 2011 at 10:22 PM

Hi,

I've created the following app and everything seems to be ok, does this reflect something similar to what you are doing? also, are you using the latest version of the code (ie. not version 1)

[DataContract]
    public class MyEntity : IRapidEntity
    {
        [DataMember]
        public System.Guid Id { get; set; }

        [DataMember]
        public string MyValue { get; set; }
    }

    [DataContract]
    public class MyEntity2 : IRapidEntity
    {
        [DataMember]
        public System.Guid Id { get; set; }

        public string MyValue { get; set; }
    }

    [DataContract]
    public class MyEntity3 : IRapidEntity
    {
        [DataMember]
        public System.Guid Id { get; set; }

        [DataMember]
        public string MyValue { get; set; }
    }

 

public MainPage()
        {
            InitializeComponent();

            MyEntity entity1 = new MyEntity() { MyValue = "initial value" };
            MyEntity2 entity2 = new MyEntity2() { MyValue = "initial value2" };
            MyEntity3 entity3 = new MyEntity3() { MyValue = "initial value3" };

            RapidRepository<MyEntity> rep1 = new RapidRepository<MyEntity>();
            RapidRepository<MyEntity2> rep2 = new RapidRepository<MyEntity2>();
            RapidRepository<MyEntity3> rep3 = new RapidRepository<MyEntity3>();
            rep1.Add(entity1);
            rep2.Add(entity2);
            rep3.Add(entity3);
            RapidContext.CurrentContext.SaveChanges();

            var entities = rep1.GetAll();
            var entities2 = rep2.GetAll();
            var entities3 = rep3.GetAll();

            var newEntity = entities[0];
            newEntity.MyValue = "new value";

            RapidContext.CurrentContext.SaveChanges();
            var updateEntity = rep1.GetById(newEntity.Id);

            Debug.Assert(updateEntity.MyValue == "new value");


        }

Mar 13, 2011 at 10:50 PM

One missing element in test

var newEntity = entities[0];

remove

newEntity.MyValue = "new value";

add

var myEntity = new MyEntity {MyValue = "new Value", Id= newEntity.Id}; - Data Travel between ViewModel and DataModel or Service

rep1.Update(myEntity); - my object travels between data model and view model - so I'm updating a copy of MyEntity not the same reference - ID's are equal

}

I think object copy and rep.Update is a key to solve the problem

I'm using last source code form codeplex



Coordinator
Mar 13, 2011 at 11:38 PM

Hi biper,

I'll take a look tomorrow after work as I need to turn in for the night, let me know if you fix, otherwise I'll get back to you tomorrow.

 

Cheers,

 

Sean.

Coordinator
Mar 14, 2011 at 8:57 PM

Hi,

Heres my test as I currently understand, could you possibly fill in the blanks for me.

Updating the record with a different entity with the same Id will work fine as essentially update just removes the current record and adds the new one.

(The delete is commented out as I wasn't sure how this part fits in).

 

Cheers,

Sean.

public MainPage()
        {
            InitializeComponent();

            MyEntity entity1 = new MyEntity() { MyValue = "initial value" };
            MyEntity2 entity2 = new MyEntity2() { MyValue = "initial value2" };
            MyEntity3 entity3 = new MyEntity3() { MyValue = "initial value3" };

            RapidRepository<MyEntity> rep1 = new RapidRepository<MyEntity>();
            RapidRepository<MyEntity2> rep2 = new RapidRepository<MyEntity2>();
            RapidRepository<MyEntity3> rep3 = new RapidRepository<MyEntity3>();
            rep1.Add(entity1);
            rep2.Add(entity2);
            rep3.Add(entity3);
            RapidContext.CurrentContext.SaveChanges();

            var entities = rep1.GetAll();
            var entities2 = rep2.GetAll();
            var entities3 = rep3.GetAll();

            var newEntity = entities[0];
            newEntity.MyValue = "new value";

            // remove
            //rep1.Delete(newEntity);            
            //RapidContext.CurrentContext.SaveChanges();

            var myNewEntity = new MyEntity { Id = newEntity.Id, MyValue = newEntity.MyValue };
            rep1.Update(myNewEntity);

            RapidContext.CurrentContext.SaveChanges();

            var finalEntity = rep1.GetById(myNewEntity.Id);

            Debug.Assert(finalEntity.MyValue == "new value");
        }

Mar 15, 2011 at 9:44 AM

My code is exactly like test.

No delete before update.

If this test works fine for you. We have two diffrent version of source code.

 

Coordinator
Mar 15, 2011 at 8:21 PM

Hi Biper,

This is working for me on the current download V2.0 and what you're doing is fairly typical usage so it shouldn't be causing you these issues.

Can you try running the test code above to see if it works, just stick it in the MainPage method and see what happens. I'm sure that it should work, let me know either way.

After that, any further details you can give me would help.

Another possible solution to get you going...

In your application startup, you could turn off the caching altogether, it is only there for performance but the file system is pretty quick anyway.

The syntax for this is:

RapidRepository.Cache.EntityCache.NoCache<MyEntity>();
RapidRepository.Cache.EntityCache.NoCache<MyEntity2>();
RapidRepository.Cache.EntityCache.NoCache<MyEntity3>();

This should at least get you going, let me know how you get on.

Cheers,

Sean.

Mar 15, 2011 at 9:33 PM

  RapidRepositoryTests.MyEntity entity1 = new RapidRepositoryTests.MyEntity() { MyValue = "initial value", Id = Guid.NewGuid() };
            RapidRepositoryTests.MyEntity2 entity2 = new RapidRepositoryTests.MyEntity2() { MyValue = "initial value2", Id = Guid.NewGuid() };
            RapidRepositoryTests.MyEntity3 entity3 = new RapidRepositoryTests.MyEntity3() { MyValue = "initial value3", Id = Guid.NewGuid() };

            RapidRepository rep1 = new RapidRepository();
            RapidRepository rep2 = new RapidRepository();
            RapidRepository rep3 = new RapidRepository();
            rep1.Add(entity1);
            rep2.Add(entity2);
            rep3.Add(entity3);
            RapidContext.CurrentContext.SaveChanges();

            var entities = rep1.GetAll();
            var entities2 = rep2.GetAll();
            var entities3 = rep3.GetAll();

            var myNewEntity = new RapidRepositoryTests.MyEntity { Id = entity1.Id, MyValue = "new value" };
            rep1.Update(myNewEntity);

            RapidContext.CurrentContext.SaveChanges();

            var finalEntity = rep1.GetById(myNewEntity.Id);

            Debug.Assert(finalEntity.MyValue == "new value"); // here on debug i have "inital value" insted of new value

 

Remove from your test

  var newEntity = entities[0];
newEntity.MyValue = "new value";

Maybe I should record a video from debug session ?
It will be easier to find problem.
Mar 15, 2011 at 9:37 PM

I really try to be helpful.

I'm planning to use RapidRepository on my production application.

Send me private message via http://clever-software.net/home/Contact

Coordinator
Mar 15, 2011 at 10:13 PM

Hi Biper,

I normally try and keep all correspondence for this project through code plex, feel free to private message me if you want to move from this thread.

Cheers,

Sean.

Coordinator
Mar 15, 2011 at 10:44 PM

Hi Biper,

I have managed to reproduce your issue, it appears there is an issue with the caching when using multiple types.

For the time being I would suggest turning off the caching and I'll have to look into this as a bug, unless you're loading a great deal of entities, having the caching turned off will not make any noticeable difference.

Kind Regards,

Sean.

 

RapidRepository.Cache.EntityCache.NoCache<MyEntity>();
RapidRepository.Cache.EntityCache.NoCache<MyEntity2>();
RapidRepository.Cache.EntityCache.NoCache<MyEntity3>();
Mar 15, 2011 at 10:50 PM

Problem is caused by flowing lines:

        private static void UpdateCachedEntity(Type entityType, Guid entityId, object entity)
{
if (cachedEntities.Any(x => x.EntityType != entityType))
{
return;
}
I have already fixed problem. Remove if statement. Then it works fine for me.
Developer
Jan 24, 2012 at 12:42 PM
Hi Sean,

I appreciate RapidRepository a lot. For my app I have also have some custom code built upon RR to implement background saving through consumer/producer key. Unforsunately, I also came across this same issue. I found that cachedEntities has desired EntityType in the _items collection but cachedEntities.Any for the said condition in UpdateCachedEntity still returns true and thus the control returns. The condition should rather be like this:

            if (!cachedEntities.Any(x => x.EntityType == entityType))
            {
                return;
            }

Thanks

Sanjay

Coordinator
Jan 24, 2012 at 12:53 PM
Hi Sanjay,

There are a few bugs unfortunately within the code, unfortunately I'm no longer actively working on the code base, if you would like to be able to update and commit to the repository let me know and I'll add you as a dev to the project.

Thanks,

Sean.


From: [email removed]
To: [email removed]
Date: Tue, 24 Jan 2012 05:43:03 -0800
Subject: Re: Update cached entity [RapidRepository:249426]

From: shosansharma
Hi Sean,
I appreciate RapidRepository a lot. For my app I have also have some custom code built upon RR to implement background saving through consumer/producer key. Unforsunately, I also came across this same issue. I found that cachedEntities has desired EntityType in the _items collection but cachedEntities.Any for the said condition in UpdateCachedEntity still returns true and thus the control returns. The condition should rather be like this:
            if (!cachedEntities.Any(x => x.EntityType == entityType))
            {
                return;
            }
Thanks
Sanjay
Read the full discussion online.
To add a post to this discussion, reply to this email (RapidRepository@discussions.codeplex.com)
To start a new discussion for this project, email RapidRepository@discussions.codeplex.com
You are receiving this email because you subscribed to this discussion on CodePlex. You can unsubscribe or change your settings on codePlex.com.
Please note: Images and attachments will be removed from emails. Any posts to this discussion will also be available online at codeplex.com