abhi62003 / relax-net

Automatically exported from code.google.com/p/relax-net
GNU General Public License v3.0
0 stars 0 forks source link

Getting Entities back from custom views that return partial doc rows #35

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I was experimenting with Hammock, and I really like it.

One problem I found was that if I use the Query<T> class to access a custom 
view I wrote that only returns part of the document, when Executing it wouldn't 
deserialize the partial-document json into my custom Entity.

// accessing a pre-made view that drills down into the xmldoc
                    var q = new Query<Cover>(xmlsess, "cxmldocument", "getcoversbycollection");

var covers = q.All().Exactly("HCBOOK").Execute().Rows.Select(r => r.Entity); // 
returns an empty entity for each row

I wrote some code that allows this to function:

 // accessing a pre-made view that drills down into the xmldoc
                    var q = new Query<Cover>(xmlsess, "cxmldocument", "getcoversbycollection");

                    var covers = q.All().Exactly("HCBOOK").CustomViewEntities;

namespace RedBranch.Hammock
{
    /// <summary>
    /// Functions to improve Hammock usefulness
    /// Separate file for maintainability when hammock changes
    /// Luke Daffron, 11-9-2012
    /// </summary>
    /// <typeparam name="TEntity"></typeparam>
    public partial class Query<TEntity>
    {
        public partial class Result
        {
            public partial class Row
            {
                public TEntity CustomViewEntity // typed entity used in a custom view query where a row is a partial document
                {
                    get
                    {
                        return JsonConvert.DeserializeObject<TEntity>(Value.ToString());
                    }
                }
            }
        }
    }
    public partial class Query<TEntity>
    {
        public partial class Spec
        {
            public IEnumerable<TEntity> CustomViewEntities // quick enumeration of entities returned from 'partial document' views
            {
                get
                {
                    return this.Execute().Rows.Select(r => r.CustomViewEntity);
                }
            }
        }
    }
}

Original issue reported on code.google.com by thingi...@gmail.com on 9 Nov 2012 at 9:57

GoogleCodeExporter commented 9 years ago
Example of my view... map function:

function(doc) {
                        if (doc._id.indexOf('cxmldocument-') === 0) {
            for(i=0;i<doc.XDoc.presscovers.cover.length; i++)
                        {
                        emit(doc.XDoc.presscovers.cover[i].collectioncode, doc.XDoc.presscovers.cover[i]);
                        }
                      }
                    }

Sample document:

