Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Add override points for Query transformation and processing per object
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Robert Stam  
View profile  
 More options Oct 30 2012, 12:40 pm
From: Robert Stam <rob...@10gen.com>
Date: Tue, 30 Oct 2012 12:40:24 -0400
Local: Tues, Oct 30 2012 12:40 pm
Subject: Re: [mongodb-csharp] Add override points for Query transformation and processing per object

You can do this already using a custom serializer. Here's a program
illustrating how you might do it:

http://pastie.org/5138335

I've created  a StringTable class that maps strings to ids and back, and a
StringIdSerializer that serializes a string by looking up the id and
serializing the id, and deserializes the id and looks up the corresponding
string. Since you might want to have multiple string tables, you can
provide the string table as an argument to the StringIdSerializer.

This does mean that you can't completely initialize the class using
attributes alone, but you can use Automap to do most of the work and then
just set the serializer for you string property.

The LINQ implementation knows all about custom serializers, so by doing it
this way your LINQ queries will be correctly transformed to MongoDB queries
using the string ids. See the sample program.

Let me know if you have any questions.

Robert

p.s. Note: the StringTable class in my sample program is not thread safe.
You would also need some way to load it with a predictable set of ids.

On Tue, Oct 30, 2012 at 12:03 PM, Ted Elliott <elliott....@gmail.com> wrote:
> Would it be possible to add some override points in the driver to do Linq
> query transformation as well as some processing done on an object after its
> deserialized but before its returned from the enumerator?  Here is what we
> are trying to accomplish, we have some fields where we've "compressed" some
> strings to save space in the database.  Basically we've taken a string,
> stuck it in a lookup table to get back an int Id and the int gets stored in
> the database.  We would like to put that as close to the database as
> possible to make it more automatic and not require the developer to think
> about it too much.  The object is modeled like this:

> class Foo {
>     [BsonIgnore]
>     [MyCompressedField("BarId")]
>     public string BarString { get; set; }

>     [BsonElement("B")]
>     public int BarId { get; set; }
> }

> So we want to do a few things here:
> 1. Rewrite Linq queries so that when someone does FooCollection.Where(f =>
> f.BarString == "bar"), that is translated to FooCollection.Where(f =>
> f.BarId == 123) which results in the mongo query: "{ B : 123 }". where 123
> is the key for the string "bar".
> 2. As the object comes out of the database repopulate our string field:
>    f => { f.BarString = stringLookup.GetString(f.BarId) }
> 3. Inserts/Updates need to do the reverse:
>    f=> { f.BarId = stringLookup.GetStringId(f.BarString) }

> We have 1 and 2 working, but we've had to wrap the MongoQueryProvider and
> MongoQueryable in odd ways.  The BeginInit/EndInit methods don't really
> work for this scenario as we require a reference to external object to do
> the conversion, and it is not a singleton.

> So could something like this be added so that we have some places to
> register some callbacks to occur?  I would be willing to write the code if
> I could get some pointers as to where the best places to put these would
> be.  I think Item #1 seems like it would go on MongoCollection.

> Thanks,
> Ted


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.