0

Result<T>このような結果を返すためにメソッドでよく使用するジェネリックジェネリッククラスがあります

public Result<User> ValidateUser(string email, string password)

ILoggingServiceサービス注入をログに記録するためのクラスにインターフェイスがありますResultが、実際の実装を注入する方法が見つかりません。

以下のコードを実行しようとしましたが、TestLoggingServiceインスタンスがプロパティに注入されませんLoggingService。常に null を返します。それを解決する方法はありますか?

 using (var kernel = new StandardKernel())
            {               
                kernel.Bind<ILoggingService>().To<TestLoggingService>();
                var resultClass = new ResultClass();
                var exception = new Exception("Test exception");
                var testResult = new Result<ResultClass>(exception, "Testing exception", true);                
            }  


      public class Result<T>
        {

           [Inject]
           public ILoggingService LoggingService{ private get; set; } //Always get null


            protected T result = default(T);
            //Code skipped




            private void WriteToLog(string messageToLog, object resultToLog, Exception exceptionToLog)
            {

                LoggingService.Log(....); //Exception here, reference is null



        }
4

1 に答える 1

2

を使用してインスタンスを手動で作成していますnew。Ninjectは、によって作成されたオブジェクトのみを注入しますkernel.Get()。さらに、推奨されていない何かをDTOに注入しようとしているようです。結果を作成したクラスでログを記録することをお勧めします。

public class MyService
{
    public MyService(ILoggingService loggingService) { ... }

    public Result<T> CalculateResult<T>() 
    {
        Result<T> result = ...
        _loggingService.Log( ... );
        return result;
    }
}
于 2013-02-04T17:22:30.040 に答える