{"_id":"cxmldocument-44355d78-6fb3-4f2c-9662-7bc72a997ef3","_rev":"1-dd2234e7657
bc0be1b64b6fe133d5b5c","XDoc":{"?xml":{"@version":"1.0","@encoding":"utf-8"},"pr
esscovers":{"cover":[{"collectioncode":"HCBOOK","size":"8x10","productcode":"PB0
11","covertype":"CANVAS","covercode":"PB070","description":"PRINTED 
IMAGE-CANVAS","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK
","size":"10x18","productcode":"PB012","covertype":"CANVAS","covercode":"PB071",
"description":"PRINTED 
IMAGE-CANVAS","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK
","size":"12.5x10","productcode":"PB013","covertype":"CANVAS","covercode":"PB072
","description":"PRINTED 
IMAGE-CANVAS","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK
","size":"8x10","productcode":"PB011","covertype":"GLOSS","covercode":"PB061","d
escription":"PRINTED 
IMAGE-GLOSSY","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK
","size":"10x18","productcode":"PB012","covertype":"GLOSS","covercode":"PB062","
description":"PRINTED 
IMAGE-GLOSSY","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK
","size":"12.5x10","productcode":"PB013","covertype":"GLOSS","covercode":"PB063"
,"description":"PRINTED 
IMAGE-GLOSSY","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK
","size":"8x10","productcode":"PB011","covertype":"SATIN","covercode":"PB061","d
escription":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK"
,"size":"10x18","productcode":"PB012","covertype":"SATIN","covercode":"PB062","d
escription":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK"
,"size":"12.5x10","productcode":"PB013","covertype":"SATIN","covercode":"PB063",
"description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBOOK"
,"size":"8x10","productcode":"PB011","covertype":"METALLIC","covercode":"PB073",
"description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBO
OK","size":"10x18","productcode":"PB012","covertype":"METALLIC","covercode":"PB0
74","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"HCBO
OK","size":"12.5x10","productcode":"PB013","covertype":"METALLIC","covercode":"P
B075","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"TRUE"},{"collectioncode":"ACCO
RDIONMINIS","size":"3x3","productcode":"MS014","covertype":"SATIN","covercode":"
PR154","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACCORD
IONMINIS","size":"3x3","productcode":"MS014","covertype":"METALLIC","covercode":
"PR155","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACC
ORDIONMINIS","size":"3x3","productcode":"MS121","covertype":"SATIN","covercode":
"PR220","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACCORD
IONMINIS","size":"3x3","productcode":"MS121","covertype":"METALLIC","covercode":
"PR221","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACC
ORDIONMINIS","size":"2.5x3.5","productcode":"MS015","covertype":"SATIN","coverco
de":"PR156","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACCORD
IONMINIS","size":"2.5x3.5","productcode":"MS015","covertype":"METALLIC","coverco
de":"PR157","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACC
ORDIONMINIS","size":"2.5x3.5","productcode":"MS120","covertype":"SATIN","coverco
de":"PR222","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACCORD
IONMINIS","size":"2.5x3.5","productcode":"MS120","covertype":"METALLIC","coverco
de":"PR223","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACC
ORDIONBOOKS","size":"4x8","productcode":"CD063","covertype":"SATIN","covercode":
"PR158","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACCORD
IONBOOKS","size":"4x8","productcode":"CD063","covertype":"METALLIC","covercode":
"PR159","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ACC
ORDIONBOOKS","size":"4x8","productcode":"CD063","covertype":"SATIN","covercode":
"PR170","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE","fullproductcontains":"FLA
P"},{"collectioncode":"ACCORDIONBOOKS","size":"4x8","productcode":"CD063","cover
type":"METALLIC","covercode":"PR171","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE","fullproductcontains":"
FLAP"},{"collectioncode":"ULTIMATEALBUM","size":"10x10","productcode":"UA009","c
overtype":"GL","covercode":"GL009","description":"PRINTED 
IMAGE-GLOSSY","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ULTIM
ATEALBUM","size":"10x10","productcode":"UA009","covertype":"ST","covercode":"ST0
09","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ULTIMA
TEALBUM","size":"10x10","productcode":"UA009","covertype":"MT","covercode":"MT00
9","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ULT
IMATEALBUM","size":"10x10","productcode":"UA009","covertype":"CV","covercode":"C
V009","description":"PRINTED 
IMAGE-CANVAS","prefixorderleveloptionwithsize":"FALSE"},{"collectioncode":"ULTIM
ATEALBUM","size":"10x10","productcode":"UA009","covertype":"GL","covercode":"GL0
09","description":"PRINTED 
IMAGE-GLOSSY","prefixorderleveloptionwithsize":"FALSE","fullproductcontains":"TH
ICK"},{"collectioncode":"ULTIMATEALBUM","size":"10x10","productcode":"UA009","co
vertype":"ST","covercode":"ST009","description":"PRINTED 
IMAGE-SATIN","prefixorderleveloptionwithsize":"FALSE","fullproductcontains":"THI
CK"},{"collectioncode":"ULTIMATEALBUM","size":"10x10","productcode":"UA009","cov
ertype":"MT","covercode":"MT009","description":"PRINTED 
IMAGE-METALLIC","prefixorderleveloptionwithsize":"FALSE","fullproductcontains":"
THICK"},{"collectioncode":"ULTIMATEALBUM","size":"10x10","productcode":"UA009","
covertype":"CV","covercode":"CV009","description":"PRINTED 
IMAGE-CANVAS","prefixorderleveloptionwithsize":"FALSE","fullproductcontains":"TH
ICK"}]}}}

Original comment by thingi...@gmail.com on 9 Nov 2012 at 10:00