Browse Source

Merge branch 'develop'

Exilon 3 years ago
parent
commit
7c4d1dda94
3 changed files with 61 additions and 9 deletions
  1. 3 3
      Quick.Commons.pas
  2. 52 0
      Quick.IOC.pas
  3. 6 6
      Quick.Pooling.pas

+ 3 - 3
Quick.Commons.pas

@@ -7,7 +7,7 @@
   Author      : Kike Pérez
   Author      : Kike Pérez
   Version     : 2.0
   Version     : 2.0
   Created     : 14/07/2017
   Created     : 14/07/2017
-  Modified    : 02/06/2021
+  Modified    : 22/08/2021
 
 
   This file is part of QuickLib: https://github.com/exilon/QuickLib
   This file is part of QuickLib: https://github.com/exilon/QuickLib
 
 
@@ -87,9 +87,9 @@ const
   LOG_ALL = [etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException];
   LOG_ALL = [etInfo, etSuccess, etWarning, etError, etDebug, etDone, etTrace, etCritical, etException];
   LOG_DEBUG = [etInfo,etSuccess,etWarning,etError,etDebug];
   LOG_DEBUG = [etInfo,etSuccess,etWarning,etError,etDebug];
   {$IFDEF DELPHIXE7_UP}
   {$IFDEF DELPHIXE7_UP}
-  EventStr : array of string = ['INFO','SUCC','WARN','ERROR','DEBUG','TRACE'];
+  EventStr : array of string = ['INFO','SUCC','WARN','ERROR','DEBUG','DONE','TRACE','CRITICAL','EXCEPTION'];
   {$ELSE}
   {$ELSE}
-  EventStr : array[0..5] of string = ('INFO','SUCC','WARN','ERROR','DEBUG','TRACE');
+  EventStr : array[0..8] of string = ('INFO','SUCC','WARN','ERROR','DEBUG','DONE','TRACE','CRITICAL','EXCEPTION');
   {$ENDIF}
   {$ENDIF}
 type
 type
   TPasswordComplexity = set of (pfIncludeNumbers,pfIncludeSigns);
   TPasswordComplexity = set of (pfIncludeNumbers,pfIncludeSigns);

+ 52 - 0
Quick.IOC.pas

@@ -157,6 +157,28 @@ type
     procedure DoInvoke(Method: TRttiMethod;  const Args: TArray<TValue>; out Result: TValue);
     procedure DoInvoke(Method: TRttiMethod;  const Args: TArray<TValue>; out Result: TValue);
   end;
   end;
 
 
+  IFactory<T> = interface
+  ['{92D7AB4F-4C0A-4069-A821-B057E193DE65}']
+    function New : T;
+  end;
+
+  TSimpleFactory<T : class, constructor> = class(TInterfacedObject,IFactory<T>)
+  private
+    fResolver : TIocResolver;
+  public
+    constructor Create(aResolver : TIocResolver);
+    function New : T;
+  end;
+
+  TSimpleFactory<TInterface : IInterface; TImplementation : class, constructor> = class(TInterfacedObject,IFactory<TInterface>)
+  private
+    fResolver : TIocResolver;
+  public
+    constructor Create(aResolver : TIocResolver);
+    function New : TInterface;
+  end;
+
+
   TIocContainer = class(TInterfacedObject,IIocContainer)
   TIocContainer = class(TInterfacedObject,IIocContainer)
   private
   private
     fRegistrator : TIocRegistrator;
     fRegistrator : TIocRegistrator;
@@ -187,6 +209,7 @@ type
     function AbstractFactory<T : class, constructor>(aClass : TClass) : T; overload;
     function AbstractFactory<T : class, constructor>(aClass : TClass) : T; overload;
     function AbstractFactory<T : class, constructor> : T; overload;
     function AbstractFactory<T : class, constructor> : T; overload;
     function RegisterTypedFactory<TFactoryInterface : IInterface; TFactoryType : class, constructor>(const aName : string = '') : TIocRegistration<TTypedFactory<TFactoryType>>;
     function RegisterTypedFactory<TFactoryInterface : IInterface; TFactoryType : class, constructor>(const aName : string = '') : TIocRegistration<TTypedFactory<TFactoryType>>;
+    function RegisterSimpleFactory<TInterface : IInterface; TImplementation : class, constructor>(const aName : string = '') : TIocRegistration;
     procedure Build;
     procedure Build;
   end;
   end;
 
 
@@ -385,6 +408,11 @@ begin
   Result := Self.RegisterOptions<T>(options);
   Result := Self.RegisterOptions<T>(options);
 end;
 end;
 
 
+function TIocContainer.RegisterSimpleFactory<TInterface, TImplementation>(const aName: string): TIocRegistration;
+begin
+  Result := fRegistrator.RegisterInstance<IFactory<TInterface>>(TSimpleFactory<TInterface,TImplementation>.Create(fResolver),aName).AsSingleton;
+end;
+
 function TIocContainer.Resolve(aServiceType: PTypeInfo; const aName: string): TValue;
 function TIocContainer.Resolve(aServiceType: PTypeInfo; const aName: string): TValue;
 begin
 begin
   Result := fResolver.Resolve(aServiceType,aName);
   Result := fResolver.Resolve(aServiceType,aName);
