ObjectFactory 1.0.0

dotnet add package ObjectFactory --version 1.0.0
                    
NuGet\Install-Package ObjectFactory -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ObjectFactory" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ObjectFactory" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="ObjectFactory" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ObjectFactory --version 1.0.0
                    
#r "nuget: ObjectFactory, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ObjectFactory@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ObjectFactory&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=ObjectFactory&version=1.0.0
                    
Install as a Cake Tool

ObjectFactory

Lightweight dependency injection for legacy code. Replace new with testable factories without major refactoring.

Why ObjectFactory?

Legacy code often has hard-coded dependencies that make testing impossible:

public class OrderProcessor
{
    public void Process(Order order)
    {
        var email = new EmailService(); // Can't test this!
        email.Send(order.Customer.Email, "Order confirmed");
    }
}

ObjectFactory lets you make code testable with minimal changes:

using static ObjectFactory.GlobalObjectFactory;

public class OrderProcessor
{
    public void Process(Order order)
    {
        var email = Create<IEmailService, EmailService>(); // Now testable!
        email.Send(order.Customer.Email, "Order confirmed");
    }
}

Installation

<PackageReference Include="ObjectFactory" Version="1.0.0" />

Usage

Basic Creation

using static ObjectFactory.GlobalObjectFactory;

// Create with interface mapping
var service = Create<IEmailService, EmailService>();

// Create with constructor parameters
var repo = Create<IRepository, SqlRepository>(connectionString);

// Create concrete class
var processor = Create<OrderProcessor>(config, logger);

Testing with Test Doubles

[Fact]
public void TestOrderProcessing()
{
    // Arrange
    var factory = ObjectFactory.ObjectFactory.Instance();
    factory.ClearAll(); // Clean state

    var mockEmail = new MockEmailService();
    factory.SetOne<IEmailService>(mockEmail);

    // Act
    var processor = new OrderProcessor();
    processor.Process(testOrder);

    // Assert
    Assert.True(mockEmail.WasCalled);

    // Cleanup
    factory.ClearAll();
}

SetOne vs SetAlways

// SetOne: Queued, used once then removed
factory.SetOne<IService>(mock1);
factory.SetOne<IService>(mock2);
var s1 = Create<IService>(); // Returns mock1
var s2 = Create<IService>(); // Returns mock2
var s3 = Create<IService>(); // Creates new instance

// SetAlways: Persistent, returned every time
factory.SetAlways<IService>(mock);
var s1 = Create<IService>(); // Returns mock
var s2 = Create<IService>(); // Returns mock (same instance)

Custom Factories

For advanced scenarios, inject custom factory functions:

factory.SetFactory<IService>(args => {
    // Custom logic to create instances
    var config = (Config)args[0];
    return config.UseProduction
        ? new ProductionService()
        : new TestService();
});

Integration with Testing Frameworks

ObjectFactory works with any .NET testing framework (xUnit, NUnit, MSTest). It also integrates seamlessly with SpecRec for automated testing workflows.

License

PolyForm Noncommercial License 1.0.0

Product Compatible and additional computed target framework versions.
.NET net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net9.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on ObjectFactory:

Package Downloads
SpecRec

SpecRec - Automated Legacy Testing Tools for .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 149 10/17/2025