I have a class defined as follows;
public abstract class Repository<TEntity, TDataContext> : DisposableBaseClass
where TEntity : class
where TDataContext : DataContext, new()
{...contains Linq to SQL related functionality
In the concrete sub-class I define the types as so;
public class ConcreteRepo : Repository<LSTableClass, LSDataContext>
At the next tier up, I have business objects which hold the Repository object as a private variable.
This was fine;
private ConcreteRepo _repository;
However, I then refactored this out into a parent class for all the business objects - this parent class holds the repository/ implements Dispose pattern to dispose of repository etc.
My problem is that I just can't get the syntax right for the declaration of the variable.
The closest I have come is;
protected Repository<Object, DataContext> _repository;
but this gives the compile error:
"Error 1 'System.Data.Linq.DataContext' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'TDataContext' in the generic type or method '....Repository'..."
I've tried various other things but hit other problems.
In the business layer object inheriting this abstract class I am creating and using the _repository variable with a cast;
(Repository<LSTableClass, LSDataContext>)_repository = new ConcreteRepo();
- and I think this will be fine, assuming I can get this declaration right in the parent.
If I can't get this to work I have to declare the _repository in each business object, with the full/ concrete type details, and implement the dispose pattern in each one to clear up. Not the end of the world, but I'd like to not have to.