The principle of dependency injection is not said here, the dependency injection framework is more, such as: Castle, Unity, Ninject, AUTOFAC, StructureMap, spring.net and so on. Recently used AUTOFAC in the project, take this opportunity to make some summary, also by the way to discuss with you. AUTOFAC mainly uses the Autofac.dll,automapper.dll.
As a simple example, there is an application database that is SQL Server that will not be ruled out in the future using Oracle or other databases. Then we are in the development of the time to consider, in the future to change the database is not to be re-development? In general, we would like to interact with the database in the interface, and then use SQL Server, Oracle related classes to implement the interface, in the program initialization according to the database to instantiate the corresponding interface. Paste the code directly:
Idatabase interface
Public Interface Idatabase { stringget;} void Select (string commandtext); void Insert (string commandtext); void Update (string commandtext); void Delete (string commandtext); }
View Code
SQLDatabase class
Public classSqldatabase:idatabase { Public stringName {Get{return "SQL Server"; } } Public voidSelect (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a query for SQL in {1}!", CommandText, Name)); } Public voidInsert (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a inserts SQL in {1}!", CommandText, Name)); } Public voidUpdate (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a update for SQL in {1}!", CommandText, Name)); } Public voidDelete (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a delete sql in {1}!", CommandText, Name)); } }
View Code
Oracledatabase class
Public classOracledatabase:idatabase { Public stringName {Get{return "Oracle"; } } Public voidSelect (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a query for SQL in {1}!", CommandText, Name)); } Public voidInsert (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a inserts SQL in {1}!", CommandText, Name)); } Public voidUpdate (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a update for SQL in {1}!", CommandText, Name)); } Public voidDelete (stringCommandText) {Console.WriteLine (string. Format ("' {0} ' is a delete sql in {1}!", CommandText, Name)); } }
View Code
In this case, the management class of database is added, and the implementation class of Idatabase is avoided by the client directly.
Public classDatabasemanager {idatabase _database; PublicDatabasemanager (idatabase database) {_database=database; } Public voidSearch (stringCommandText) {_database. Select (CommandText); } Public voidADD (stringCommandText) {_database. Insert (CommandText); } Public voidSave (stringCommandText) {_database. Update (CommandText); } Public voidRemove (stringCommandText) {_database. Delete (CommandText); } }
View Code
Below, let's look at the use of AUTOFAC:
varBuilder =NewContainerbuilder (); Builder. Registertype<DatabaseManager>(); //Builder. Registertype<sqldatabase> (). As<idatabase> ();Builder. Registertype<oracledatabase> (). As<idatabase>(); using(varcontainer =Builder. Build ()) {varManager = Container. Resolve<databasemanager>(); Manager. Search ("SELECT * FORM USER"); }
View Code
If you are using a SQL Server database, you will be builder. Registertype<oracledatabase> (). As<idatabase> () changed to builder. Registertype<sqldatabase> (). As<idatabase> (), the other is unchanged.
However, some people will ask, I am directly new object is not easier? Indeed, if there is only one implementation class, it might be easier to use new. However, an application usually has a lot of implementation classes, and if you use new, it will undoubtedly increase the difficulty of maintenance.
Can I not modify the code? The answer is yes. You can configure it with a configuration file and add a configuration below:
<configSections> <section name="autofac" type= " Autofac.Configuration.SectionHandler, autofac.configuration"/> </configSections> <autofac defaultassembly="autofacdemo"> <components> <component type="autofacdemo.sqldatabase, Autofacdemo" service= " Autofacdemo.idatabase "/> </components> </autofac>
View Code
The code for the client implementation is as follows:
varBuilder =NewContainerbuilder (); Builder. Registertype<DatabaseManager>(); Builder. Registermodule (NewConfigurationsettingsreader ("AUTOFAC")); using(varcontainer =Builder. Build ()) {varManager = Container. Resolve<databasemanager>(); Manager. Search ("SELECT * FORM USER"); }
View Code
You can also get the same results.
This is just a simple use of AUTOFAC. There are thousands of implementations for an application, and if you are registering and acquiring the implementation class each time you use it, it doesn't make much sense to use dependency injection. In fact, we can do a public component, in the program initialization when the registration, need to use the time to get out, so it is very convenient.
Enginecontext.initialize (false) is called when the program is initialized and can then be used directly like enginecontext.current.resolve<idatabase> ().
Http://files.cnblogs.com/files/dengwenbo/AutofacInfrastructure.rar
Simple use of AUTOFAC