2014-01-29 48 views
0

我已經實現了通用存儲庫模式和UnitOfWork,但是當我嘗試填充模型類的新實例時,在我的控制器中收到空引用異常。控制器中的存儲庫模式空引用異常

這裏是我的倉庫模式:

class GenericRepository<TEntity> where TEntity : class 
{ 
    internal PropmetEntities context; 
    internal DbSet<TEntity> dbSet; 

    public GenericRepository(PropmetEntities context) 
    { 
     this.context = context; 
     this.dbSet = context.Set<TEntity>(); 
    } 

    public virtual IEnumerable<TEntity> Get() 
    { 
     IQueryable<TEntity> query = dbSet; 
     return query.ToList(); 
    } 

    public virtual TEntity GetByID(object id) 
    { 
     return dbSet.Find(id); 
    } 

    public virtual void Insert(TEntity entity) 
    { 
     dbSet.Add(entity); 
    } 

    public virtual void Delete(object id) 
    { 
     TEntity entityToDelete = dbSet.Find(id); 
     Delete(entityToDelete); 
    } 

    public virtual void Delete(TEntity entityToDelete) 
    { 
     if (context.Entry(entityToDelete).State == EntityState.Detached) 
     { 
      dbSet.Attach(entityToDelete); 
     } 
     dbSet.Remove(entityToDelete); 
    } 

    public virtual void Update(TEntity entityToUpdate) 
    { 
     dbSet.Attach(entityToUpdate); 
     context.Entry(entityToUpdate).State = EntityState.Modified; 
    } 
} 

這是我工作的單位:

class UnitOfWork 
{ 
    private PropmetEntities context = new PropmetEntities(); 
    private GenericRepository<User> userRepository; 

    public GenericRepository<User> UserRepository 
    { 
     get 
     { 
      if (this.userRepository == null) 
       this.userRepository = new GenericRepository<User>(context); 
      return userRepository; 
     } 
    } 
} 

這是我的模型類:

public class UserGridModel 
{ 
    public int ID { get; set; } 
    public string Username {get; set;} 
    public string EMail { get; set; } 
    public string Surname { get; set; } 
    public string Role { get; set; } 
    public ContactInformation ContactInformation { get; set; } 
    public Role Role1 { get; set; } 
} 

這是我的控制器操作:

private UnitOfWork.UnitOfWork unitOfWork = new UnitOfWork.UnitOfWork(); 

    [PrivilegeFilter(priv = Privileges.UserView)] 
    public ActionResult Grid(GridSettings set) 
    { 
     var userList = from user in unitOfWork.UserRepository.Get() select user; 
     var users = new List<UserGridModel>(); 
     if (userList.Any()) 
     { 
      foreach (var user in userList) 
      { 
       users.Add(new UserGridModel() 
       { 
        ID = user.ID, 
        Username = user.Username, 
        EMail = user.ContactInformation.EMail, 
        Surname = user.ContactInformation.Surname, 
        Role = user.Role1.Description 
       }); 



      } 
     } 

     GridModel grid = new GridModel(set, users.Count()); 
     grid.rows = users.Skip((set.PageIndex - 1) * set.PageSize).Take(set.PageSize).ToList(); 
     return Json(grid, JsonRequestBehavior.AllowGet); 

    } 

我這裏得到一個空引用異常:

users.Add(new UserGridModel() 
      { 
       ID = user.ID, 
       Username = user.Username, 
       EMail = user.ContactInformation.EMail, 
       Surname = user.ContactInformation.Surname, 
       Role = user.Role1.Description 
      }); 
+0

您可能擁有user.ContactInformation == null。 –

+0

不要。創建。通用。庫。 – Phill

+0

@Phill你能解釋一下爲什麼不這樣做。創建。通用。存儲庫? – pravprab

回答

1

你有聯繫信息給用戶嗎?我的猜測是user.ContactInformation爲null,並且在獲取user.ContactInformation.Email時出錯。

+0

我在DataBase中的'ContactInformation'字段是空的。創建新用戶時,我只是將它們設置爲必填字段,以便它們不能爲空。 – Hydro

0

可能的原因是 user.ContactInformation == nulluser.Role1 == null。您應該在UserGridModel創建代碼中添加一些空值檢查。

0

你可以null檢查做這樣的事情:

foreach (var user in userList.Where(x => x.ContactInformation != null && x.Role1 != null)) 
{ 
    users.Add(new UserGridModel() 
    { 
     ID = user.ID, 
     Username = user.Username, 
     EMail = user.ContactInformation.EMail, 
     Surname = user.ContactInformation.Surname, 
     Role = user.Role1.Description 
    }); 
} 

這至少會阻止你獲得空引用異常當這些屬性之一是null