valueinjector를 사용하여 모델의 값을 삽입하는 viewmodel을 가지고 있습니다 (TPT 상속을 구현했습니다). (모델 소스에없는 속성) 내 사용자 정의 속성 중 하나 때문에이 과정 동안, 나는 다음과 같은 오류가 점점 계속 :TPT 상속 프로젝트에서 valueinjector가있는 View Model의 사용자 정의 속성
Object reference not set to an instance of an object.
을 내가 그 valueinjector이 때때로 해당 속성에가는 계속해서 발견 그때. 아래 예제와 같이 사용자 정의 속성은 "FullName"입니다.
public class EmployeeVm
{
public EmployeeVm(Employee employee)
{
this.InjectFrom<Employee>(employee);
}
public EmployeeVm(int id):this(new Context().Employees.Find(id))
{
}
public EmployeeVm()
{
}
public int EmployeeId { get; set; }
[Display(Name = "First Name")]
[Required(ErrorMessage = "Pleae enter First Name"), StringLength(50)]
public string FirstName { get; set; }
[Display(Name="Middle Name"), StringLength(50)]
public string MiddleName { get; set; }
[Display(Name="Last Name"), StringLength(50)]
[Required(ErrorMessage = "Please enter Last Name")]
public string LastName { get; set; }
public string FullName {
get
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append("<b>");
stringBuilder.Append(LastName.ToUpper());
stringBuilder.Append("</b>");
if (!string.IsNullOrEmpty(MiddleName))
{
stringBuilder.Append(", ");
stringBuilder.Append(MiddleName);
}
stringBuilder.Append(", ");
stringBuilder.Append(LastName);
return stringBuilder.ToString();
}
}
}
내 마음을 교차 유일한 해결책은 다른 속성을 설정하기 전에 속성을 얻으려고하지 않도록 해당 속성을 무시 valueinjector 확인하는 것입니다.
[Table("Person")]
public abstract class Person:ConventionInjection
{
public Person()
{
this.PersonAddresses = new List<PersonAddress>();
this.PersonEmails = new List<PersonEmail>();
this.PersonPhones = new List<PersonPhone>();
}
[Key]
public int PersonId { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
//public virtual Employee Employee { get; set; }
protected override bool Match(ConventionInfo c)
{
throw new NotImplementedException();
}
public List<PersonAddress> PersonAddresses { get; set; }
public List<PersonEmail> PersonEmails { get; set; }
public List<PersonPhone> PersonPhones { get; set; }
}
public class Employee:Person
{
public Employee()
{
this.Identifications=new List<Identification>();
this.BankAccounts=new List<BankAccount>();
}
public DateTime? DateOfBirth { get; set; }
//Other properties are inherited from Person abstract class
public virtual ICollection<Identification> Identifications { get; set; }
public virtual ICollection<BankAccount> BankAccounts { get; set; }
protected override bool Match(ConventionInfo c)
{
if (c.TargetProp.Name == "FullName")
{
return false;
}
var isMatch = (c.SourceProp.Name == "PersonId" && c.TargetProp.Name == "EmployeeId") ||
(c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == c.TargetProp.Type);
return isMatch;
}
}
가에서-에도 불구하고 이런, 내가 같은 위에서 언급 한 오류가에 보관 다음과 같이이를 위해 전 직원 모델에서 사용자 정의 인젝터를 작성했습니다.
또한 LoopValueInjection의 UseSourceProp 메서드를 재정의하는 다른 솔루션을 발견했습니다.
http://valueinjecter.codeplex.com/discussions/234706
하지만, 그 나는 이미 내 기본 클래스뿐만 아니라 파생 클래스에서 하나 개의 클래스를 상속으로 내 시나리오에서 그렇게 쉬운 일이 아닙니다. EmployeeVm viewmodel에서 볼 수 있듯이 사용자 정의 valueinjector를 구현하는 것도 불가능합니다.
this.InjectFrom<Employee>(employee);
누군가가이 구현을 도와 주거나 다른 해결책이 있다면 감사하겠습니다.
또한 시청자에게는 thanx입니다.
고맙습니다. 완벽하게 잘 작동했습니다. 천재 ... –