473,396 Members | 1,929 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Cache in Dlinq

I need to repeatedly execute same queries which returns single entity by id,
like:

Customer cust = (from c in db.Customers
where c.CustomerID=="AIRBU"
select c).SingleOrDefault();

DLinq holds tracked object list internally so customer "AIRBU" exists in
memory.

How to force DLinq to look its list and not to make round trip to server ?
I looked into Entity Framework also but havent found any caching solution in
EF.

Andrus.
Jun 27 '08 #1
19 1503
"LINQ in Action" states that Single() is an exception, in that it
checks the cache first, not the database. I haven't tried it yet (long
day...) - but worth a try ;-p

(I'm assuming that SingleOrDefault doesn't qualify for this exception
since you've tried it... - and it would presumably need the predicate
to be based solely on the primary key column[s])

Marc
Jun 27 '08 #2
OK; I couldn't find a single case when Single() did anything other
than hit the database... I wonder what the authors had in mind?

Anyways... here is something truly, truly hacky that does the job; I'm
off to lather...
using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using WindowsFormsApplication4; // ns to DataClasses1DataContext =
Northwind

static class DataContextExt {
public static T GetFromCache<T>(this Table<Ttable, params
object[] keyValues) where T : class
{
if (table == null) throw new ArgumentNullException("table");
return GetFromCache<T>(table.Context, keyValues);
}
public static T GetFromCache<T>(this DataContext context, params
object[] keyValues) where T : class
{
// surfactants on standby - this is dirty, plain and simple
// (oh, and brittle, and not compile-time safe, and requires
// sufficient trust... you get the idea; nasty nasty nasty)
if (context == null) throw new
ArgumentNullException("context");
const BindingFlags FLAGS = BindingFlags.Instance |
BindingFlags.Public | BindingFlags.NonPublic;
object services = context.GetType().GetProperty("Services",
FLAGS).GetValue(context, null);

object[] args = { context.Mapping.GetMetaType(typeof(T)),
keyValues };
Type[] paramTypes = { typeof(MetaType), typeof(object[]) };
return (T)services.GetType().GetMethod("GetCachedObject",
FLAGS, null, paramTypes, null).Invoke(services, args);
}
}
static class Program
{
static void Main()
{
using (DataClasses1DataContext ctx = new
DataClasses1DataContext())
{
// cache the original
Supplier sup = ctx.Suppliers.First();
int id = sup.SupplierID;

// get from the context
Supplier same = ctx.GetFromCache<Supplier>(id);
Trace.WriteLine(ReferenceEquals(sup, same), "Got from
Context");

// get from the table
Supplier again = ctx.Suppliers.GetFromCache(id);
Trace.WriteLine(ReferenceEquals(sup, again), "Got from
Table");

// and check what happens if it isn't there...
Supplier nothing = ctx.GetFromCache<Supplier>(53);
Trace.WriteLine(ReferenceEquals(nothing, null), "Null when
missing");
}

}
}
Jun 27 '08 #3
Marc,
OK; I couldn't find a single case when Single() did anything other
than hit the database... I wonder what the authors had in mind?
MSDN clearly states that every simple query must return result form identity
cache if it exists, not reaching to database.

Do you have any idea why it is not working ?
How to use identity cache in documented way ?
Anyways... here is something truly, truly hacky that does the job; I'm
off to lather...
Where did you found information which allows to create such program ?
I havent found any documentation on Services property and GetCachedObject
method.

Andrus.
Jun 27 '08 #4
MSDN clearly states that every simple query must return result form identity
cache if it exists, not reaching to database.
Can you cite a source there?
Do you have any idea why it is not working ?
How to use identity cache in documented way ?
I have asked that very question on a LINQ group; no answer yet...
Where did you found information which allows to create such program ?
From grubbling around... it is dirty and brittle (i.e. it will break if
somebody [quite reasonably] refactors the internal implementation)...

Marc
Jun 27 '08 #5
MSDN clearly states that every simple query must return result form identity
cache if it exists, not reaching to database.
(in particular - yes it should return the same instance from the cache
[and it does] - the question is focused around whether it hits the db...
can you cite a page for the above claim?)
Jun 27 '08 #6
>MSDN clearly states that every simple query must return result form
>identity cache if it exists, not reaching to database.

(in particular - yes it should return the same instance from the cache
[and it does] - the question is focused around whether it hits the db...
can you cite a page for the above claim?)
http://msdn2.microsoft.com/en-us/library/bb399376.aspx

Quote:
If the object requested by the query is easily identifiable as one already
retrieved, no query is executed. The identity table acts as a cache of all
previously retrieved objects.

Andrus.
Jun 27 '08 #7
So I guess it comes down to defining "easily identifiable"... I'll run
some more tests at some point (not "right now"), but I don't know how
easy / hard it will be to get working... which is why I am trying to get
some input from the LINQ people...

Marc
Jun 27 '08 #8
Marc,
So I guess it comes down to defining "easily identifiable"... I'll run
some more tests at some point (not "right now"), but I don't know how easy
/ hard it will be to get working... which is why I am trying to get some
input from the LINQ people...
id == constant expression if definitely "easily identifiable".
So it seems that we have found serious bug in Linq-SQL.
Can we submit product feedback to Microsoft?
Is it reasonable to subclass DataContext to fix this ?

There is also bug in "Linq in Action" book if it states that cache works
only with Single(). Ca we inform author about this ?

Andrus.
Jun 27 '08 #9
Can we submit product feedback to Microsoft?
Feel free ["connect", or the managed LINQ forum]; personally I'd like
to do a little more looking first...
Is it reasonable to subclass DataContext to fix this ?
The cache is internal, not protected. Subclassing won't help.
Ca we inform author about this ?
Authors plural; but I guess I can try to find some e-mail addresses
and see what they say...

Marc
Jun 27 '08 #10
For info, looked at the EF offering... unfortunately it is harder to
profile, and I don't have my SQL profiler handy; following gets the
right objects at least - but I don't know (until I get a few seconds
to switch servers) how many times it hits the database:

using System;
using System.Data;
using System.Data.Objects.DataClasses;
using System.Linq;
using AdventureWorksModel;

static class Program
{
[STAThread]
static void Main()
{
AdventureWorksEntities1 ctx = new AdventureWorksEntities1();

string setName = GetEntitySetName<Customer>(false),
qualifiedSetName = GetEntitySetName<Customer>(true),
keyName = GetKeyName<Customer>();

// expect DB hit here
var cust = ctx.Customer.First();
var id = cust.CustomerID;

// look by known key
EntityKey key = ctx.GetEntityKey(setName, cust);
var byKey = ctx.GetObjectByKey(key);
object obj;
if (ctx.TryGetObjectByKey(key, out obj))
{
var cust1 = (Customer)obj;
Console.WriteLine("Got...");
}

// look by custom key and a new ctx (assume a single primary
key for keyName)
AdventureWorksEntities1 ctx2 = new AdventureWorksEntities1();
EntityKey customKey = new EntityKey(qualifiedSetName, keyName,
id);
// (different instance, but it works...; expect another DB hit
here)
var cust2 = ctx2.GetObjectByKey(customKey);

// look using predicates
var cust3 = ctx.Customer.Where(x =x.CustomerID ==
id).First();
var cust4 = ctx.Customer.First(x =x.CustomerID == id);
}

static string GetEntitySetName<T>(bool qualified) where T :
EntityObject
{
EdmEntityTypeAttribute attrib =
(EdmEntityTypeAttribute)Attribute.GetCustomAttribu te(typeof(T),
typeof(EdmEntityTypeAttribute));
if (attrib == null) return null;
return qualified ? (attrib.NamespaceName + "." +
attrib.Name) : attrib.Name;
}
static string GetKeyName<T>() where T : EntityObject
{
return ( from prop in typeof(T).GetProperties()
let attrib = (EdmScalarPropertyAttribute)
Attribute.GetCustomAttribute(prop,
typeof(EdmScalarPropertyAttribute))
where attrib != null && attrib.EntityKeyProperty
select prop.Name).Single();
}
}
Jun 27 '08 #11
Marc,
For info, looked at the EF offering... unfortunately it is harder to
profile, and I don't have my SQL profiler handy; following gets the
right objects at least - but I don't know (until I get a few seconds
to switch servers) how many times it hits the database:
Thank you. Have you got any idea why Linq-SQL does not use cache ?

