View on GitHub

SARAFF SOFTWARE

All products / Saraff.IoC

Migration from Saraff.IoCX

Saraff.IoCX (netstandard2.0) is superseded by Saraff.IoC 2.x, which ships both net40 and netstandard2.0 assemblies under the Saraff.IoC package ID.

Package replacement

dotnet remove package Saraff.IoCX
dotnet add package Saraff.IoC
<!-- Before -->
<PackageReference Include="Saraff.IoCX" Version="1.x" />

<!-- After -->
<PackageReference Include="Saraff.IoC" Version="2.0.0" />

The namespace Saraff.IoC and the ServiceContainer type are unchanged. Existing code that uses Bind, Load, CreateInstance and [BindService] continues to work.

flowchart LR
    Old["Saraff.IoCX 1.x<br/>netstandard2.0"] --> New["Saraff.IoC 2.x<br/>net40 + netstandard2.0"]
    New --> Legacy["Legacy API<br/>Bind / Load / CreateInstance"]
    New --> Modern["Modern API<br/>Register / Scopes / Async"]

What changed in 2.0

Added

Changed

Breaking

API mapping

1.x / Saraff.IoCX 2.x
Bind<IService, Service>() Register<IService, Service>() (POCO friendly) or keep Bind
Bind<IService>(instance) RegisterInstance<IService>(instance) or keep Bind
Load(assembly) Load(assembly) or explicit Register calls
CreateInstance<T>() CreateInstance<T>() (still available)
Singleton only Singleton, Scoped, Transient, custom IServiceLifetime
No scopes CreateScope() / CreateNestedContainer()
IConfiguration with all members IConfiguration + configuration groups
Listener returns object Listener returns InvocationResult
No async startup IAsyncInitializable, ResolveAsync (netstandard2.0)
No async disposal DisposeAsync (netstandard2.0)

Migration steps

  1. Replace the package reference.
  2. Recompile. Fix any code that implemented the old IConfiguration shape.
  3. Replace listener implementations that returned object with InvocationResult.
  4. Move new code to Register and scopes.
  5. Keep Bind for existing Component-based code; migrate gradually.
  6. If you target netstandard2.0, use DisposeAsync when services hold async resources.

Listener migration

Before:

public object OnInvoking(MethodBase method, object instance, object[] parameters) {
    if(method.Name == "MethodA") {
        return "short";
    }
    return null; // null means "not handled"
}

public object OnInvoked(MethodBase method, object instance, object result) {
    return result;
}

public Exception OnCatch(MethodBase method, object instance, Exception ex) {
    return null; // null means "not handled"
}

After:

public InvocationResult OnInvoking(MethodBase method, object instance, object[] parameters) {
    if(method.Name == "MethodA") {
        return InvocationResult.FromResult("short");
    }
    return InvocationResult.NotHandled;
}

public InvocationResult OnInvoked(MethodBase method, object instance, object result) {
    return InvocationResult.NotHandled;
}

public InvocationResult OnCatch(MethodBase method, object instance, Exception ex) {
    return InvocationResult.NotHandled;
}

The new result type removes the “null means not handled” ambiguity: a listener can now explicitly return null as a handled result.

Configuration migration

Old code implemented IConfiguration with all properties. New code can:

var configuration = new ServiceContainerConfiguration {
    ServiceRequiredAttributeType = typeof(MyInjectAttribute),
    ListenerType = typeof(IMyListener)
};

See Configuration.

Compatibility notes

See also