1

I'm trying to make a universal 3-tier application with generic repositories for any data provider/orm. I just rewrote the code and got stuck on the error of Object reference not set to an instance of an object while trying make action in DbSet. I'm completely confused.. at least nudge me in the right direction.

Repository.cs

 public class Repository<T> : IRepository<T> where T : class
{

    public IUnitOfWork UnitOfWork { get; set; }
    private IDbSet<T> _objectset;

    private IDbSet<T> DbSet
    {
        get { return _objectset ?? (_objectset = UnitOfWork.Context.Set<T>()); }
    }
public List<T> GetAll()
    {
        try
        {
            return DbSet.ToList();
        }
        catch (Exception)
        {
            throw new Exception("Error in repository while attempts get all elements");
        }
    }
}

EFUnitOfWork.cs

 public class EfUnitOfWork : IUnitOfWork
{
    public EfUnitOfWork()
    {
        Context = new AppContext();
    }

    public DbContext Context { get; set; }

    public void Commit()
    {
        Context.SaveChanges();
    }

    public bool LazyLoadingEnabled
    {
        get { return Context.Configuration.LazyLoadingEnabled; }
        set { Context.Configuration.LazyLoadingEnabled = value; }
    }

    public void Dispose()
    {
        Context.Dispose();
    }
}

AppContext.cs

 public class AppContext : DbContext, IDbContext
{
    public AppContext()
    {
        Database.SetInitializer(new MigrateDatabaseToLatestVersion<AppContext,            Configuration>());
        Configuration.ProxyCreationEnabled = false;
    }

    public DbSet<Logic> Logics { get; set; }

    public DbSet<Category> Categories { get; set; }

    public new IDbSet<TEntity> Set<TEntity>() where TEntity : class
    {
        return base.Set<TEntity>();
    }
}

IDbContext.cs

public interface IDbContext
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
    void Dispose();
}

Configuration.cs

class Configuration : DbMigrationsConfiguration<AppContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = true;
        AutomaticMigrationDataLossAllowed = true;
    }

    protected override void Seed(AppContext context)
    {

    }
}
5
  • How do you set property UnitOfWork in your IRepository object? Commented May 9, 2013 at 15:40
  • IUnitOfWork UnitOfWork { get; set; } Commented May 9, 2013 at 15:41
  • Which line of code is throwing the exception? Commented May 9, 2013 at 15:41
  • DbSet.ToList(); or any action with DbSet in Repository.cs. In locals _objectset - null, DbSet - NullRegExp, UnitOfWork - null. Commented May 9, 2013 at 15:44
  • @MaksMartynov i see the property but i don't see how it gets value? Later in your DbSet property you are calling UnitOfWork.Context which i suspect causes null reference exception if UnitOfWork is null Commented May 9, 2013 at 15:44

1 Answer 1

1

I suggest you change the Repository to expect IUnitOfWork in the constructor as it is not valid without it.

public class Repository<T> : IRepository<T> where T : class
{
    private readonly IUnitOfWork _unitOfWork;
    public Repository(IUnitOfWork unitOfWork;)
    {
        _unitOfWork = unitOfWork;
    }
    public IUnitOfWork UnitOfWork { get { return _unitOfWork; } }
}

IDbContext should be defined as implementing IDisposable (and remove the Dispose method from the interface definition)

public interface IDbContext : IDisposable
{
    IDbSet<TEntity> Set<TEntity>() where TEntity : class;
    int SaveChanges();
}

The following line of code from AppContext will have the effect of disabling lazy loading because lazy loading is implemented by proxies

Configuration.ProxyCreationEnabled = false;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.