0

dapper로 ID 구현을 맞춤 설정하려면 MVC5-Dapper-Identity repo를 따라야합니다. 내 문제는 ioc 컨테이너에 의존성을 등록 할 때이 예제는 Ninject를 사용하지만 Simple Injector를 사용합니다. 이 예는 내가에 대한 예외를 생성하는 간단한 인젝터와 노력 belowIdentity 맞춤 구현 Simple Injector로

kernel.Bind<IConnectionFactory>().To<SqlConnectionFactory>() 
    .WithConstructorArgument("connectionString", 
     ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString); 
kernel.Bind<IUserRepository>().To<UserRepository>(); 
kernel.Bind<IUserStore<User>>().To<UserRepository>(); 
kernel.Bind<IUserLoginStore<User>>().To<UserRepository>(); 
kernel.Bind<IUserPasswordStore<User>>().To<UserRepository>(); 
kernel.Bind<IUserSecurityStampStore<User>>().To<UserRepository>(); 
kernel.Bind(typeof(UserManager<>)).ToSelf(); 

그들의 dependacies 등록 나

container.Register<IUserRepository, UserRepository>(); 
container.Register<IUserStore<User>, UserRepository>(); 
container.Register<IUserLoginStore<User>, UserRepository>(); 
container.Register<IUserPasswordStore<User>, UserRepository>(); 
container.Register<IUserSecurityStampStore<User>, UserRepository>(); 
container.Register(typeof(UserManager<User>)); 

간단한 인젝터와 위의 구현을 수행하는 방법에

업데이트

@Ric .Net 답변 주셔서 감사하지만 구현 속 테이 예외

enter image description here

+0

예외에서 제공된 설명서 링크를 읽었습니까? – Steven

+0

오, 내 정보에 대한 감사합니다. – Gayan

답변

3

나를 위해 예외를 생성?

메시지와 스택 추적을 제공하면 질문을 이해하는 것이 좋습니다. 이 경우 나는 대답 할 수 있다고 생각합니다. 예외를 알지 못해서 나는 확신 할 수없고 단지 추측 할 수 없습니다 ...

IConnectionFactory에 대한 등록 누락과 Ninject에 대한 등록이 모두 반드시 필요한 것은 아닙니다. 내가 본 것에서 나온 것이 아닙니다.

마찬가지로 linked GitHub repo에서 볼 수있는 것처럼 응용 프로그램 코드 (MVC 컨트롤러)의 유일한 종속은 UserManager<User>입니다.

var accountController = 
    new AccountController(
     new UserManager(
      new UserRepository(
       new SqlConnectionFactory(
        connectionString)))); 

그래서 당신은 단지 (떨어져 MVC 컨트롤러에서 당연히)에 대한 등록을해야합니다 다음과 같이

개체 그래프는 이렇게 보이는

  • UserManager => UserManager
  • IUserStore => UserRepository
  • IConnectionFactory => SqlConnectionFactory

다른 등록은 현재 필요하지 않습니다.

구현을 보면 Dapper.Identity의 클래스는 일반 SQL 문을 사용하며 상태가 없습니다. 따라서 모든 Dapper.Identity 구성 요소의 수명은 Singleton 일 수 있습니다. Asp.Net Identity의 UserManager의 경우 Singleton 일 수 있는지 확실하지 않습니다.

var connectionFactory = new SqlConnectionFactory(connectionString); 
container.RegisterSingleton<IConnectionFactory>(connectionFactory); 
container.RegisterSingleton<IUserStore<User>, UserRepository>(); 
container.Register<UserManager<User>>(); 

간단한 인젝터 .WithConstructorArgument 유창 API 호출에 대한 아웃 - 오브 - 박스를 지원하지 않지만, 구현 어쨌든 Singleton이 될 수 있기 때문에이 문제가되지 않습니다 :

필요한 등록은 아예 컴포지션 루트에 인스턴스를 만들고 Simple Injector에이 인스턴스를 저장하게합니다.

실제로 다른 인터페이스를 어딘가에 주입해야하는 경우 Simple Injector에 등록하는 방법은 설명서에서 here으로 설명되어 있습니다.

+0

답변 주셔서 감사합니다. 업데이트 된 질문을 참조하십시오. – Gayan

+0

UserManager가 'Lifestyle.Scoped'로 등록되어야한다고 생각합니다. – Steven

관련 문제