2013-12-16 2 views
2

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입니다.

답변

3

이 시도 :

을 EmployeeVm의 생성자에서 :

public EmployeeVm(Employee employee) 
{ 
    this.InjectFrom<Employee>(employee); 

    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(FirstName); 
    this.FullName = stringBuilder.ToString(); 
} 

변환하면 FullName 속성을 자동 속성 :

또한
public string FullName { get; set; } 

가에 직원에 재정의 방법을 변경 :

protected override bool Match(ConventionInfo c) 
{ 
    var isMatch = (c.SourceProp.Name == "PersonId" && c.TargetProp.Name == "EmployeeId") ||(c.SourceProp.Name == c.TargetProp.Name && c.SourceProp.Type == c.TargetProp.Type); 

     return isMatch; 

} 

LoopValueInjector의 match 속성이나 useSourceProp을 재정의 할 필요가 없습니다. 일치는 source 및 target 속성이 일치하는지 여부를 반환하기위한 것이고 useSourceProp은 대상 속성에 매핑되지 않도록 SourceProperty를 무시하기위한 것입니다.

시나리오에서 원본 속성에는 전체 이름 속성이없고 일치하는 경우 이름이 일치하지 않는 것처럼 말할 필요가 없습니다. 속성이 매핑되지 않습니다.

오류는 값을 지정하기 전에 상단에 성 속성을 변환하려고

LastName.ToUpper() 

인해

했다. valueinjector가 값을 설정 한 후에 생성자에서 값을 설정하면 문제가 해결되어야합니다.

+0

고맙습니다. 완벽하게 잘 작동했습니다. 천재 ... –

관련 문제