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: