我正在使用Repository模式,並在我的Repository.cs中,我的_dataContext對象返回null。我可以做什麼,以便它不會返回null?存儲庫模式中的NullReferenceException
private BlogDataContext _dataContext;
private readonly IDbSet<T> _dbset;
private string[] _paths;
public Repository(IDatabaseFactory databaseFactory)
{
DataContext.Database.Log = Console.Write;
DatabaseFactory = databaseFactory;
_dbset = DataContext.Set<T>();
}
protected IDatabaseFactory DatabaseFactory
{
get;
private set;
}
protected BlogDataContext DataContext
{
get { return _dataContext ?? (_dataContext = DatabaseFactory.Get()); }
}
是否拋出異常? –
是的,DataContext拋出一個異常類型的System.NullReferenceException –
您是否使用某種IOC框架? Ninject/Autofac /等。可能是需要設置爲注射的東西? – thsorens