Andrus.
Jun 27 '08 #12
No; I've been invited to log a "connect" bug about it - I just want to
make sure I can't get it to work first...

Marc
Jun 27 '08 #13

Marc,
Logged:
https://connect.microsoft.com/Visual...dbackID=340036
Thank you.
I marked it as very important.

Andrus.
Jun 27 '08 #15

Marc,
Logged:
https://connect.microsoft.com/Visual...dbackID=340036
Thank you.
I marked it as very important.

Andrus.

Jun 27 '08 #16
On Apr 23, 4:38*pm, Marc Gravell <marc.grav...@gmail.comwrote:
Can we submit product feedback to Microsoft?

Feel free ["connect", or the managed LINQ forum]; personally I'd like
to do a little more looking first...
Is it reasonable to subclass DataContext to fix this ?

The cache is internal, not protected. Subclassing won't help.
Ca we inform author about this ?

Authors plural; but I guess I can try to find some e-mail addresses
and see what they say...

Marc
Andrus, thanks for letting us know about this issue. I suspect the
confusion stems from a change between the betas and the final RTM of
LINQ to SQL. I'm trying to get confirmation from the product teams and
will let you know if we find otherwise. If you find other issues with
the LINQ in Action book, feel free to let us know in our forum at
http://www.manning-sandbox.com/forum.jspa?forumID=302. Also, we do
have an errata list at http://linqinaction.net/blogs/main/p...on-errata.aspx
which we will update once we receive confirmation on this issue.

