2014-12-28 111 views
0

我正在编写一个MVC 5互联网应用程序,并且在初始化Lazy对象时遇到了一些麻烦。延迟初始化 - 无效参数

这里是我的代码:

public Lazy<IGenericRepository<Account>> accounts; 
public IGenericRepository<Account> accountsNotLazy; 

我想在构造函数中调用类初始化这两个变量。

这是构造函数代码;

public GenericMultipleRepository(DbContext dbContext) 
{ 
    this.dbContext = dbContext; 
    accounts = new Lazy<GenericRepository<Account>>(dbContext); 
    accountsNotLazy = new GenericRepository<Account>(dbContext); 
} 

我可以请这个代码帮忙吗?

在此先感谢。

编辑

我想完全相同的初始化的变量accountsNotLazy,但使用Lazy loading。变量accountsNotLazy正确初始化,accounts怎么没有?唯一的区别是Lazy关键字。

这些都是错误:

The best overloaded method match for 'System.Lazy>.Lazy(System.Func>)' has some invalid arguments

除了:

cannot convert from 'System.Data.Entity.DbContext' to 'System.Func>'

这里是GenericRepository类:

public class GenericRepository<TEntity> : IGenericRepository<TEntity> where TEntity : class 
{ 
    protected DbSet<TEntity> DbSet; 

    private readonly DbContext _dbContext; 

    public GenericRepository(DbContext dbContext) 
    { 
     _dbContext = dbContext; 
     DbSet = _dbContext.Set<TEntity>(); 
    } 

    public GenericRepository() 
    { 
    } 

    public IQueryable<TEntity> GetAll() 
    { 
     return DbSet; 
    } 

    public async Task<TEntity> GetByIdAsync(int id) 
    { 
     return await DbSet.FindAsync(id); 
    } 

    public IQueryable<TEntity> SearchFor(Expression<Func<TEntity, bool>> predicate) 
    { 
     return DbSet.Where(predicate); 
    } 

    public async Task EditAsync(TEntity entity) 
    { 
     _dbContext.Entry(entity).State = EntityState.Modified; 
     await _dbContext.SaveChangesAsync(); 
    } 

    public async Task InsertAsync(TEntity entity) 
    { 

     DbSet.Add(entity); 
     await _dbContext.SaveChangesAsync(); 
    } 

    public async Task DeleteAsync(TEntity entity) 
    { 
     DbSet.Remove(entity); 
     await _dbContext.SaveChangesAsync(); 
    } 
} 
+2

很确定你不能传递'dbContext'到类似的构造函数中。它需要'Func ' – DavidG

+0

代码无法如图所示进行编译 - 所以这段代码没有办法使运行时“参数超出范围”异常。请检查帖子中的代码是否是您遇到问题的代码。 –

回答

1

正如许多人所提到的你需要通过一个Func<T>,但是建议作为答案的表达方式不正确。

初始化您accounts变量如下 -

accounts = new Lazy<IGenericRepository<Account>>(() => new GenericRepository<Account>(dbContext)); 

请注意,当你要访问您的GenericRepository 懒洋洋地的情况下,你需要访问Value财产Lazy类的..像这样 - accounts.Value ,这将是类型GenericRepository<Account>

+0

我收到以下错误:无法将类型'System.Lazy >'隐式转换为'System.Lazy > ' – user3736648

+0

好的..你在变量的声明中有所不同.. vs你正在创建的'Lazy'的实例..我最初是由后者去的,只修正了'Func' ..现在我已经更新了泛型类型参数也被传递,即从'GenericRepository'到'IGenericRepository' –

0

Lazy<T>的构造需要Func<T>类型的参数。您可能要改为试试这个:

accounts = new Lazy<GenericRepository<Account>>(() => { return dbContext; }); 
+0

“参数超出范围异常” - 是运行时异常,这只是意味着在帖子中的代码不显示问题OP已... –

+0

@AlexeiLevenkov我同意,但我不能看到代码发布将编译给定的任何可用的构造函数。 – DavidG

+0

在OP编辑问题之后,这确实是一个答案。 –

0

Lazy<T>类的构造函数是

Lazy<T>(Func<T>) 

您可以GenericMultipleRepository构造改变如下:

public GenericMultipleRepository(DbContext dbContext) 
{ 
    this.dbContext = dbContext; 
    accounts = new Lazy<GenericRepository<Account>>(() => { return dbContext; }); 
    accountsNotLazy = new GenericRepository<Account>(dbContext); 
} 
+0

我的代码出现这个错误:无法将lambda表达式转换为键入'bool',因为它不是委托类型。 – user3736648