且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

DocumentDb中的类继承

更新时间:2023-02-15 09:24:49

是否有内置的支持来处理此问题?如果我在客户代码中使用Lambda表达式,它将能够自动区分黑白类型吗?

是的,只要您在通用方法中将特定类型指定为client.CreateDocumentQuery<YourType>(collectionLink),就可以在客户端使用lambda语法.

Yes, you can use lambda syntax in client side, as long as you specify the specific type in the generic method, as client.CreateDocumentQuery<YourType>(collectionLink).

以下查询是否只会带回B型对象?还是会考虑基类A的实例?

DocumentDB是无模式存储,不存储类型信息.客户端中提供的重载泛型方法是语法糖,使您可以轻松创建查询. 所有查询均针对没有类型信息的json文档进行评估.

DocumentDB is a schemaless store and does not store type information. The overloaded generic methods provided in the client are syntactic sugar, to let you easily create queries. All queries are evaluated against json documents, which do not have type information.

继承方案

因此,如果您对仅在派生类中存在的属性进行查询,则将获得仅与派生类相对应的值.但是,如果要查询的属性同时在基类和派生类中,则将同时获得两个结果.例如,在您的情况下,对W进行过滤将只给您类B的结果,而对X, Y or Z进行过滤将给您两个类AB的值.

So, if you fire a query for a property which is present only in your derived class, you will get values corresponding only to your derived class. But, if the property you are querying on is in both base class and derived class, you'll get back both results. For example, in your case filtering on W would give you results of only class B, but filtering on X, Y or Z would give you values for both classes A and B.

在同一集合中具有共享架构的类

请注意,这不仅发生在基于基类的情况下.如果您拥有两个单独的类,它们不会互相继承,但是具有相同名称的属性,则可能会发生相同的行为.查询该属性将返回两个类的结果. 例如,如果您有两个存储在同一集合中的类:

Note that this does not just happen in the base-derived class scenario. Same behavior would happen if you have 2 separate classes, which do not inherit each other, but have a property with the same name. Querying on that property will return results of both classes. For example, if you have 2 classes which are stored in the same collection:

class A1 { int x; }
class A2 { int x; }

即使您使用client.CreateDocumentQuery<A1>(collectionLink)构成查询,A级和A级的结果也是如此. B将被退回.正如我前面提到的,客户端中的类型规范只是为了使您在查询时更轻松.

Even if you form your query using the client.CreateDocumentQuery<A1>(collectionLink), results of both class A & B will be returned. As I mentioned earlier, the type specification in the client is just to make your life easier while forming the query.

我希望能够查询不同类型的数据,并且将共享的架构元素存储在同一集合中-我建议您使用一个单独的属性来手动存储类型信息并对该属性进行过滤.

I you want to be able to query different types of data, having shared schema elements, stored in the same collection - I would recommend having a separate property to store the type information manually and filtering on that property.

class DocumentDbData 
{ 
    string DataType; 

    DocumentDbData(string type) { DataType = type;}

}
class A1 : DocumentDbData 
{
    string x;
    A1() : base("A1")
}
class A2 : DocumentDbData 
{
    string x;
    A2() : base("A2")
}

查询client.CreateDocumentQuery<A1>(collectionLink).Where(d => d.DataType == "A1" && d.x == "xvaluefilter")现在将仅返回A1类的数据.

The query, client.CreateDocumentQuery<A1>(collectionLink).Where(d => d.DataType == "A1" && d.x == "xvaluefilter") will now return only data for class A1.