2011-04-11 5 views
0

간단히 말해 사용자 유형을 가져 와서 ID를 얻은 다음 서비스 클래스를 사용하여 강력한 형식의 객체를 검색하는 사용자 정의 모델 바인더를 작성하려고합니다.AOP : Ninject를 사용한 사용자 정의 모델 바인더 속성

더 좋은 방법이 있다면 알려 주시기 바랍니다.

Elabaration가 :

내 DomainService 층 내의 모든 내 바인딩 Ninject에 설정을 가지고, 3 웹 UI 년대는 도메인 서비스 계층에 매여있다. 각 asp.net mvc 응용 프로그램은 kernal에 바인딩을로드합니다.

// 내 사용자 정의 모델 바인더

public class UserModelBinder : IModelBinder 
    { 
     private IAuthenticationService auth; 

     public UserModelBinder(IAuthenticationService _auth, EntityName type, 
     string loggedonuserid) 
     { 
      this.auth = _auth; 
      CurrentUserType = type; 
      CurrentUserId = loggedonuserid; 
     } 


     public EntityName CurrentUserType { get; private set; } 
     private string CurrentUserId { get; set; } 

     public object BindModel(ControllerContext controllerContext, 
     ModelBindingContext bindingContext) 
     { 
      object loggedonuser = null; 

      if (CurrentUserType == EntityName.Client) 
       loggedonuser = GetLoggedOnClientUser(CurrentUserId); 
      else if (CurrentUserType == EntityName.Shop) 
       loggedonuser = GetLoggedOnShopUser(CurrentUserId); 
      else 
       throw new NotImplementedException(); 

      return loggedonuser; 
     } 

     public ClientUser GetLoggedOnClientUser(string loggedonuserid) 
     { 
      var user = _auth.GetLoggedOnClientUser(loggedonuserid); 
      if (user == null) 
       throw new NoAccessException(); 

      return user; 
     } 

     public ShopUser GetLoggedOnShopUser(string loggedonuserid) 
     { 
      var user = _auth.GetLoggedOnShopUser(loggedonuserid); 
      if (user == null) 
       throw new NoAccessException(); 

      return user; 
     } 

    } 

내 Global.aspx.cs

// using NInject to override application started 
     protected override void OnApplicationStarted() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      // hand over control to NInject to register all controllers 
      RegisterRoutes(RouteTable.Routes); 



//how do I instantiate? 
      ModelBinders.Binders.Add(typeof(object), new 
      UserModelBinder(null,EntityName.Client, User.Identity.Name)); 

     } 

내 문제는 어떻게 수행 IAuthentication이 서비스는이 저장소와 같은 다른 것들에 연결되어입니다 실제로 이것을 올바르게 인스턴스화합니까? 새로운 NinjectModule을 생성해야합니까? 나는 이걸로 정말 혼란스러워서 어떤 도움을 주시면 대단히 감사하겠습니다. Container.Get()에서 전달하려고했습니다. -하지만 null입니다 ...

참고 : 제가 modelbinder를 만드는 이유는 모든 컨트롤러가 사용자의 유형에 사용자의 유형이 필요하기 때문에 어떤 유형의 사용자가 요청을하는지, 대부분의 방법이 내 서비스 계층은이 ShopUser 또는 ClientUser 또는 시스템의 다른 사용자에 대한 하나의 일을 할 것입니다 과부하 ...

편집해야합니다 : 을 나는 매우 easiy IAuthenticationService에 따라 내 컨트롤러 호출 내부와 종류를 얻을 수 사용자의 내 도메인 서비스 계층에 전달하여 관련 작업을 처리하지만 ModelBindings를 사용하여 어떻게 가능한지 알고 싶습니다.

Edit2 : ISomethingService 인스턴스를 호출하거나 바인딩하는/사용자 지정 특성이있는 AOP와 함께 사용자 지정 특성을 사용하는 작업 예제가 있습니까?

답변

0

여기서 서비스 탐지기 패턴을 사용할 수 있습니다. Ninject 컨테이너 (IKernel?)를 생성자에 전달하고 무언가를 바인딩해야 할 때마다 AuthenticationService를 확인하십시오.

이 세분화는 서비스를 해결하는 함수를 전달하는 생성자 인수 Func를 가질 수 있습니다. 이것은 더 명시 적이며 Ninject에 대한 종속성을 제거합니다. 이런 식으로 뭔가 :

public class MyModelBinder : IModelBinder 
{ 
    Func<IAuthenticationService> _resolveAuthService; 

    public MyModelBinder(Func<IAuthenticationService> resolveAuthService) 
    { 
     _resolveAuthService = resolveAuthService; 
    } 

    public override object Bind(Context c) 
    { 
     var authService = _resolveAuthService(); 

     authService.GetSomething(); 

     // etc... 
    } 
} 
+0

@rmac : 서비스 로케이터 패턴이 좋아 보인다, ... 나는 내 게시물의 제목을 변경하고 그러나 AOP 방식이 더 좋을 수도 약간 수정합니다, 죄송합니다! – Haroon

+0

MVC 버전 2의 제약 조건을 제거 했습니까? MVC 3은 인젝션을 훨씬 잘 지원하며 각 요청마다 새로운 모델 바인더 인스턴스를 해결할 방법이 있다고 생각합니다. 그것은 최적의 솔루션이 될 것입니다. – rmac

+0

나는 AOP로 무엇을하고 싶은지 상상하기가 어렵다. 내가 생각하기에 조금 더 정교하게 다룰 필요가있어. – rmac