3
I have this class example:
public class User : APerson
{
private string _userName;
[DataMember]
public virtual string UserName
{
get { return _userName; }
set
{
if (string.IsNullOrWhiteSpace(value))
{
throw new FormatException(ErrorMessage.User.USERNAME_REQUIRED);
}
if (value.Length > 50)
{
throw new FormatException(ErrorMessage.User.USERNAME_TOO_LONG);
}
_userName = value;
}
}
[DataMember]
public virtual string Password { get; set; }
[DataMember]
public virtual bool IsActiveDirectory { get; set; } = false;
[IgnoreDataMember]
public virtual IList<Application> Applications { get; set; }
[IgnoreDataMember]
public virtual IList<UserAccessKey> UserAccessKeys { get; set; }
[IgnoreDataMember]
public virtual IList<UserApplication> UserApplications { get; set; }
[IgnoreDataMember]
public virtual AClient Client { get; set; }
[IgnoreDataMember]
public virtual IList<UserLog> UserLogs { get; set; }
}
The Aclient, is this abstract class:
public abstract class AClient
{
private string _companyName;
private string _company;
[DataMember]
public virtual int Id { get; set; }
[DataMember]
public virtual Guid Hash { get; set; }
[DataMember]
public virtual bool IsManager { get; set; }
[DataMember]
public virtual string CompanyName
{
get { return _companyName; }
set
{
if (!string.IsNullOrEmpty(value) && value.Length > 100)
{
throw new Exception(ErrorMessage.Client.COMPANY_NAME_TOO_LOG);
}
_companyName = value;
}
}
[DataMember]
public virtual string Company
{
get { return _company; }
set
{
if (!string.IsNullOrEmpty(value) && value.Length > 150)
{
throw new Exception(ErrorMessage.Client.COMPANY_TOO_LONG);
}
_company = value;
}
}
[DataMember]
public virtual bool IsActive { get; set; }
[DataMember]
public virtual string CssFileExtensionName { get; set; }
}
So far so good. The problem is that if I via LINQ try to locate in the Client attribute of the User class, the Description attribute, which does not exist in the abstract class, I will have a build error.
What to do in these cases? How to explain to LINQ what concrete class it inherits from the abstract class, which has this attribute.
In addition: If I have the following example:
public virtual IList<APerson> Patients { get; set; } = new List<Patient>();
I have a compilation error, even the Patient class inheriting from Aperson.
If I do this below, the second line works. The first no!
public virtual IList<APerson> Patients { get; set; } = new List<Patient>();
public APerson person { get; set; } = new Patient();
I cannot understand these problems. Can someone give me a light? Note: Starting now with ID.
Right, but your questioning has no relation to dependency injection, can paste the content of the build error into the question?
– Felipe Assunção
No??? But in the injection of dependency you prefer interfaces and abstract classes instead of concrete classes, no? My question is how to program this way. Which category does it indicate? And regarding the error, I’m not in the machine now but the error informs that Patient is not an aperson... the craziest and that if I make a simple attribute, not a list, it’s sure... Already set that example
– DiegoSantos
Related: What are covariance and countervariance?
– ramaral