Here's the scenario i am faced with:
public abstract class Record { }
public abstract class TableRecord : Record { }
public abstract class LookupTableRecord : TableRecord { }
public sealed class UserRecord : LookupTableRecord { }
public interface IDataAccessLayer<TRecord>
where TRecord : Record { }
public interface ITableDataAccessLayer<TTableRecord> : IDataAccessLayer<TTableRecord>
where TTableRecord : TableRecord { }
public interface ILookupTableDataAccessLayer<TLookupTableRecord> : ITableDataAccessLayer<TLookupTableRecord>
where TLookupTableRecord : LookupTableRecord { }
public abstract class DataAccessLayer<TRecord> : IDataAccessLayer<TRecord>
where TRecord : Record, new() { }
public abstract class TableDataAccessLayer<TTableRecord> : DataAccessLayer<TTableRecord>, ITableDataAccessLayer<TTableRecord>
where TTableRecord : TableRecord, new() { }
public abstract class LookupTableDataAccessLayer<TLookupTableRecord> : TableDataAccessLayer<TLookupTableRecord>, ILookupTableDataAccessLayer<TLookupTableRecord>
where TLookupTableRecord : LookupTableRecord, new() { }
public sealed class UserDataAccessLayer : LookupTableDataAccessLayer<UserRecord> { }
Now when i try to cast UserDataAccessLayer to it's generic base type ITableDataAccessLayer<TableRecord>
, the compiler complains that it cannot implicitly convert the type.
When i try and use the in
or out
keywords in the interface declaration for the generic parameters, the compiler complains about Invalid variance: The type parameter must be invariantly valid.
I have the following abstract class:
public abstract class FileProcessor : IDisposable
{
protected abstract ITableDataAccessLayer<TableRecord> CreateTableDataAccessLayer();
}
And a sample concrete implementation as follows:
public class UserFileProcessor : FileProcessor
{
protected override ITableDataAccessLayer<TableRecord> CreateTableDataAccessLayer()
{
return new UserDataAccessLayer();
}
}
return new UserDataAccessLayer(); is where the compiler is complaining.