c# - Dependency Injection Container -
i have data access layer library make "portable". reason portable because want work sql azure & azure file storage (eg, data + pdf reports) sql server 2008r2 , file system storage on concrete server.
based on spec system supposed go live later implementation (sql + file system storage), while upon meeting scalability threshold plan on moving azure.
the data access class use implements idataprovider interface (which built) , defines methods data access concrete implementation should have. consuming data access layer done via passing idataprovider interface , calling methods on eg:
public interface idataprovider { public bool dosomething(); } public class azuredataprovider : idataprovider { private string connectionstring; public azuredataprovider(string connectionstring) { this.connectionstring = connectionstring; } public azuredataprovider():this( configurationmanager.connectionstring["conn"].connectionstring) { } public bool dosomething() { return false; } }
so issue consumer class call methods on idataprovider interface has following:
public class dataaccessconsumer { public void someoperation() { azuredataprovider azureprovider = new azuredataprovider(); idataprovider dataprovider = (idataprovider)azureprovider; bool result = dataprovider.dosomething(); } }
so issue above code client still has have knowledge of concrete azuredataprovider class. want have method of providing client idataprovider interface, without passing connection string every interface method, or looking connection string within every method via configurationmanager.
is possible? abstract factory or sort of dependency injection container pattern trick? if so, appreciate code samples, or links code samples.
firstly, azuredataprovider has dependency on configurationmanager. should injected in instead.
secondly, dataprovider should injected dataaccessconsumer.
this means realistic application have dependency injection throughout, no dependency on container, need "wiring" - connecting dependencies together. pain - use dependencyinjectioncontainer @ main entry point resolve wiring. (this allows use more convenient declarative approach rather imperative approach, because can ask container "get me dataaccessconsumer", , dependency injection framework figure out dependencies you.
my favorite dependency injection framework c# ninject2.
Comments
Post a Comment