如何从通用 DocumentDBRepository 中的 DocumentDB 文档模型中读取类型名称?
How can I read a type name from a DocumentDB document model from within a generic DocumentDBRepository?
我有一个包含异构文档类型的 DocumentDB 集合。我的 DocumentDB 存储库基于 this GitHub project,并且具有 GetItems
方法,如下所示:
public IReadOnlyCollection<T> GetItems()
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink).ToList();
}
public T GetItem(Expression<Func<T, bool>> predicate)
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink)
.Where(predicate)
.AsEnumerable()
.FirstOrDefault();
}
存储库运行良好,但是当我真正想按类型查询时,使用上述查询返回了我的所有文档。
一些来源(例如 this reddit and )建议使用我的 DocumentModels 中内置的类型属性,因此我更新了我的 BaseDocument
class(所有文档都继承自该属性)以包含一种。 BaseDocument
现在看起来像这样:
[Serializable]
public class BaseDocument
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "type")]
public string Type { get; set; }
public BaseDocument(string id, string type)
{
Id = id;
Type = type;
}
}
我已尝试更改我的 GetItems 方法以包含类型字符串,如下所示:
public IReadOnlyCollection<T> GetItems(string type)
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink,
String.Format("SELECT * FROM collection c WHERE c.type = '{0}'", type)).ToList();
}
这行得通,但每次调用 GetItems()
时都必须通过魔术字符串,这似乎很笨拙。我想我可以在每个 FooDocument
(每个扩展 BaseDocument
)中包含一个常量来指定类型名称,但我不知道如何在不传递它的情况下读取它。我如何从我的 DocumentDbRepository<FooDocument>
class 中读取这个常量?
添加一个public得到属性到returns类型的BaseDocument
类似的东西(这里是伪代码,请多多包涵);
public string Type
get {
return this.GetType() // you can control if you want the FQN or just the name
}
将以下内容添加到 GetItems 方法签名的末尾
where T : BaseDocument
现在您可以在您的存储库中执行类似的操作;
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink,
String.Format("SELECT * FROM collection c WHERE c.type = '{0}'", T.Type)).ToList();
这消除了对常量的需要,您不必将类型作为参数传递给您的方法。
您甚至可以使用 LINQ 执行以下操作;
GetItems<T>(Predicate predicate) Where T : BaseDocument {
return client.CreateDocumentQuery(collectionLink)
.Where(predicate)
.Where(d => d.Type == T.Type);
}
如果您希望我可以在某个地方分享/post,我有一个可以执行此操作的工作存储库。
我有一个包含异构文档类型的 DocumentDB 集合。我的 DocumentDB 存储库基于 this GitHub project,并且具有 GetItems
方法,如下所示:
public IReadOnlyCollection<T> GetItems()
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink).ToList();
}
public T GetItem(Expression<Func<T, bool>> predicate)
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink)
.Where(predicate)
.AsEnumerable()
.FirstOrDefault();
}
存储库运行良好,但是当我真正想按类型查询时,使用上述查询返回了我的所有文档。
一些来源(例如 this reddit and BaseDocument
class(所有文档都继承自该属性)以包含一种。 BaseDocument
现在看起来像这样:
[Serializable]
public class BaseDocument
{
[JsonProperty(PropertyName = "id")]
public string Id { get; set; }
[JsonProperty(PropertyName = "type")]
public string Type { get; set; }
public BaseDocument(string id, string type)
{
Id = id;
Type = type;
}
}
我已尝试更改我的 GetItems 方法以包含类型字符串,如下所示:
public IReadOnlyCollection<T> GetItems(string type)
{
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink,
String.Format("SELECT * FROM collection c WHERE c.type = '{0}'", type)).ToList();
}
这行得通,但每次调用 GetItems()
时都必须通过魔术字符串,这似乎很笨拙。我想我可以在每个 FooDocument
(每个扩展 BaseDocument
)中包含一个常量来指定类型名称,但我不知道如何在不传递它的情况下读取它。我如何从我的 DocumentDbRepository<FooDocument>
class 中读取这个常量?
添加一个public得到属性到returns类型的BaseDocument 类似的东西(这里是伪代码,请多多包涵);
public string Type
get {
return this.GetType() // you can control if you want the FQN or just the name
}
将以下内容添加到 GetItems 方法签名的末尾
where T : BaseDocument
现在您可以在您的存储库中执行类似的操作;
return Client.CreateDocumentQuery<T>(Collection.DocumentsLink,
String.Format("SELECT * FROM collection c WHERE c.type = '{0}'", T.Type)).ToList();
这消除了对常量的需要,您不必将类型作为参数传递给您的方法。
您甚至可以使用 LINQ 执行以下操作;
GetItems<T>(Predicate predicate) Where T : BaseDocument {
return client.CreateDocumentQuery(collectionLink)
.Where(predicate)
.Where(d => d.Type == T.Type);
}
如果您希望我可以在某个地方分享/post,我有一个可以执行此操作的工作存储库。