Please consider this interface:
public interface IInitialiazableEntity<TRepository> where TRepository : class, IRepository
{
void Initialize(TRepository repository);
}
This class (snippet):
public class SomeFacade<TRepository> where TRepository : class, IRepository
{
public void Add<TEntity>(TEntity entity) where TEntity : AbstractEntity
{
try
{
if (entity is IInitialiazableEntity<TRepository>)
(entity as IInitialiazableEntity<TRepository>).Initialize(this.Repository);
}
catch (Exception ex)
{
this.Logger.Error(ex);
}
}
}
And the entity:
public class Student : AbstractEntity, IInitialiazableEntity<IRepository>
{
void Initialize(IRepository repository) { ... }
}
Since the student is only IInitialiazableEntity<IRepository>
and the facade will have an actual repository which is more specialized than the basic IRepository
(i.e. it will be IMySpecialRepository : IRepository
), will the is
keyword realize that it can cast the IMySpecialRepository
and pass it to the Initialize
method of the entity? Or if not, how to do it?