@@ -744,4 +772,28 @@ begin
   if Result then aService := GlobalContainer.Resolve<T>;
   if Result then aService := GlobalContainer.Resolve<T>;
 end;
 end;
 
 
+{ TSimpleFactory<T> }
+
+constructor TSimpleFactory<T>.Create(aResolver: TIocResolver);
+begin
+  fResolver := aResolver;
+end;
+
+function TSimpleFactory<T>.New: T;
+begin
+  Result := fResolver.CreateInstance(TClass(T)).AsType<T>;
+end;
+
+{ TSimpleFactory<TInterface, TImplementation> }
+
+constructor TSimpleFactory<TInterface, TImplementation>.Create(aResolver: TIocResolver);
+begin
+  fResolver := aResolver;
+end;
+
+function TSimpleFactory<TInterface, TImplementation>.New: TInterface;
+begin
+  Result := fResolver.CreateInstance(TClass(TImplementation)).AsType<TInterface>;
+end;
+
 end.
 end.

+ 6 - 6
Quick.Pooling.pas

@@ -195,7 +195,7 @@ var
 begin
 begin
   Result := nil;
   Result := nil;
   {$IFDEF DEBUG_OBJPOOL}
   {$IFDEF DEBUG_OBJPOOL}
-  TDebugger.Trace('Waiting for get idle Pool Item...');
+  TDebugger.Trace(Self,'Waiting for get idle Pool Item...');
   {$ENDIF}
   {$ENDIF}
   waitResult := fSemaphore.WaitFor(fWaitTimeoutMs);
   waitResult := fSemaphore.WaitFor(fWaitTimeoutMs);
   if waitResult <> TWaitResult.wrSignaled then raise Exception.Create('Connection Pool Timeout: Cannot obtain a connection');
   if waitResult <> TWaitResult.wrSignaled then raise Exception.Create('Connection Pool Timeout: Cannot obtain a connection');
@@ -208,7 +208,7 @@ begin
       begin
       begin
         fPool[i] := TPoolItem<T>.Create(fSemaphore,fLock,i,fDelegate);
         fPool[i] := TPoolItem<T>.Create(fSemaphore,fLock,i,fDelegate);
         {$IFDEF DEBUG_OBJPOOL}
         {$IFDEF DEBUG_OBJPOOL}
-        TDebugger.Trace('Create Pool Item: %d',[i]);
+        TDebugger.Trace(Self,'Create Pool Item: %d',[i]);
         {$ENDIF}
         {$ENDIF}
         Exit(fPool[i]);
         Exit(fPool[i]);
       end;
       end;
@@ -216,13 +216,13 @@ begin
       begin
       begin
         //writeln('get ' + i.ToString);
         //writeln('get ' + i.ToString);
         {$IFDEF DEBUG_OBJPOOL}
         {$IFDEF DEBUG_OBJPOOL}
-        TDebugger.Trace('Get Idle Pool Item: %d',[i]);
+        TDebugger.Trace(Self,'Get Idle Pool Item: %d',[i]);
         {$ENDIF}
         {$ENDIF}
         Exit(fPool[i]);
         Exit(fPool[i]);
       end
       end
       {$IFDEF DEBUG_OBJPOOL}
       {$IFDEF DEBUG_OBJPOOL}
       else
       else
-      TDebugger.Trace('Pool Item: %d is busy (RefCount: %d)',[i,fPool[i].RefCount]);
+      TDebugger.Trace(Self,'Pool Item: %d is busy (RefCount: %d)',[i,fPool[i].RefCount]);
       {$ENDIF}
       {$ENDIF}
     end;
     end;
   finally
   finally
@@ -283,7 +283,7 @@ function TPoolItem<T>._AddRef: Integer;
 begin
 begin
   fLock.Enter;
   fLock.Enter;
   {$IFDEF DEBUG_OBJPOOL}
   {$IFDEF DEBUG_OBJPOOL}
-  TDebugger.Trace('Got Pool item');
+  TDebugger.Trace(Self,'Got Pool item');
   {$ENDIF}
   {$ENDIF}
   try
   try
     Inc(FRefCount);
     Inc(FRefCount);
@@ -297,7 +297,7 @@ function TPoolItem<T>._Release: Integer;
 begin
 begin
   fLock.Enter;
   fLock.Enter;
   {$IFDEF DEBUG_OBJPOOL}
   {$IFDEF DEBUG_OBJPOOL}
-  TDebugger.Trace('Released Pool item');
+  TDebugger.Trace(Self,'Released Pool item');
   {$ENDIF}
   {$ENDIF}
   try
   try
     Dec(fRefCount);
     Dec(fRefCount);