Skip to content

Commit b0e39ce

Browse files
committed
Introduced RegisterValueType<TValueType>() method to allow custom value type registration using generics
1 parent 8e71889 commit b0e39ce

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

src/Marten/StoreOptions.Identity.cs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ internal IDocumentStorage<TDoc, TId> ResolveCorrectedDocumentStorage<TDoc, TId>(
2121
var provider = Providers.StorageFor<TDoc>();
2222
var raw = provider.Select(tracking);
2323

24-
if (raw is IDocumentStorage<TDoc, TId> storage) return storage;
24+
if (raw is IDocumentStorage<TDoc, TId> storage)
25+
return storage;
2526

2627
var valueTypeInfo = TryFindValueType(raw.IdType);
2728
if (valueTypeInfo == null)
@@ -76,8 +77,10 @@ internal IIdGeneration DetermineIdStrategy(Type documentType, MemberInfo idMembe
7677

7778
private bool idMemberIsSettable(MemberInfo idMember)
7879
{
79-
if (idMember is FieldInfo f) return f.IsPublic;
80-
if (idMember is PropertyInfo p) return p.CanWrite && p.SetMethod != null;
80+
if (idMember is FieldInfo f)
81+
return f.IsPublic;
82+
if (idMember is PropertyInfo p)
83+
return p.CanWrite && p.SetMethod != null;
8184

8285
return false;
8386
}
@@ -93,6 +96,19 @@ internal ValueTypeInfo FindOrCreateValueType(Type idType)
9396
return valueType ?? RegisterValueType(idType);
9497
}
9598

99+
/// <summary>
100+
/// Register a custom value type with Marten. Doing this enables Marten
101+
/// to use this type correctly within LINQ expressions. The "TValueType"
102+
/// should wrap a single, primitive value with a single public get-able
103+
/// property
104+
/// </summary>
105+
/// <param name="type"></param>
106+
/// <returns></returns>
107+
public ValueTypeInfo RegisterValueType<TValueType>() where TValueType : notnull
108+
{
109+
return RegisterValueType(typeof(TValueType));
110+
}
111+
96112
/// <summary>
97113
/// Register a custom value type with Marten. Doing this enables Marten
98114
/// to use this type correctly within LINQ expressions. The "value type"
@@ -122,7 +138,8 @@ public ValueTypeInfo RegisterValueType(Type type)
122138
valueProperty = type.GetProperties().SingleOrDefaultIfMany();
123139
}
124140

125-
if (valueProperty == null || !valueProperty.CanRead) throw new InvalidValueTypeException(type, "Must be only a single public, 'gettable' property");
141+
if (valueProperty == null || !valueProperty.CanRead)
142+
throw new InvalidValueTypeException(type, "Must be only a single public, 'gettable' property");
126143

127144
var ctor = type.GetConstructors()
128145
.FirstOrDefault(x => x.GetParameters().Length == 1 && x.GetParameters()[0].ParameterType == valueProperty.PropertyType);

0 commit comments

Comments
 (0)