Jim Wooley
www.ThinqLinq.Com
www.LinqInAction.net
Jun 27 '08 #17
Now that is service with a smile! Anything you can find out would be
appreciated...

Loving the book, btw.

Marc
Jun 27 '08 #18
Marc,
Logged:
https://connect.microsoft.com/Visual...dbackID=340036
Microsoft has marked this issue as Closed (Fixed) without providing any
information.

What do you think about this behaviour ?

Do you have any more information about this issue ?

Andrus.

Jun 27 '08 #19
Do you have any more information about this issue ?

My response is already on connect (from ages ago). I know nothing more.
Jun 27 '08 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Lei Jiang | last post by:
1) Is it support other database other than SQL Server, such as Oracle, Sybase? 2) How about the performance? Does it relay on reflections to bind the value from databse to Entity object? ...
7
by: Senna | last post by:
Hi Have a question about DLinq. The example code floating around looks like this: Northwind db = new Northwind(@"C:\...\northwnd.mdf"); var custs = from c in db.Customers where c.City ==...
3
by: Chiranjib | last post by:
I have the following queries/Observations about DLINQ. 1. I could not find any direct way of handling many to many relations. Suppose if User and Role are related by a join table UserRole then I...
0
by: Scott Nonnenberg [MSFT] | last post by:
This is our first official DLinq chat. We're still early in the planning and development stage for this very cool technology, so we can react to your feedback much more easily. Show up and tell us...
0
by: Scott Nonnenberg [MSFT] | last post by:
The DLinq team will be ready and waiting for your questions and comments at this date and time at this location: http://msdn.microsoft.com/chats/chatroom.aspx. This is otherwise known as a chat -...
0
by: Scott Nonnenberg [MSFT] | last post by:
Show up and talk to members of the DLinq team. What's DLinq, you ask? Well, to understand that you'll need to know what LINQ is - you can start with the blurb below, read more about it here:...
4
by: Brett Romero | last post by:
I've downloaded the DLINQ samples from Microsoft and have always been able to compile these in VS.NET 2005 Pro. I have a new project that I added DLINQ references to and put in a simlpe query. It...
9
by: Marc Gravell | last post by:
How to fix ? Write it the way that you know works... (i.e. the one you commented out), or write that parses the input string doing a Split on '.', and uses reflection to navigate the child...
1
by: Andrus | last post by:
I need to create Dlinq entity property value caches for every entity and every property so that all entity caches can cleared if entity of this type is changed. Entity class method uses this as:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.