Created
May 17, 2013 12:03
-
-
Save puryfury/5598628 to your computer and use it in GitHub Desktop.
Nancy Ninject Bootstrapper DAO Inject Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using System; | |
| using System.Collections.Generic; | |
| using System.Linq; | |
| using System.IO; | |
| using System.Reflection; | |
| using System.Text; | |
| using System.Web; | |
| using CustomerWanso.Defined; | |
| using CustomerWanso.Secutiry; | |
| using Nancy; | |
| using Nancy.Extensions; | |
| using Nancy.Bootstrappers.Ninject; | |
| using Nancy.ViewEngines; | |
| using Nancy.ErrorHandling; | |
| using SquishIt.Framework; | |
| using SquishIt.Framework.CSS; | |
| using SquishIt.Framework.JavaScript; | |
| namespace CustomerWanso | |
| { | |
| public class Startup : NinjectNancyBootstrapper | |
| { | |
| /// <summary> | |
| /// 각 요청시마다 컨테이너 초기화 | |
| /// </summary> | |
| /// <param name="container"></param> | |
| /// <param name="context"></param> | |
| protected override void ConfigureRequestContainer(Ninject.IKernel container, NancyContext context) | |
| { | |
| base.ConfigureRequestContainer(container, context); | |
| //모든 DAO 인젝션 | |
| var daos = from type in Assembly.GetExecutingAssembly().GetTypes() | |
| where type.IsPublic && type.Namespace.Equals("CustomerWanso.DataAccess", StringComparison.OrdinalIgnoreCase) | |
| select type; | |
| //인터페이스 추출 | |
| var inters = from type in daos | |
| where type.IsInterface | |
| select type; | |
| //각 인터페이스와 구현을 매칭하여 종속성 주입 실시 | |
| foreach (Type inter in inters) | |
| { | |
| Type impl = daos.SingleOrDefault(t => inter.IsAssignableFrom(t) && t.IsClass && !t.IsAbstract); | |
| if (impl == null) continue; | |
| container.Bind(inter).To(impl).InSingletonScope();//당연히 싱글톤으로 | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment