{"id":160,"date":"2013-07-17T11:19:52","date_gmt":"2013-07-17T07:19:52","guid":{"rendered":"http:\/\/infobs.athn.ru\/?p=160"},"modified":"2013-07-17T11:19:52","modified_gmt":"2013-07-17T07:19:52","slug":"c-mongodb-polymorphic-classes","status":"publish","type":"post","link":"https:\/\/infobase.athn.ru\/program\/2013\/07\/c-mongodb-polymorphic-classes\/","title":{"rendered":"C#-MongoDB Polymorphic Classes"},"content":{"rendered":"<p>\u041f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0430\u0434\u0430\u0447\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0441 \u0440\u0430\u0437\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0432 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438.<\/p>\n<p>\u0412\u043e\u043f\u0440\u043e\u0441 \u043d\u0430 \u0421\u041e<br \/>\nhttp:\/\/stackoverflow.com\/questions\/13956413\/mongodb-embedded-polymorphic-objects<\/p>\n<p>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<br \/>\nhttp:\/\/docs.mongodb.org\/ecosystem\/tutorial\/serialize-documents-with-the-csharp-driver\/#polymorphic-classes-and-discriminators<\/p>\n<p>\u041c\u043e\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b.<br \/>\n<!--more--><br \/>\n\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u044f \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 \u043a \u0431\u0434<\/p>\n<pre>\nclass Program\n    {\n        static void Main(string[] args)\n        {\n            var mongoClient = new MongoClient(\"mongodb:\/\/localhost\");\n            var mongoServer = mongoClient.GetServer();\n\n            var collection = mongoServer.GetDatabase(\"test\").GetCollection(\"animals\");\n            collection.Drop(); \/\/\u0434\u043b\u044f \u0447\u0438\u0441\u0442\u043e\u0442\u044b \u044d\u043a\u0441\u043f\u0435\u0440\u0438\u043c\u0435\u043d\u0442\u0430\n            \n            \/\/\u0417\u0434\u0435\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e\n            \n            \/\/\u0417\u0434\u0435\u0441\u044c \u0441\u0434\u0435\u043b\u0430\u0435\u043c \u0432\u044b\u0431\u043e\u0440\u043a\u0438\n         }\n      }\n<\/pre>\n<p>\u041a\u043b\u0430\u0441\u0441\u044b. \u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043e\u0431 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u0430\u0445 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0432 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438. \u041a\u0441\u0442\u0430\u0442\u0438, \u0443 \u043c\u0435\u043d\u044f \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0434\u0430\u0442\u044c \u043f\u043e\u043b\u044f\u043c \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u044f, \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u044e\u0449\u0438\u0435 \u0441 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435\u043c \u043a\u043b\u0430\u0441\u0441\u0430.<br \/>\n\u0412 \u043f\u0440\u0438\u043d\u0446\u0438\u043f\u0435, \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0440\u0430\u0437\u043d\u044b\u0435 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u043c\u043e\u0436\u043d\u043e \u0438 \u0431\u0435\u0437 \u0432\u0441\u044f\u043a\u0438\u0445 \u0430\u0442\u0440\u0438\u0431\u0443\u0442\u043e\u0432. \u041e\u043d\u0438 \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u044f\u0442\u0441\u044f, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u0438\u0442\u044c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e \u043a \u0447\u0442\u0435\u043d\u0438\u044e \u0438\u0437 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u044b.<\/p>\n<pre>\n    [BsonDiscriminator(RootClass = true)]\n    [BsonKnownTypes(typeof(Cat), typeof(Dog))]\n    public class Animal\n    {\n        public ObjectId Id { get; set; }\n        public String _Animal { get; set; }\n    }\n\n    [BsonKnownTypes(typeof(Lion), typeof(Tiger))]\n    public class Cat : Animal\n    {\n        public String _Cat { get; set; }\n    }\n\n    public class Dog : Animal\n    {\n        public String _Dog { get; set; }\n    }\n\n    public class Lion : Cat\n    {\n        public String _Lion { get; set; }\n    }\n\n    public class Tiger : Cat\n    {\n        public String _Tiger { get; set; }\n    }\n<\/pre>\n<p>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e<\/p>\n<pre>\n            collection.Insert(new Cat\n            {\n                _Animal = \"meow\",\n                _Cat = \"murka\"\n            });\n            collection.Insert(new Cat\n            {\n                _Animal = \"murr\",\n                _Cat = \"zoyka\"\n            });\n            collection.Insert(new Animal\n            {\n                _Animal = \"omnomnom\",\n            });\n            collection.Insert(new Tiger\n            {\n                _Animal = \"rrr\",\n                _Cat = \"kitty\",\n                _Tiger = \"poloskin\"\n            });\n            collection.Insert(new Dog\n            {\n                _Animal = \"bobik\",\n                _Dog = \"gav gav\"\n            });\n            collection.Insert(new Lion\n            {\n                _Animal = \"grr\",\n                _Cat = \"big\",\n                _Lion = \"king\"\n            });\n<\/pre>\n<p>\u0412\u044b\u0431\u0440\u0430\u0442\u044c \u0432\u0441\u0435\u0445 \u0436\u0438\u0432\u043e\u0442\u043d\u044b\u0445<\/p>\n<pre>\n            var p = collection.AsQueryable().ToArray();\n<\/pre>\n<p>\u0418\u043b\u0438 \u0432\u0441\u0435\u0445 \u043a\u043e\u0448\u0435\u043a (\u0431\u0435\u0437 \u043a\u043e\u0448\u0430\u0447\u044c\u0438\u0445 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e\u0441\u0442\u0435\u0439)<\/p>\n<pre>\n            var query = Query.EQ(\"_t\", \"Cat\");\n            var cats = collection.FindAs(query).ToArray();\n<\/pre>\n<p>\u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f \u0438\u043c\u0435\u0435\u0442 \u0432\u0438\u0434<\/p>\n<pre>\n{\n        \"_id\" : ObjectId(\"51ee525dfafee225483379cd\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Cat\"\n        ],\n        \"_Animal\" : \"meow\",\n        \"_Cat\" : \"murka\"\n}\n{\n        \"_id\" : ObjectId(\"51ee525dfafee225483379ce\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Cat\"\n        ],\n        \"_Animal\" : \"murr\",\n        \"_Cat\" : \"zoyka\"\n}\n{\n        \"_id\" : ObjectId(\"51ee525dfafee225483379cf\"),\n        \"_t\" : \"Animal\",\n        \"_Animal\" : \"omnomnom\"\n}\n{\n        \"_id\" : ObjectId(\"51ee525dfafee225483379d0\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Cat\",\n                \"Tiger\"\n        ],\n        \"_Animal\" : \"rrr\",\n        \"_Cat\" : \"kitty\",\n        \"_Tiger\" : \"poloskin\"\n}\n{\n        \"_id\" : ObjectId(\"51ee525dfafee225483379d1\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Dog\"\n        ],\n        \"_Animal\" : \"bobik\",\n        \"_Dog\" : \"gav gav\"\n}\n{\n        \"_id\" : ObjectId(\"51ee525dfafee225483379d2\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Cat\",\n                \"Lion\"\n        ],\n        \"_Animal\" : \"grr\",\n        \"_Cat\" : \"big\",\n        \"_Lion\" : \"king\"\n}\n<\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0432\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c\u0438 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438.<\/strong><\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043a\u043e\u0448\u043a\u0430\u043c \u0438 \u0441\u043e\u0431\u0430\u043a\u0430\u043c \u0433\u043b\u0430\u0437\u0430.<\/p>\n<pre>\n    public class Eye\n    {\n        public String Color { get; set; }\n    }\n\n    public class CatEye : Eye\n    {\n        public String Size { get; set; }\n    }\n\n    public class DogEye : Eye\n    {\n        public String Shape { get; set; }\n    }\n<\/pre>\n<p>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f  \u0432 \u043a\u043b\u0430\u0441\u0441\u0430\u0445 \u043a\u043e\u0448\u043a\u0438 \u0438 \u0441\u043e\u0431\u0430\u043a\u0438.<\/p>\n<pre>\n    public class Cat : Animal\n    {\n        public String _Cat { get; set; }\n        public List Eyes { get; set; }\n    }\n\n    public class Dog : Animal\n    {\n        public String _Dog { get; set; }\n        public List Eyes { get; set; }\n    }\n<\/pre>\n<p>\u0418\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u043e\u0432 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e.<\/p>\n<pre>\n            collection.Insert(new Cat\n            {\n                _Animal = \"meow\",\n                _Cat = \"murka\",\n                Eyes = new List()\n                {\n                    new CatEye(){Color=\"green\", Size=\"large\"},\n                    new CatEye(){Color=\"blue\", Size=\"middle\"}\n                }\n            });\n            collection.Insert(new Cat\n            {\n                _Animal = \"murr\",\n                _Cat = \"zoyka\",\n                Eyes = new List()\n                {\n                    new CatEye(){Color=\"red\", Size=\"small\"},\n                    new CatEye(){Color=\"pink\", Size=\"middle\"}\n                }\n            });\n            collection.Insert(new Dog\n            {\n                _Animal = \"bobik\",\n                _Dog = \"gav gav\",\n                Eyes = new List()\n                {\n                    new DogEye(){Color=\"red\", Shape=\"round\"},\n                    new DogEye(){Color=\"pink\", Shape=\"square\"}\n                }\n            });\n<\/pre>\n<p>\u0412\u044b\u0431\u043e\u0440\u043a\u0430 (\u043a\u0430\u043a \u0438 \u0432 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u043c \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u0435)<\/p>\n<pre>\n            var query = Query.EQ(\"_t\", \"Cat\");\n            var cats = collection.FindAs(query).ToArray();\n<\/pre>\n<p>\u041a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f<\/p>\n<pre>\n{\n        \"_id\" : ObjectId(\"51ee622cfafee21464fd1fb2\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Cat\"\n        ],\n        \"_Animal\" : \"meow\",\n        \"_Cat\" : \"murka\",\n        \"Eyes\" : [\n                {\n                        \"Color\" : \"green\",\n                        \"Size\" : \"large\"\n                },\n                {\n                        \"Color\" : \"blue\",\n                        \"Size\" : \"middle\"\n                }\n        ]\n}\n{\n        \"_id\" : ObjectId(\"51ee622cfafee21464fd1fb3\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Cat\"\n        ],\n        \"_Animal\" : \"murr\",\n        \"_Cat\" : \"zoyka\",\n        \"Eyes\" : [\n                {\n                        \"Color\" : \"red\",\n                        \"Size\" : \"small\"\n                },\n                {\n                        \"Color\" : \"pink\",\n                        \"Size\" : \"middle\"\n                }\n        ]\n}\n{\n        \"_id\" : ObjectId(\"51ee622cfafee21464fd1fb4\"),\n        \"_t\" : [\n                \"Animal\",\n                \"Dog\"\n        ],\n        \"_Animal\" : \"bobik\",\n        \"_Dog\" : \"gav gav\",\n        \"Eyes\" : [\n                {\n                        \"Color\" : \"red\",\n                        \"Shape\" : \"round\"\n                },\n                {\n                        \"Color\" : \"pink\",\n                        \"Shape\" : \"square\"\n                }\n        ]\n}\n<\/pre>\n<p><strong>\u041f\u0440\u0438\u043c\u0435\u0440 \u0441 \u0440\u0430\u0437\u043d\u044b\u043c\u0438 \u043f\u043e\u0442\u043e\u043c\u043a\u0430\u043c\u0438 \u0432 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438 <em>(\u0442\u0430\u043a\u043e\u0439 \u0436\u0435 \u043f\u043e \u0441\u0441\u044b\u043b\u043a\u0435 \u043d\u0430 SO)<\/em>.<\/strong><\/p>\n<p>\u0414\u043e\u043f\u0443\u0441\u0442\u0438\u043c, \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043a\u043e\u0442\u043e\u043f\u0451\u0441, \u0443\u043d\u0430\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043e\u0442 \u0436\u0438\u0432\u043e\u0442\u043d\u043e\u0433\u043e.<\/p>\n<pre>\n    public class CatDog : Animal\n    {\n        public String _CatDog { get; set; }\n        public List Eyes { get; set; }\n    }\n<\/pre>\n<p>\u041e\u043d \u043d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435 \u0442\u0440\u0435\u0445\u0433\u043b\u0430\u0437\u044b\u0439. \u0414\u043e\u0431\u0430\u0432\u0438\u043c \u0435\u0433\u043e \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044e.<\/p>\n<pre>\n            collection.Insert(new CatDog\n            {\n                _Animal = \"kotopes\",\n                _CatDog = \"three eyed\",\n                Eyes = new List()\n                {\n                    new Eye(){Color=\"transparent\"},\n                    new CatEye(){Color=\"red\", Size =\"big\"},\n                    new DogEye(){Color=\"pink\", Shape=\"square\"}\n                }\n            });\n<\/pre>\n<p>\u0418 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0435\u043c\u0443 \u0432 \u0433\u043b\u0430\u0437\u0430.<\/p>\n<pre>\n            var query = Query.EQ(\"_Animal\", \"kotopes\");\n            var eyes = collection.FindOneAs(query).Eyes;\n\n            foreach (var eye in eyes)\n            {\n                Console.WriteLine(eye.GetType().ToString());\n            }\n<\/pre>\n<p>\u0420\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:<br \/>\nPolyMongo.Eye<br \/>\nPolyMongo.CatEye<br \/>\nPolyMongo.DogEye<\/p>\n<p>\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442 \u0432 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438:<\/p>\n<pre>\n{\n        \"_id\" : ObjectId(\"51ee648afafee2248811b94e\"),\n        \"_t\" : [\n                \"Animal\",\n                \"CatDog\"\n        ],\n        \"_Animal\" : \"kotopes\",\n        \"_CatDog\" : \"three eyed\",\n        \"Eyes\" : [\n                {\n                        \"Color\" : \"transparent\"\n                },\n                {\n                        \"_t\" : \"CatEye\",\n                        \"Color\" : \"red\",\n                        \"Size\" : \"big\"\n                },\n                {\n                        \"_t\" : \"DogEye\",\n                        \"Color\" : \"pink\",\n                        \"Shape\" : \"square\"\n                }\n        ]\n}<\/pre>\n<p><strong>\u0418\u0442\u043e\u0433\u043e<\/strong><br \/>\n\u041c\u043e\u0436\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u044f\u043c\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432, \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u043d\u044b\u0445 \u043e\u0442 \u043e\u0434\u043d\u043e\u0433\u043e \u043a\u043b\u0430\u0441\u0441\u0430. \u042d\u0442\u043e \u0445\u043e\u0440\u043e\u0448\u043e.<br \/>\n\u041d\u0430\u0437\u0432\u0430\u043d\u0438\u044f \u0442\u0438\u043f\u043e\u0432 \u0445\u0440\u0430\u043d\u044f\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0435, \u0438 \u0432 \u0441\u043b\u0443\u0447\u0430\u0435 \u0440\u0435\u0444\u0430\u043a\u0442\u043e\u0440\u0438\u043d\u0433\u0430&#8230; \u041d\u0443 \u0432\u044b \u043f\u043e\u043d\u044f\u043b\u0438.<\/p>\n<p>\u041c\u043e\u0436\u0435\u0442 \u044f \u0433\u0434\u0435-\u0442\u043e \u0447\u0442\u043e-\u0442\u043e \u0443\u043f\u0443\u0441\u0442\u0438\u043b\u0430 \u0438\u043b\u0438 \u043d\u0435\u0434\u043e\u043f\u043e\u043d\u044f\u043b\u0430, \u043d\u0435 \u0441\u0443\u0434\u0438\u0442\u0435 \u0441\u0442\u0440\u043e\u0433\u043e.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u041f\u043e\u044f\u0432\u0438\u043b\u0430\u0441\u044c \u0437\u0430\u0434\u0430\u0447\u0430 \u0445\u0440\u0430\u043d\u0438\u0442\u044c \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u044b \u0441 \u0440\u0430\u0437\u043d\u043e\u0439 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u043e\u0439 \u0432 \u043e\u0434\u043d\u043e\u0439 \u043a\u043e\u043b\u043b\u0435\u043a\u0446\u0438\u0438. \u0412\u043e\u043f\u0440\u043e\u0441 \u043d\u0430 \u0421\u041e http:\/\/stackoverflow.com\/questions\/13956413\/mongodb-embedded-polymorphic-objects \u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f http:\/\/docs.mongodb.org\/ecosystem\/tutorial\/serialize-documents-with-the-csharp-driver\/#polymorphic-classes-and-discriminators \u041c\u043e\u0438 \u043f\u0440\u0438\u043c\u0435\u0440\u044b.<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,53,20],"tags":[227,254],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-csharp","category-mongodb","category-mycode","tag-nasledovanie","tag-polimorfizm"],"_links":{"self":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/comments?post=160"}],"version-history":[{"count":0,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/posts\/160\/revisions"}],"wp:attachment":[{"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/media?parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/categories?post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/infobase.athn.ru\/program\/wp-json\/wp\/v2\/tags?post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}