44// Created by: Dmitri Maximov
55// Created: 2007.09.12
66
7- using System ;
8- using System . Collections . Generic ;
7+ using System . Collections . Frozen ;
98using System . Text . RegularExpressions ;
109using Xtensive . Orm . Building . Definitions ;
1110using Xtensive . Orm . Internals ;
1413
1514namespace Xtensive . Orm . Building
1615{
17- internal class Validator
16+ internal class Validator ( IEnumerable < Type > validFieldTypes )
1817 {
19- private readonly HashSet < Type > validFieldTypes ;
20- private readonly Regex columnNamingRule ;
21- private readonly Regex typeNamingRule ;
22- private readonly Regex fieldNamingRule ;
18+ private static readonly Regex ColumnNamingRule = new ( @"^[\w][\w\-\.]*$" , RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
19+ private static readonly Regex TypeNamingRule = new ( @"^[\w][\w\-\.\(\),]*$" , RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
20+ private static readonly Regex FieldNamingRule = new ( @"^[\w][\w\-\.]*$" , RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
21+
22+ private readonly FrozenSet < Type > validFieldTypes = validFieldTypes . Append ( WellKnownOrmTypes . Key ) . ToFrozenSet ( ) ;
2323
2424 /// <summary>
2525 /// Determines whether the specified name is valid.
@@ -41,13 +41,13 @@ public void ValidateName(string name, ValidationRule rule)
4141 case ValidationRule . Type :
4242 case ValidationRule . Schema :
4343 case ValidationRule . Database :
44- namingRule = typeNamingRule ;
44+ namingRule = TypeNamingRule ;
4545 break ;
4646 case ValidationRule . Field :
47- namingRule = fieldNamingRule ;
47+ namingRule = FieldNamingRule ;
4848 break ;
4949 case ValidationRule . Column :
50- namingRule = columnNamingRule ;
50+ namingRule = ColumnNamingRule ;
5151 break ;
5252 default :
5353 throw new ArgumentOutOfRangeException ( ) ;
@@ -95,7 +95,7 @@ public void ValidateHierarchy(HierarchyDef hierarchyDef)
9595 }
9696
9797 // if one of key fields is TypeId field and number of fields == 2 then it is OK
98- if ( keyFields . Count == 2 && keyFields . Find ( f => f . Name == WellKnown . TypeIdFieldName ) != null ) {
98+ if ( keyFields . Count == 2 && keyFields . Any ( f => f . Name == WellKnown . TypeIdFieldName ) ) {
9999 throw new DomainBuilderException ( Strings . ExDefaultGeneratorCanServeHierarchyWithExactlyOneKeyField ) ;
100100 }
101101 }
@@ -257,17 +257,6 @@ internal void EnsureIsNullable(Type valueType)
257257 }
258258 }
259259
260- // Type initializer
261-
262- public Validator ( IEnumerable < Type > validFieldTypes )
263- {
264- columnNamingRule = new Regex ( @"^[\w][\w\-\.]*$" , RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
265- typeNamingRule = new Regex ( @"^[\w][\w\-\.\(\),]*$" , RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
266- fieldNamingRule = new Regex ( @"^[\w][\w\-\.]*$" , RegexOptions . Compiled | RegexOptions . CultureInvariant ) ;
267-
268- this . validFieldTypes = new HashSet < Type > ( validFieldTypes ) { WellKnownOrmTypes . Key } ;
269- }
270-
271260 public void ValidateHierarchyEquality ( TypeDef @interface , HierarchyDef first , HierarchyDef second )
272261 {
273262 // TypeId mode must match
@@ -277,17 +266,20 @@ public void ValidateHierarchyEquality(TypeDef @interface, HierarchyDef first, Hi
277266 @interface . Name , first . Root . Name , second . Root . Name ) ) ;
278267 }
279268
269+ var firstKeyFields = first . KeyFields ;
270+ var secondKeyFields = second . KeyFields ;
271+
280272 // Number of key fields must match
281- if ( first . KeyFields . Count != second . KeyFields . Count ) {
273+ if ( firstKeyFields . Count != secondKeyFields . Count ) {
282274 throw new DomainBuilderException ( string . Format (
283275 Strings . ExImplementorsOfXInterfaceBelongToHierarchiesWithDifferentKeyStructureYZ ,
284276 @interface . Name , first . Root . Name , second . Root . Name ) ) ;
285277 }
286278
287279 // Type of each key field must match
288- for ( var i = 0 ; i < first . KeyFields . Count ; i ++ ) {
289- var masterField = first . Root . Fields [ first . KeyFields [ i ] . Name ] ;
290- var candidateField = second . Root . Fields [ second . KeyFields [ i ] . Name ] ;
280+ for ( var i = 0 ; i < firstKeyFields . Count ; i ++ ) {
281+ var masterField = first . Root . Fields [ firstKeyFields [ i ] . Name ] ;
282+ var candidateField = second . Root . Fields [ secondKeyFields [ i ] . Name ] ;
291283 if ( masterField . ValueType != candidateField . ValueType ) {
292284 throw new DomainBuilderException ( string . Format (
293285 Strings . ExImplementorsOfXInterfaceBelongToHierarchiesWithDifferentKeyStructureYZ ,
0 commit comments