Skip to main content

Class AbstractFactory<T>

Provides a generic factory implementation for creating instances of type T using a delegate-based approach.

Namespace: Workspace.XBR.Xperiflow.Utilities

Assembly: Xperiflow.dll

Declaration
public class AbstractFactory<T> : IAbstractFactory<T>

Examples

Basic usage with simple constructor:

// Create a factory for StringBuilder instances
var stringBuilderFactory = new AbstractFactory(
() => new StringBuilder(capacity: 1000)
);

// Use the factory to create instances
var sb1 = stringBuilderFactory.Create();
var sb2 = stringBuilderFactory.Create();

// Each instance is independent
sb1.Append("Hello");
sb2.Append("World");

Advanced usage with dependency injection:

// Create a factory for complex objects with dependencies
var repositoryFactory = new AbstractFactory(
() => new UserRepository(
connectionString: configuration.GetConnectionString("UserDb"),
logger: serviceProvider.GetService(),
cache: serviceProvider.GetService()
)
);

// Create instances as needed
var userRepo = repositoryFactory.Create();
var users = await userRepo.GetAllUsersAsync();

Usage with XBR-specific patterns:

// Create a factory for artifact IO operations
var dataTableFactory = new AbstractFactory>(
() => new DataTableParquetArtifactIoFactory(sessionInfo)
);

// Use in data processing workflows
var artifactIoFactory = dataTableFactory.Create();
var reader = artifactIoFactory.CreateReader();
var dataTable = await reader.ReadAsync(artifactPath);

Remarks

The Workspace.XBR.Xperiflow.Utilities.AbstractFactory%601 class implements the Workspace.XBR.Xperiflow.Utilities.IAbstractFactory%601 interface using a delegate-based approach for maximum flexibility. This implementation allows you to specify the creation logic as a System.Func%601 delegate, enabling support for various creation patterns including simple constructors, complex initialization sequences, and dependency injection.

Key Features:

  • Delegate-Based CreationUses Func for flexible object creation logic

  • Lazy EvaluationObjects are only created when Create() is called

  • Thread SafetyThread-safe as long as the provided delegate is thread-safe

  • Exception PropagationExceptions from the delegate are propagated to the caller

Performance Considerations:

The factory delegate is invoked every time Create() is called, so ensure that the delegate is efficient if the factory will be used frequently. For expensive operations, consider implementing caching or singleton patterns within the delegate.

Implements: Workspace.XBR.Xperiflow.Utilities.IAbstractFactory<{T}>

Methods

Create()

Creates and returns a new instance of type T using the configured factory delegate.

Declaration
public T Create()
Remarks

This method invokes the factory delegate that was provided during construction. Each call to this method results in a new invocation of the delegate, typically producing a new instance of the target type.

Exception Handling:

Any exceptions thrown by the factory delegate are propagated to the caller. This allows for appropriate error handling at the point of use while maintaining the flexibility of the delegate-based approach.

Returns

<T>

A new instance of type T created by invoking the factory delegate

Exceptions

System.Exception Any exception thrown by the factory delegate is propagated to the caller

Implements

  • Workspace.XBR.Xperiflow.Utilities.IAbstractFactory<{T}>

Inherited Members

  • System.Object.Equals(System.Object)
  • System.Object.Equals(System.Object,System.Object)
  • System.Object.GetHashCode
  • System.Object.GetType
  • System.Object.MemberwiseClone
  • System.Object.ReferenceEquals(System.Object,System.Object)
  • System.Object.ToString

Was this page helpful?