I have a repository class with optional dependency:
class MyRepository : BaseRepository, IMyRepository
{
public MyRepository(IDataContext dataContext, IC
For 'nice to have' dependencies you should use property injection instead of ctor injection. Config would look something like this:
public class MyRepository
{
public ICacheProvider Cache { get; set; }
}
container.RegisterType(new InjectionProperty("Cache", typeof(ICacheProvider)));
That would inject an implementation of ICacheProvider into a property named Cache of your MyRepository. As you would have to implement null checks wherever you make a call to the Cache property inside your repository class I would go with @dtryon's proposal and implement a NullCacheProvider. That is far more convenient and less error prone.