The purpose of using ReadOnly collections is discussed here: Solution for CA2227 or better approach?
My question is if there is a way not to make the code "ugly" by using the new initializer of c#.
For example we have the following code
return new User
{
Firstname = claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.UserFirstname)?.Value,
Lastname = claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.UserLastname)?.Value,
LogonName = claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.UserLogon)?.Value,
Roles = claims.FirstOrDefault(x => x.Type == Constants.ClaimTypes.UserRoles)?.Value.Split(";#").ToList()
};
When the model for Roles
is
public List<string> Roles { get; } = new List<string>();
the above code won't work because we cannot assign a new List to Roles.
Is there a way to return new User
without blowing up the code? Shure I could create an initializer method in the User class but this makes the new User(...)
call cluttered