Unity (c # ioc framework) usage summary,

Source: Internet
Author: User

Unity (c # ioc framework) usage summary,

Unity here refers to unity that completes dependency injection rather than the game engine.

Spring.net was used to complete dependency injection in the original project, but spring.net has not been maintained for a long time. Therefore, Unity officially launched by Microsoft has become one of the best candidates to replace spring.net.

Simple console demo:

 

First, create a new console application, search for unity and install nuget.

Using System; using Unity. attributes; namespace ConsoleApp1 {public class Index {[Dependency] public virtual IUserService UserService {set; get;} public static void Main (string [] args) {var container = new UnityContainer (); container. registerType <IUserService, UserService> (); // register IUserService userService = container. resolve <IUserService> (); // get userService. hello () ;}} public interface IUserService {void Hello () ;}class UserService: IUserService {public void Hello () {Console. writeLine ("hello ");}}}

 

Use unity in asp.net mvc:

1. Create an asp.net mvc application and install unity. mvc in nuget.

2. Create a new class library BLL and IBLL. The directory is as follows:

3. BLL creates UserService and IBLL creates IUserService to implement the Hello method.

namespace IBLL{    public interface IUserService    {        string Hello();    }}
using IBLL;namespace BLL{    public class UserService:IUserService    {        public string Hello()        {            return "hello";        }    }}

4: Fill in the configuration in Global. asax. cs. Example:

Using System. web. mvc; using System. web. optimization; using System. web. routing; using BLL; using IBLL; using Unity. aspNet. mvc; namespace WebApp {public class MvcApplication: System. web. httpApplication {protected void Application_Start () {AreaRegistration. registerAllAreas (); FilterConfig. registerGlobalFilters (GlobalFilters. filters); RouteConfig. registerRoutes (RouteTable. routes); BundleConfig. registerBundles (BundleTable. bundles); var container = BuildUnityContainer (); // customize the BuildUnityContainer method to obtain the container DependencyResolver. setResolver (new UnityDependencyResolver (container);} IUnityContainer BuildUnityContainer () {UnityContainer container = new UnityContainer (); container. registerType <IUserService, UserService> (); return container ;}}}

 

5. Obtain the UserService class in Control and use

Using System. web. mvc; using IBLL; namespace WebApp. controllers {public class HomeController: Controller {private IUserService UserService; public HomeController (IUserService userService) {this. userService = userService; // use constructor injection} public ActionResult Index () {string str = UserService. hello (); return Content (str );}}}

This completes a simple mvc demo.

 

Other such as the use of configuration files, the use of other methods of injection and other detailed operations, you can refer to this blog: https://www.cnblogs.com/qqlin/archive/2012/10/18/2720828.html

Unity official documentation: https://msdn.microsoft.com/en-us/library/ff649564.aspx

 

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.