Skip to content

Commit 384f730

Browse files
committed
Throw exception on [FilterByAttribute("ContentType", …)]
The `ContentType` is no longer stored as an attribute, so models attempting to `[FilterByAttribute("ContentType", …)]` will never return results—or, perhaps worse, operate off of potentially stale results from prior to the migration, yielding inconsistent results. To help avoid this situation, this will throw a runtime exception when a user attempts to map a model with this setting. A code analyzer would be a more sophisticated approach, since this is fundamentally a design-time problem. This is a quick and easy alternative that should prevent this scenario from being introduced, while giving clear guidance to implementations that have already implemented this from previous versions.
1 parent 13b1114 commit 384f730

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

OnTopic/Mapping/Annotations/FilterByAttributeAttribute.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
| Project Topics Library
55
\=============================================================================================================================*/
66

7+
using System;
8+
using OnTopic.Internal.Diagnostics;
9+
710
namespace OnTopic.Mapping.Annotations {
811

912
/*============================================================================================================================
@@ -30,9 +33,22 @@ public sealed class FilterByAttributeAttribute : System.Attribute {
3033
/// <param name="key">The key of the attribute to filter by.</param>
3134
/// <param name="value">The value of the attribute to filter by.</param>
3235
public FilterByAttributeAttribute(string key, string value) {
36+
37+
/*------------------------------------------------------------------------------------------------------------------------
38+
| Validate input
39+
\-----------------------------------------------------------------------------------------------------------------------*/
3340
TopicFactory.ValidateKey(key, false);
41+
Contract.Requires<ArgumentException>(
42+
!key.Equals("ContentType", StringComparison.OrdinalIgnoreCase),
43+
"The ContentType is not stored as an attribute. To filter by ContentType, use [FilterByContentType] instead."
44+
);
45+
46+
/*------------------------------------------------------------------------------------------------------------------------
47+
| Set properties
48+
\-----------------------------------------------------------------------------------------------------------------------*/
3449
Key = key;
3550
Value = value;
51+
3652
}
3753

3854
/*==========================================================================================================================

0 commit comments

Comments
 (0)