0

I am trying to implement generic repository pattern using LINQ to SQL data context. The context is coming as NULL. What change need to be done in order to get the context and make it working?

I have a dbml file which has the following:

public partial class LibraryManagementClassesDataContext : System.Data.Linq.DataContext

It has Account entity

CODE

static void Main(string[] args)
{

        RepositoryLayer.Repository<RepositoryLayer.Account> selectedRepository = new RepositoryLayer.Repository<RepositoryLayer.Account>();
        AccountBusiness accountBl = new AccountBusiness(selectedRepository);
        List<RepositoryLayer.Account> accountList =   accountBl.GetAllAccounts();

}


namespace RepositoryLayer
{
public interface IRepository<T> where T : class
{
    System.Linq.IQueryable<T> GetAll();
}

public class Repository<T> : IRepository<T> where T : class
{
    public System.Data.Linq.DataContext Context
    {
        get;
        set;
    }

    public virtual System.Linq.IQueryable<T> GetAll()
    {
        if (Context == null)
        {
            throw new Exception("Context is null");
        }
        return Context.GetTable<T>();
    }

}
}

public class AccountBusiness
{
    //IRepository<T>
    RepositoryLayer.IRepository<RepositoryLayer.Account> accountRepository;
    public AccountBusiness(RepositoryLayer.IRepository<RepositoryLayer.Account> repo)
    {
        accountRepository = repo;
    }

    public List<RepositoryLayer.Account> GetAllAccounts()
    {
        IQueryable<RepositoryLayer.Account> acc = accountRepository.GetAll();
        return acc.ToList();
    }

}

READING:

  1. LinqToSql declare and instantiate DataContext best practice?
Community
  • 1
  • 1
LCJ
  • 22,196
  • 67
  • 260
  • 418

2 Answers2

1

Of course it's null: you never assign a value to your Context property. Just do it:

using(var context = new LibraryManagementClassesDataContext())
{
    RepositoryLayer.Repository<RepositoryLayer.Account> selectedRepository = new RepositoryLayer.Repository<RepositoryLayer.Account>();
    selectedRepository.Context = context;
    AccountBusiness accountBl = new AccountBusiness(selectedRepository);
    List<RepositoryLayer.Account> accountList =   accountBl.GetAllAccounts();
}

I also suggest Repository.DataContext to be of LibraryManagementClassesDataContext type.

Ivo
  • 8,172
  • 5
  • 27
  • 42
  • Thanks. But I am getting following two errors - 1) type used in a using statement must be implicitly convertible to 'System.IDisposable' .... 2) Cannot implicitly convert type 'LINQTest1.DataContext' to 'System.Data.Linq.DataContext'. Note: I have a dbml file which has the following: public partial class LibraryManagementClassesDataContext : System.Data.Linq.DataContext – LCJ Jun 20 '12 at 07:49
0

As the context has not been instantiated you can create it within your class constructor. Something like this...

public class Repository<T> : IRepository<T> where T : class
{
    public Repository()
    {
        this.Context = new System.Data.Linq.DataContext()
    }
}

You can also keep your context short lived and dispose of it early. Read more about disposing of it early here: In LINQ-SQL, wrap the DataContext is an using statement - pros cons

public virtual System.Linq.IQueryable<T> GetAll()
{
    using (var context == new System.Data.Linq.DataContext())
    {
        return Context.GetTable<T>();
    }
}
Community
  • 1
  • 1
Kane
  • 16,471
  • 11
  • 61
  • 86
  • 1
    You forgot to dispose the data context – Ivo Jun 20 '12 at 06:29
  • Yep, added another example showing how to dispose of it correctly and a link to another SO question about disposing of the data context. – Kane Jun 20 '12 at 06:31
  • If you do it that way, you will end retrieving a Table that is not connected to any datacontext (as it has been disposed), resulting in an exception. Also, using one datacontext for each query is a bad practice. – Ivo Jun 20 '12 at 06:33