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