Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Asp.net MVC DI

345 views

Published on

Asp.net MVC DI

Published in: Technology
  • Login to see the comments

  • Be the first to like this

Asp.net MVC DI

  1. 1. ASP.NET MVC DI Jason
  2. 2. Dependency injection (DI) ● 提高可維護性 ● 建立寬鬆耦合性 ● 增加可測試性 ● 平行開發 (program to interface)
  3. 3. Example public class DocumentPrinter { public void PrintDocument(string documentName) { var repository = new DocumentRepository(); var formatter = new DocumentFormatter(); var printer = new Printer(); var document = repository.GetDocumentByName(documentName); var formattedDocument = formatter.Format(document); printer.Print(formattedDocument); } } var document = new DocumentPrint(); documentPrinter.PrintDocument(@”C:xxx.doc”);
  4. 4. Contructor DI public class DocumentPrinter { private DocumentRepository _repository; private DocumentFormatter _formatter; private Printer _printer; public DocumentPrinter(DocumentRepository repository, DocumentFormatter formatter, Printer printer) { _repository = repository; _formatter = formatter; _printer = printer; } public void PrintDocument(string documentName) { var document = _repository.GetDocumentByName(documentName); var formattedDocument = _formatter.Format(document); _printer.Print(formattedDocument); } }
  5. 5. Contructor DI var repository = new DocumentRepository(); var formatter = new DocumentFormatter(); var printer = new Printer(); var documentPrinter = new DocumentPrinter(repository, formatter, printer); documentPrinter.PrintDocument(@”C:xxx.doc”);
  6. 6. DI - Interface
  7. 7. DI - Interface public class DocumentPrinter { private IDocumentRepository _repository; private IDocumentFormatter _formatter; private IPrinter _printer; public DocumentPrinter(IDocumentRepository repository, IDocumentFormatter formatter, IPrinter printer) { _repository = repository; _formatter = formatter; _printer = printer; } public void PrintDocument(string documentName) { var document = _repository.GetDocumentByName(documentName); var formattedDocument = _formatter.Format(document); _printer.Print(formattedDocument); } }
  8. 8. DI - Interface var repository = new FilesystemDocumentRepository(); var formatter = new DocumentFormatter(); var printer = new Printer(); var documentPrinter = new DocumentPrinter(repository, formatter, printer); documentPrinter.PrintDocument(@”C:xxx.doc”); OR var repository = new DatabaseDocumentRepository(); var formatter = new DocumentFormatter(); var printer = new Printer(); var documentPrinter = new DocumentPrinter(repository, formatter, printer); documentPrinter.PrintDocument(”xxx.doc”);
  9. 9. DI Container Ex. StructureMap、Castle、Windsor、Ninject、Autofac and Unity using StructureMap: var container = new Container(x => { x.For<IDocumentRepository>().Use<DocumentRepository>(); x.For<IDocumentFormatter>().Use<DocumentFormatter>(); x.For<IPrinter>().Use<Printer>(); }); var documentPrinter = container.GetInstance<DocumentPrinter>(); documentPrinter.PrintDocument(@”C:xxx.doc”);
  10. 10. DI - ASP.NET MVC Controller 不應該執行: 直接進行資料庫存取 直接和檔案系統溝通 直接傳送 e-mail 直接呼叫 web service
  11. 11. DI - ASP.NET MVC ● Controller factory ● Dependency resolver
  12. 12. DI - Controller Factory public interface IMessageProvider { string GetMessage(); } public class EnglishMessageProvider : IMessageProvider { public string GetMessage() { return "Hello!"; } }
  13. 13. DI - Controller Factory HomeController.cs public class HomeController : Controller { private IMessageProvider _messageProvider; public HomeController( IMessageProvider messageProvider ) { _messageProvider = messageProvider; } public ActionResult Index() { ViewBag.Message = _messageProvider.GetMessage(); return View(); } }
  14. 14. DI - Controller Factory StructureMapControllerFactory.cs public class StructureMapControllerFactory: DefaultControllerFactory { protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType) { if (controllerType == null) { throw new HttpException(404, "Controller not found"); } var container = new Container(x => { x.For<IMessageProvider>().Use<EnglishMessageProvider>(); }); return container.GetInstance(controllerType) as IController; } }
  15. 15. DI - Controller Factory Global.asax.cs protected void Application_Start() { ControllerBuilder.Current.SetControllerFactory(new StructureMapControllerFactory()); }
  16. 16. DI - Dependency Resolver ● IDependencyResolver ● DependencyResolver
  17. 17. DI - Dependency Resolver Create ControllerDependency Resolver Create Controller DefaultController Activator Activator CreateInstance DI Container Create Instance NO Yes
  18. 18. DI - Dependency Resolver public class StructureMapDependencyResolver : IDependencyResolver { public object GetService(Type serviceType) { var container = new Container(x => { x.For<IMessageProvider>().Use<EnglishMessageProvider>(); }); var instance = container.TryGetInstance(serviceType); if (instance == null && !serviceType.IsAbstract && !serviceType.IsInterface) { instance = container.GetInstance(serviceType); } return instance; } }
  19. 19. DI - Dependency Resolver Global.asax.cs protected void Application_Start() { DependencyResolver.SetResolver(new StructureMapDependencyResolver()); }
  20. 20. Reference ASP.NET MVC4 http://www.books.com.tw/products/0010589490 StructureMap http://structuremap.github.io/quickstart/

×