As of c# 6.0, which is now released, you should just do this,
public class Room
{
public Room(Client client)
{
this.Client = client;
}
public Client Client { get; }
}
public class Client
{
public Client(long id)
{
this.Id = id;
}
public long Id { get; }
}
Raising a property of Client
to Room
is an obvious breach of encapsulation and the DRY principle.
If you need to access the Id
of a Client
of a Room
you can do,
var clientId = room.Client?.Id;
Note the use of the Null Conditional Operator, clientId
will be an long?
, if Client
is null
, clientId
will be null
, otherwise clientId
will have the value of Client.Id
.