'.NET 6 & Cosmos DB: How best to return a different type T2 from container.CreateItemAsync<T1>(t1)?

Background

Using .NET6 with Cosmos DB v4, I can create an item in a container like so:

Container container = client.GetContainer(config.GetValue<string>("CosmosDatabaseName"),
                    config.GetValue<string>("CosmosUsersContainerName"));
NewUser createResponse = await container.CreateItemAsync<NewUser>(newUser);

The thing is, container.CreateItemAsync<NewUser>(newUser) seems to only be able to return the NewUser type, but I want to return type User. User has all the same fields as NewUser, plus lastCheckedDate and accreditationInProgress. Writing this:

User createResponse = await container.CreateItemAsync<NewUser>(newUser);

returns

CS0029 Cannot implicitly convert type 'Microsoft.Azure.Cosmos.ItemResponse<QualityManagement.CreateUser.CreateUser.NewUser>' to 'QualityManagement.CreateUser.CreateUser.User'

Question

How do I easily cast the result of await container.CreateItemAsync<NewUser>(newUser) to type User?

Possible backup options

If I can't return a different type, as far as I can see I have 3 options:

  1. Make a second constructor in User that takes a NewUser object,
  2. Simply await container.CreateItemAsync<NewUser>(newUser) and have a second method that queries the db for the new User, however that will be rather verbose.
  3. Make User inherit NewUser, but that seems messy, especially as I want to keep each object immutable.

C# Objects

public class NewUser
{
  public string Id { get; internal set; }
  public string FirstName { get; }
  public string Surname { get; }
  public string Email { get; }
  public string AccreditationManagerId { get; internal set; }
  public string AccreditationManagerName { get; }
  public string Role { get; }
  public WorkArea WorkArea { get; }

  public NewUser(string firstName, string surname, string email, string accreditationManagerId, string accreditationManagerName, string role, WorkArea workArea)
  {
    FirstName = firstName;
    Surname = surname;
    Email = email;
    AccreditationManagerId = accreditationManagerId;
    AccreditationManagerName = accreditationManagerName;
    Role = role;
    WorkArea = workArea;
  }
}

public class User
{
  public string Id { get; }
  public string FirstName { get; }
  public string Surname { get; }
  public string Email { get; }
  public string AccreditationManagerId { get; }
  public string AccreditationManagerName { get; }
  public string Role { get; }
  public WorkArea WorkArea { get; }
  public DateTime LastCheckedDate { get; }
  public IEnumerable<AccreditationInProgress> AccreditationInProgress { get; }

  public User(string id, string firstName, string surname, string email, string accreditationManagerId, string accreditationManagerName, string role, WorkArea workArea, DateTime lastCheckedDate, List<AccreditationInProgress> accreditationInProgress)
  {
    Id = id;
    FirstName = firstName;
    Surname = surname;
    Email = email;
    AccreditationManagerId = accreditationManagerId;
    AccreditationManagerName = accreditationManagerName;
    Role = role;
    WorkArea = workArea;
    LastCheckedDate = lastCheckedDate;
    AccreditationInProgress = accreditationInProgress;
  }
}

References

I found https://joonasw.net/view/exploring-cosmos-db-sdk-v3, but it didn't help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source