@@ -10,39 +10,39 @@ namespace JsonApiDotNetCore.Configuration
1010 /// Provides metadata for a resource, such as its attributes and relationships.
1111 /// </summary>
1212 [ PublicAPI ]
13- public class ResourceContext
13+ public sealed class ResourceContext
1414 {
1515 private IReadOnlyCollection < ResourceFieldAttribute > _fields ;
1616
1717 /// <summary>
1818 /// The publicly exposed resource name.
1919 /// </summary>
20- public string PublicName { get ; set ; }
20+ public string PublicName { get ; }
2121
2222 /// <summary>
2323 /// The CLR type of the resource.
2424 /// </summary>
25- public Type ResourceType { get ; set ; }
25+ public Type ResourceType { get ; }
2626
2727 /// <summary>
2828 /// The identity type of the resource.
2929 /// </summary>
30- public Type IdentityType { get ; set ; }
30+ public Type IdentityType { get ; }
3131
3232 /// <summary>
3333 /// Exposed resource attributes. See https://jsonapi.org/format/#document-resource-object-attributes.
3434 /// </summary>
35- public IReadOnlyCollection < AttrAttribute > Attributes { get ; set ; }
35+ public IReadOnlyCollection < AttrAttribute > Attributes { get ; }
3636
3737 /// <summary>
3838 /// Exposed resource relationships. See https://jsonapi.org/format/#document-resource-object-relationships.
3939 /// </summary>
40- public IReadOnlyCollection < RelationshipAttribute > Relationships { get ; set ; }
40+ public IReadOnlyCollection < RelationshipAttribute > Relationships { get ; }
4141
4242 /// <summary>
4343 /// Related entities that are not exposed as resource relationships.
4444 /// </summary>
45- public IReadOnlyCollection < EagerLoadAttribute > EagerLoads { get ; set ; }
45+ public IReadOnlyCollection < EagerLoadAttribute > EagerLoads { get ; }
4646
4747 /// <summary>
4848 /// Exposed resource attributes and relationships. See https://jsonapi.org/format/#document-resource-object-fields.
@@ -56,7 +56,7 @@ public class ResourceContext
5656 /// <remarks>
5757 /// In the process of building the resource graph, this value is set based on <see cref="ResourceLinksAttribute.TopLevelLinks" /> usage.
5858 /// </remarks>
59- public LinkTypes TopLevelLinks { get ; internal set ; } = LinkTypes . NotConfigured ;
59+ public LinkTypes TopLevelLinks { get ; }
6060
6161 /// <summary>
6262 /// Configures which links to show in the <see cref="Serialization.Objects.ResourceLinks" /> object for this resource type. Defaults to
@@ -65,7 +65,7 @@ public class ResourceContext
6565 /// <remarks>
6666 /// In the process of building the resource graph, this value is set based on <see cref="ResourceLinksAttribute.ResourceLinks" /> usage.
6767 /// </remarks>
68- public LinkTypes ResourceLinks { get ; internal set ; } = LinkTypes . NotConfigured ;
68+ public LinkTypes ResourceLinks { get ; }
6969
7070 /// <summary>
7171 /// Configures which links to show in the <see cref="Serialization.Objects.RelationshipLinks" /> object for all relationships of this resource type.
@@ -75,11 +75,83 @@ public class ResourceContext
7575 /// <remarks>
7676 /// In the process of building the resource graph, this value is set based on <see cref="ResourceLinksAttribute.RelationshipLinks" /> usage.
7777 /// </remarks>
78- public LinkTypes RelationshipLinks { get ; internal set ; } = LinkTypes . NotConfigured ;
78+ public LinkTypes RelationshipLinks { get ; }
79+
80+ public ResourceContext ( string publicName , Type resourceType , Type identityType , IReadOnlyCollection < AttrAttribute > attributes ,
81+ IReadOnlyCollection < RelationshipAttribute > relationships , IReadOnlyCollection < EagerLoadAttribute > eagerLoads ,
82+ LinkTypes topLevelLinks = LinkTypes . NotConfigured , LinkTypes resourceLinks = LinkTypes . NotConfigured ,
83+ LinkTypes relationshipLinks = LinkTypes . NotConfigured )
84+ {
85+ ArgumentGuard . NotNullNorEmpty ( publicName , nameof ( publicName ) ) ;
86+ ArgumentGuard . NotNull ( resourceType , nameof ( resourceType ) ) ;
87+ ArgumentGuard . NotNull ( identityType , nameof ( identityType ) ) ;
88+ ArgumentGuard . NotNull ( attributes , nameof ( attributes ) ) ;
89+ ArgumentGuard . NotNull ( relationships , nameof ( relationships ) ) ;
90+ ArgumentGuard . NotNull ( eagerLoads , nameof ( eagerLoads ) ) ;
91+
92+ PublicName = publicName ;
93+ ResourceType = resourceType ;
94+ IdentityType = identityType ;
95+ Attributes = attributes ;
96+ Relationships = relationships ;
97+ EagerLoads = eagerLoads ;
98+ TopLevelLinks = topLevelLinks ;
99+ ResourceLinks = resourceLinks ;
100+ RelationshipLinks = relationshipLinks ;
101+ }
79102
80103 public override string ToString ( )
81104 {
82105 return PublicName ;
83106 }
107+
108+ public override bool Equals ( object obj )
109+ {
110+ if ( ReferenceEquals ( this , obj ) )
111+ {
112+ return true ;
113+ }
114+
115+ if ( obj is null || GetType ( ) != obj . GetType ( ) )
116+ {
117+ return false ;
118+ }
119+
120+ var other = ( ResourceContext ) obj ;
121+
122+ return PublicName == other . PublicName && ResourceType == other . ResourceType && IdentityType == other . IdentityType &&
123+ Attributes . SequenceEqual ( other . Attributes ) && Relationships . SequenceEqual ( other . Relationships ) && EagerLoads . SequenceEqual ( other . EagerLoads ) &&
124+ TopLevelLinks == other . TopLevelLinks && ResourceLinks == other . ResourceLinks && RelationshipLinks == other . RelationshipLinks ;
125+ }
126+
127+ public override int GetHashCode ( )
128+ {
129+ var hashCode = new HashCode ( ) ;
130+
131+ hashCode . Add ( PublicName ) ;
132+ hashCode . Add ( ResourceType ) ;
133+ hashCode . Add ( IdentityType ) ;
134+
135+ foreach ( AttrAttribute attribute in Attributes )
136+ {
137+ hashCode . Add ( attribute ) ;
138+ }
139+
140+ foreach ( RelationshipAttribute relationship in Relationships )
141+ {
142+ hashCode . Add ( relationship ) ;
143+ }
144+
145+ foreach ( EagerLoadAttribute eagerLoad in EagerLoads )
146+ {
147+ hashCode . Add ( eagerLoad ) ;
148+ }
149+
150+ hashCode . Add ( TopLevelLinks ) ;
151+ hashCode . Add ( ResourceLinks ) ;
152+ hashCode . Add ( RelationshipLinks ) ;
153+
154+ return hashCode . ToHashCode ( ) ;
155+ }
84156 }
85157}
0 commit comments