2

現在のloggedonuserをサービスレイヤーに挿入するのに問題があります。コードキャンプサーバーに似たものを試していますが、コードが機能しない理由を理解するのに苦労しています...

私のアプリ: UI レイヤー -> ドメイン サービスに接続 -> レポ レイヤーに接続...

レポはUIに接続されていません。すべてがチェックされ、検証され、DomainServiceレイヤーから返されます...

私のコード:

//これは私のドメインサービス内で宣言されています

public interface IUserSession
{
    UserDTO GetCurrentUser();
}

私のWebアプリケーション内で、このサービスを実装してからサービスレイヤーに挿入したいので(これが私が立ち往生している場所です):

public class UserSession : IUserSession
{
    //private IAuthorizationService _auth;
    public UserSession()//IAuthorizationService _auth)
    {
        //this._auth = _auth;
    }

    public UserDTO GetCurrentUser()
    {
        var identity = HttpContext.Current.User.Identity;
        if (!identity.IsAuthenticated)
        {
            return null;
        }
        return null;
        //return _auth.GetLoggedOnUser(identity.Name);
    }


}

私がやりたいことは、認証サービスからloggedonuserを取得することですが、うまくいかなかったため、コードをスタブしました...

global.asax 内のすべてを次のようにバインドします。

protected override void OnApplicationStarted()
    {
        AreaRegistration.RegisterAllAreas();
        // hand over control to NInject to register all controllers
        RegisterRoutes(RouteTable.Routes);
        Container.Get<ILoggingService>().Info("Application started");
       //here is the binding...
        Container.Bind<IUserSession>().To<UserSession>();
    }

まず、IUserSession を使用するサービスを使用しようとすると例外が発生します。コントローラー x にデフォルトのパラメーターなしのコンストラクターを提供してくださいと表示されますが、ドメイン サービスから参照を削除するとすべてが機能します...

サービス部門:

 private IReadOnlyRepository _repo;
    private IUserSession _session;
    public ActivityService(IReadOnlyRepository repo, IUserSession _session)
    {
      this._repo = repo;
      this._session = _session;
     }

これを実装するためのより良い方法/より簡単な方法はありますか?

以下の返信の助けを借りてUPATEして、なんとかこれを成し遂げました。

https://gist.github.com/1042173

4

1 に答える 1

0

OnApplicationStarted をオーバーライドしたので、NinjectHttpApplication を使用していると思いますか? そうでない場合は、コントローラーをNinjectに登録するので、そうする必要があります。それが起こっていない場合は、表示されているエラーが発生する可能性があります。

私のアプリケーションでこれを行った方法は次のとおりです。

コントローラーベース:

    [Inject]
    public IMembershipProvider Membership { get; set; }

    public Member CurrentMember
    {
        get { return Membership.GetCurrentUser() as Member; }
    }

IMembershipProvider:

    public Member GetCurrentUser()
    {
        if ( string.IsNullOrEmpty( authentication.CurrentUserName ) )
            return null;

        if ( _currentUser == null )
        {
            _currentUser = repository.GetByName( authentication.CurrentUserName );
        }
        return _currentUser;
    }
    private Member _currentUser; 

IAuthenticationProvider:

    public string CurrentUserName
    {
        get
        {
            var context = HttpContext.Current;
            if ( context != null && context.User != null && context.User.Identity != null )
                return HttpContext.Current.User.Identity.Name;
            else
                return null;
        }
    }

これが意味をなさない場合はお知らせください。

于 2011-06-16T13:42:24.190 に答える