I found a few articles outlining the reason why the DependencyResolver in C# MVC should be avoided
But I also found a pattern of injecting a "base dependency" into a constructor, and that class essentially hold all instances the app's dependencies, but obviously not in a sigleton, each time the dependencies are instantiated anew
// example possibly to be avoided
public interface IBaseDependencies
{
IClientRepo ClientRepo { get; }
IProductRepo ProductRepo { get; }
/// more here
}
..but while I love the simplicity of passing dependencies in this way, the problem is ALL the depencies will be instantiated but they may not all be required, so to avoid that performce hit, I thought to add C#'s Lazy<>
class like so...
// Is this example any better?
public interface ILazyBaseDependencies
{
#region IClientRepo
private readonly Lazy<IClientRepo> ClientRepoLazy =
new Lazy<IClientRepo>(() => DependencyResolver.Current.GetService<IClientRepo>());
public IClientRepo ClientRepo => ClientRepoLazy.Value;
#endregion
/// more here
}
So now the dependency is only instantiated when it is called directly. The larger the project, the more the "base dependencies" approach becomes useful, but will it hurt performance to do this this way?