Skip to content

Instantly share code, notes, and snippets.

@puryfury
Created May 17, 2013 12:03
Show Gist options
  • Select an option

  • Save puryfury/5598628 to your computer and use it in GitHub Desktop.

Select an option

Save puryfury/5598628 to your computer and use it in GitHub Desktop.
Nancy Ninject Bootstrapper DAO Inject Example
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