Asp. Net MVC Autofac initial use 1, mvcautofac

Source: Internet
Author: User

Asp. Net MVC Autofac initial use 1, mvcautofac

Autofac is one of the most popular IOC frameworks in the. NET field. It is said that it is the fastest:

Advantages:

Through NuGet in VS to load AutoFac, either way, the ultimate goal is to reference the Autofac. dll and Autofac. Configuration. dll assembly to your project.

Demo1 Structure

 

Data access interface IDAL

    public interface IDAL    {        string Insert(string commandText);    }

Implement IDAL interfaces in SQL and Oracle Modes

public class SqlDAL : IDAL    {        public string Insert(string commandText)        {            return commandText + "-------use sqlserver...";        }    } public class OracalDAL : IDAL    {        public string Insert(string commandText)        {            return commandText+"-------use oracle...";        }    }

Then define DBManager constructor Injection

 public class DBManager    {        IDAL _dal;        public DBManager(IDAL dal)        {            _dal = dal;        }        public string Insert(string commandText)        {            return _dal.Insert(commandText);        }    }

Finally, we need to truly complete dependency injection relying on AtuoFac

1.RegisterType <Object> (). As <Iobject> ()

# Region builder. registerType <Object> (). as <Iobject> () var builder = new ContainerBuilder (); builder. registerType <DBManager> (); builder. registerType <SqlDAL> (). as <IDAL> (); // zookeeper builder. registerType <Object> (). as <Iobject> (): The instance SqlDAL registration completed using (var container = builder) of the Registration type and its instance registration interface IDAL. build () {var manager = container. resolve <DBManager> (); // zookeeper parses the instance of an interface. ViewBag. SqlResult = manager. Insert ("insert into test values ('test1', 'shanghai')");} # endregion

Running effect:

2.RegisterType <Object> (). Named <Iobject> (string name)

# Region builder. registerType <Object> (). named <Iobject> (string name) var builder = new ContainerBuilder (); builder. registerType <SqlDAL> (). named <IDAL> ("SQL"); builder. registerType <OracleDAL> (). named <IDAL> ("oracle"); // register different instances for an interface. Sometimes it is inevitable that multiple classes map to the same interface. For example, SqlDAL and OracleDAL both implement the IDAL interface. In order to accurately obtain the desired type, you must name it at registration. Using (var container = builder. build () {// var manager = (SqlDAL) container. resolveNamed <IDAL> ("SQL"); var manager = container. resolveNamed <IDAL> ("oracle"); ViewBag. sqlResult = manager. insert ("insert into test values ('test1', 'shanghai')");} # endregion

Running effect:

3.Builder. RegisterType <Object> (). Keyed <Iobject> (Enum enum)

 public enum DBType    {        Sql,        Oracle    }
# Region builder. registerType <Object> (). keyed <Iobject> (Enum enum) var builder = new ContainerBuilder (); // register different instances for an interface in enumeration mode. Sometimes we use enumeration to differentiate different implementations of an interface, rather than string builder. registerType <SqlDAL> (). keyed <IDAL> (DBType. SQL ). instancePerDependency (); // zookeeper extends InstancePerDependency () is used to control the object lifecycle. Each time an instance is loaded, a new instance is created. By default, builder is used. registerType <OracleDAL> (). keyed <IDAL> (DBType. oracle ). singleInstance (); // ↑ SingleInstance () is used to control the object lifecycle. Every time an instance is loaded, the same instance using (var container = builder is returned. build () {var manager = container. resolveKeyed <IDAL> (DBType. SQL); // var manager = (OracleDAL) container. resolveKeyed <IDAL> (DBType. oracle); ViewBag. sqlResult = manager. insert ("insert into test values ('test1', 'shanghai')");} # endregion

Running effect:

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.