Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Unable to get individual properties from collection document after inheriting from Entity #56

Open
huzaynbolt opened this issue Dec 7, 2017 · 8 comments
Assignees

Comments

@huzaynbolt
Copy link

huzaynbolt commented Dec 7, 2017

The sample class I add earlier was

public class Author:Entity
    {
        public virtual string name { get; set; }
        public string dob  { get; set; }
    }

and the response on getting list of authors was

[
    {
        "id": "5a2983589157cf906c39e808"
    },
    {
        "id": "5a29899fcef3e85b9c427dd9"
    },
    {
        "id": "5a29920fc9ea2c7a38f45085"
    }
]

After adding DataMember to each property like below

public class Author:Entity
    {
        [DataMember]
        public virtual string name { get; set; }
        [DataMember]
        public string dob  { get; set; }
    }

I was able to get the name and dob as part of the response

[
    {
        "name": null,
        "id": "5a2983589157cf906c39e808"
    },
    {
        "name": "Alayo Bolt",
        "id": "5a29899fcef3e85b9c427dd9"
    },
    {
        "name": "Alayo Bolt",
        "id": "5a29920fc9ea2c7a38f45085"
    }
]

Must I add the attribute [DataMember] to every field of my POCO classes before it could map to a field in the container document?

@RobThree
Copy link
Owner

RobThree commented Dec 7, 2017

The DataMember attribute is used by, amongst others, ASP.Net, WCF and OWIN. I assume your project uses either or similar?

I can't answer your question since this is not related to MongoRepository but to some other part of your project like aforementioned ASP.Net/WCF/OWIN or such. But, generally, yes, you should add a DataMember attribute to all properties you want serialized.

@huzaynbolt
Copy link
Author

No, I am actually using a WEB API does that relate to such framework you enlisted and might it be the cause. You also added the [DataMember] attribute to the Entity class on Id property, Why was that done

@RobThree
Copy link
Owner

RobThree commented Dec 7, 2017

does that relate to such framework you enlisted

Yes

You also added the [DataMember] attribute to the Entity class on Id property, Why was that done

Because otherwise you won't be able to add it 😉

@huzaynbolt
Copy link
Author

huzaynbolt commented Dec 7, 2017

Because otherwise, you won't be able to add it

But if I am not working with the WCF,Web API ... frameworks would removing it still makes it work the
way it should

@RobThree
Copy link
Owner

RobThree commented Dec 7, 2017

But if I am not working with the WCF,Web API

Then it won't do anything.

removing it still makes it work the way it should

Why would you want to remove it? It doesn't hurt leaving it in there if that's what you mean?

Maybe you can explain a little of your actual problem; remember that as an outsider I have no clue on what you're doing and your initial issue report doesn't clarify much. I'm not psychic 😉

@huzaynbolt
Copy link
Author

Thanks Rob, I am actually using the Repository as my DAL (as it should be), Then I created Proxy classes that inherited from the base Entity class (as stated in your example). Like the Author Class I use as an example above, whenever I try Adding an instance of the object into the repository it work as it should and all filed value are mapped to the mongo documents.

The ISSUE
Whenever I try requesting the documents I added earlier via the enumerator in the MongoRepository, It only returns the Id not until I add a [DataMemebr] attribute on each field/property of the Author class

@RobThree
Copy link
Owner

RobThree commented Dec 7, 2017

The ISSUE
Whenever I try requesting the documents

... via Web API. That's pretty relevant information I think 😉 The quoted could also be read as: "Whenever I try requesting the documents ... from MongoDB using myRepo.Where(...)...". The latter is a MongoRepository support question / issue, the former is an issue related to Web API (or other framework).

But as said; Web API requires the DataMember attribute since the Entity class is annotated with the DataContract attribute:

If you prefer an "opt-in" approach, decorate the class with the DataContract attribute. If this attribute is present, members are ignored unless they have the DataMember. You can also use DataMember to serialize private members.

@nk-gears
Copy link

I resolved this challenge using CustomResolver. Please refer here for more details.
https://refactorthat.wordpress.com/2014/01/18/getting-datacontract-atttribute-and-web-api-to-work-together/

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants