2013-12-16 22 views
2

我有一個viewmodel,它使用valueinjector從模型(我也實現了TPT繼承)注入值。在此過程中,由於我的自定義屬性(屬性不是在模型中源)中的一個,我不斷收到以下錯誤:視圖模型中的定製屬性在TPT繼承項目中使用valueinjector

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

但是,它不是那麼容易在我的情況,我已經在我的基類和派生類繼承的一個類。並且爲了實現自定義valueinjector也不可能,因爲您可以從EmployeeVm viewmodel中看到它。

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。該匹配用於返回源和目標屬性是否匹配,並且useSourceProp用於忽略SourceProperty,以便它不映射到目標屬性。

在您的方案中,source屬性沒有全名屬性,並且關於匹配,您不必知道該名稱不匹配就不會映射該屬性。

錯誤是由於

LastName.ToUpper() 

哪個試圖分配值之前的姓氏屬性轉換爲大寫。因此,如果您在valueinjector設置值之後在構造函數中設置值,則應該解決問題。

+0

非常感謝。它工作得很好。天才 ... –