Skip to content

Commit

Permalink
adding reference constructors attributes (#20611)
Browse files Browse the repository at this point in the history
* update constructors for reference types

* fix stylecop issue

* add two way implicit for autorest deserialization

* update constructor signatures for autogen

* add attributes to define ctor types for autogen
  • Loading branch information
m-nash authored Apr 23, 2021
1 parent 896d78b commit 2d7ec6e
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// An attribute class indicating the constructor to use for initialization.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public class InitializationConstructor : Attribute
{
/// <summary>
/// Instatiate a new InitializationConstructor attribute.
/// </summary>
public InitializationConstructor()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,22 @@ namespace Azure.ResourceManager.Core
/// A class representing the base resource used by all azure resources.
/// </summary>
[ReferenceType(typeof(TenantResourceIdentifier))]
public class Resource<TIdentifier> : IEquatable<Resource<TIdentifier>>, IEquatable<string>,
public abstract class Resource<TIdentifier> : IEquatable<Resource<TIdentifier>>, IEquatable<string>,
IComparable<Resource<TIdentifier>>, IComparable<string> where TIdentifier : TenantResourceIdentifier
{
/// <summary>
/// Initializes an empty instance of <see cref="Resource{TIdentifier}"/>.
/// </summary>
public Resource() { }
[InitializationConstructor]
protected Resource() { }

/// <summary>
/// Initializes a new instance of <see cref="Resource{TIdentifier}"/> for deserialization.
/// </summary>
/// <param name="id"> The id of the resource. </param>
/// <param name="name"> The name of the resource. </param>
/// <param name="type"> The <see cref="ResourceType"/> of the resource. </param>
[SerializationConstructor]
protected Resource(TIdentifier id, string name, ResourceType type)
{
Id = id;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;

namespace Azure.ResourceManager.Core
{
/// <summary>
/// An attribute class indicating the constructor to use for serialization.
/// </summary>
[AttributeUsage(AttributeTargets.Constructor)]
public class SerializationConstructor : Attribute
{
/// <summary>
/// Instatiate a new SerializationConstructor attribute.
/// </summary>
public SerializationConstructor()
{
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@ public partial class SubResource
/// <summary>
/// Initializes an empty instance of <see cref="SubResource"/>.
/// </summary>
public SubResource() { }
[InitializationConstructor]
protected SubResource() { }

/// <summary> Initializes a new instance of SubResource. </summary>
/// <param name="id"> ARM resource Id. </param>
[SerializationConstructor]
protected internal SubResource(string id)
{
Id = id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ namespace Azure.ResourceManager.Core
/// Generic representation of a tracked resource. All tracked resources should extend this class
/// </summary>
[ReferenceType(typeof(TenantResourceIdentifier))]
public partial class TrackedResource<TIdentifier> : Resource<TIdentifier>
public abstract partial class TrackedResource<TIdentifier> : Resource<TIdentifier>
where TIdentifier : TenantResourceIdentifier
{
/// <summary>
/// Initializes an empty instance of <see cref="TrackedResource{TIdentifier}"/>.
/// </summary>
public TrackedResource(LocationData location)
[InitializationConstructor]
protected TrackedResource(LocationData location)
{
Location = location;
}
Expand All @@ -29,6 +30,7 @@ public TrackedResource(LocationData location)
/// <param name="type"> The <see cref="ResourceType"/> of the resource. </param>
/// <param name="tags"> The tags for the resource. </param>
/// <param name="location"> The location of the resource. </param>
[SerializationConstructor]
protected TrackedResource(TIdentifier id, string name, ResourceType type, IDictionary<string, string> tags, LocationData location)
: base(id, name, type)
{
Expand Down

0 comments on commit 2d7ec6e

Please sign in to comment.