views:

96

answers:

2

I have built my first MVC solution and used the repository pattern for retrieving/inserting/updating my database.

I am now in the process of refactoring and I've noticed that a lot of (in fact all) the methods within my repository are hitting the database everytime. This seems overkill and what I'd ideally like is to do is 'cache' the main data object e.g. 'GetAllAdverts' from the database and to then query against this cached object for things like 'FindAdvert(id), AddAdvert(), DeleteAdvert() etc..'

I'd also need to consider updating/deleting/adding records to this cache object and the database.

What is the best apporoach for something like this?

My knowledge of this type of things is minimal and really looking for advice/guidance/tutorial to point me in the right direction.

Thanks in advance.

A: 

Heath Stewart's tutorial might help you.

Kindness,

Dan

Daniel Elliott
A: 

This just hit my radar, and I assume you have already solved the issue by now. But if not, I would look into Pre-Compiled LINQ Queries. Something like this:

        private static Func<SomeDataContext, int, PersonDto> _getPersonByIdQuery =
       CompiledQuery.Compile<SomeDataContext, int, PersonDto>(
           (dataContext, personId) =>
               dataContext.PersonDtos.where(c => c.PersonId == personId).FirstOrDefault()
               );

Put something like that inside of your datacontext, then add an internal method in there to call it call it. Your retriever/saver will then call the internal method. Does that make sense? I can post more code if necessary..

Best of luck,

Justin

Justin Williams