logo资料库

Mastering Ninject for Dependency Injection.pdf

第1页 / 共142页
第2页 / 共142页
第3页 / 共142页
第4页 / 共142页
第5页 / 共142页
第6页 / 共142页
第7页 / 共142页
第8页 / 共142页
资料共142页,剩余部分请下载后查看
Cover
Copyright
Credits
About the Author
About the Reviewers
www.PacktPub.com
Table of Contents
Preface
Chapter 1: Understanding Dependency Injection
What is Dependency Injection?
DI or Inversion of Control (IoC)
How can DI help?
My First DI Application
DI Containers
Why use Ninject?
Summary
Chapter 2: Getting Started with Ninject
Hello Ninject!
It's all about Binding
Object Lifetime
Transient scope
Singleton scope
Thread scope
Request scope
Custom scope
Ninject modules
XML configuration
How to use XML configuration
Convention over configuration
Selecting the assemblies
Selecting the components
Filtering the selected components
Explicit inclusion and exclusion
Selecting service types
Configuring the Bindings
Summary
Chapter 3: Meeting Real-world Requirements
DI patterns and antipatterns
Constructor Injection
Initializer methods and properties
Service Locator
Multi binding and contextual binding
Implementing the plugin model
Contextual binding
Named binding
Resolving metadata
Attribute-based binding
Target-based conditions
Generic helper
Custom providers
Activation context
The Factory Method
Dynamic factories
The Shape Factory example
Using convention
Selecting service types
Defining Binding Generator
Telecom Switch example
Custom Instance Providers
Func
Lazy
Summary
Chapter 4: Ninject in Action
Windows Forms applications
WPF and Silverlight applications
ASP.NET MVC applications
Validator injection
Filter injection
Conditional filtering (When)
Contextual arguments (With)
WCF applications
ASP.NET Web Forms applications
Summary
Chapter 5: Doing More with Extensions
Interception
Setup Interception
Member Interception
Type Interception
Multiple Interceptors
InterceptAttribute
Mocking Kernel
Extending Ninject
Summary
Index
Mastering Ninject for Dependency Injection Learn how Ninject facilitates the implementation of Dependency Injection to solve common design problems of real-life applications Daniel Baharestani BIRMINGHAM - MUMBAI
Mastering Ninject for Dependency Injection Copyright © 2013 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: September 2013 Production Reference: 1190913 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.. ISBN 978-1-78216-620-7 www.packtpub.com Cover Image by Daniel Baharestani (baharestani@gmail.com) and Sheetal Aute (sheetala@packtpub.com)
Credits Author Daniel Baharestani Project Coordinator Romal Karani Reviewers Remo Gloor Daniel Allen Matt Duffield Ted Winslow Acquisition Editor Pramila Balan Commissioning Editor Nikhil Chinnari Technical Editors Iram Malik Krishnaveni Haridas Veena Pagare Proofreader Clyde Jenkins Indexer Monica Ajmera Graphics Ronak Dhruv Production Coordinator Conidon Miranda Cover Work Conidon Miranda
About the Author Daniel Baharestani is an experienced IT professional living in Australia. He has a BSc in software engineering and has over 10 years of professional experience in design and development of enterprise applications, mostly focused on Microsoft technologies. Daniel is currently working at 3P Learning, which is a global leader in online learning for school-aged students with its flagship platform, Mathletics—used by more than 3.5 million students in over 10,000 schools worldwide. A book is like a song, which may usually be referred to by its singer's name, whereas many people are involved in the background to make it happen. First, I am deeply indebted to my wife, Mona, who has taken all my responsibilities during this period. I understand how hard it was for her to do everything alone that we used to do together. My special thanks goes to Remo Gloor, the main developers of Ninject, who kindly accepted the final technical review of this book, and other technical reviewers, including Daniel Allen, Matt Duffield, and Ted Winslow for providing valuable feedback. I would also like to thank my manager, Houman, for being helpful and encouraging, and for understanding how important this book was to me. It would be much difficult to have it done without his support. Finally, I should acknowledge the whole Packt team, who gave me this opportunity and guided me through this process, including but definitely not limited to, Nikhil Chinnari and Yogesh Dalvi, my commissioning editors, Sneha Modi and Romal Karani, my project coordinators, and Shrutika Kalbag, the author relationship executive for opening a door.
About the Reviewers Remo Gloor has worked as a Software Architect at bbv Software Services AG in Switzerland for many years. During this time, he was using Ninject in several projects. At the beginning, he was a user of Ninject. Later, he contributed with several extensions. In 2010, he became manager and the main contributor to Ninject, which was developed originally by Nate Kohari and Ian Davis. Besides his interest in dependency injection and IoC containers, he has also a strong interest in service-oriented and message-driven architectures, as well as event sourcing. Because of this, he contributed to the ActiveMq support to NServiceBus. He blogs on http://www.planetgeek.ch/author/remo-gloor/ mainly about Ninject. He also answers many Ninject-related questions on stackoverflow: http://stackoverflow.com/users/448580/remo-gloor.
Daniel Allen is a Chicago-based developer who specializes in ASP.NET MVC 4 development and enterprise architecture design. He develops primarily in C#, JavaScript, and Objective-C. Because of his heavy focus on enterprise architecture design, Dan has experience in an array of patterns and tools that he has effectively and logically combined together to meet a project's unique needs. Dan holds a B.S. in Management Information Systems and an MBA with a concentration in Information Systems. Dan spends much of his free time working on development-related side contracts and searching for the next great startup idea. He aspires to start a consulting firm that will provide capital for the various startup ideas one day. For recreation, he enjoys training and competing in various marathons, and aspires to complete a full iron man competition one day. He has formerly worked with Millennium Information Services, Inc. as an ASP. NET MVC Web Developer. His primary tasks in this role were MVC 4 Razor development, HTML 5 frontend GUI design, enterprise architecture design, and WCF, Oracle database, and agile development. He has also worked for Arc Worldwide / Leo Burnett as an Associate Software Engineer. His primary tasks in this role were ASP.NET Web Forms development, frontend GUI design, and he also worked on SQL Server database. Dan has also worked with American Concrete Pavement Association as a Software Engineer. His primary tasks in this role were ASP.NET Web Forms and MVC 4 development, iOS mobile development, and SQL Server database, graphics and media development. For Dan's complete professional history and his online interactive portfolio, please visit http://www.apexwebz.com. I would like to thank my family for their ongoing support. My father inspired me to start working in this field, and now I can't picture myself doing anything else. I would also like to thank my close friend, past boss, and ongoing mentor, Robert Rodden, for helping me at every step of the way in my professional career.
Matt Duffield is a software architect, and has over 17 years of experience working in IT. He enjoys building a rich line of business applications that focus on great user experiences while providing excellent business intelligence, such as dashboards and expert systems. His current focus is on client-side MVC architecture and building cross-platform solutions. Matt is very active in the community, speaking at user groups and code camps. He is an INETA speaker and a Microsoft MVP in client development. He is the co-author of Microsoft Silverlight 5: Building Rich Enterprise Dashboards, Packt Publishing. His blog can be found at http://mattduffield. wordpress.com. You can follow him on Twitter at @mattduffield. Matt is also the leader of the Charlotte ALT.NET user group (http://www.meetup.com/ charlottealtnet/) and Charlotte Game Dev user group (http://www.meetup. com/Charlotte-Game-Dev/). He is also the Vice President of the Charlotte Enterprise Developers Guild (http://www.developersguild.org/) and also board member of the Carolina Code Camp. Ted Winslow has been one of those programmers who impressed the likes of NASA and Boeing with his skills behind a keyboard ever since his sixth grade. Even when he isn't working for one of the big names, he's freelancing for multimillion- dollar shops, and considers writing code a way to relax in his downtime. He started writing code while young and did it with little more than a basic starter book and a half-broken computer. Against all odds, he has now a lengthy and respected work history with code chops for which large and small companies hunger. Nowadays, he's spotted helping people in his free time to make sure the young programmers understand and have a chance to live their dream, even when the odds are stacked against them. I'd like to thank my friends for both the encouragement they've provided during my career and for putting up with me every day. You all mean a lot to me.
分享到:
收藏