Skip to content

Commit

Permalink
relaxing dependency versions. Bumps to 1.13.2
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremydmiller committed Jan 15, 2024
1 parent 5a4cd7c commit e3ddc4f
Show file tree
Hide file tree
Showing 34 changed files with 367 additions and 349 deletions.
59 changes: 28 additions & 31 deletions docs/guide/compilation/frames/frame.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ a C# `using` block that surrounds the inner code:
```cs
public class NoArgCreationFrame : SyncFrame
{
public NoArgCreationFrame(Type concreteType)
public NoArgCreationFrame(Type concreteType)
{
// By creating the variable this way, we're
// marking the variable as having been created
Expand Down Expand Up @@ -80,7 +80,7 @@ public class NoArgCreationFrame : SyncFrame
}
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/NoArgConstructor.cs#L9-L42' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_noargcreationframe' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/NoArgConstructor.cs#L9-L44' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_noargcreationframe' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Creating a Variable within a Frame
Expand All @@ -99,7 +99,7 @@ public NoArgCreationFrame(Type concreteType)
Output = new Variable(concreteType, this);
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/NoArgConstructor.cs#L47-L55' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_noargcreationframector' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/NoArgConstructor.cs#L48-L56' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_noargcreationframector' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Otherwise, you could also have written that code like this:
Expand All @@ -115,7 +115,7 @@ public NoArgCreationFrame(Type concreteType)
Output = Create(concreteType);
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/NoArgConstructor.cs#L58-L66' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_noargcreationframector2' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/NoArgConstructor.cs#L59-L67' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_noargcreationframector2' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

## Finding Dependent Variables
Expand All @@ -131,56 +131,53 @@ public class GetInstanceFrame : SyncFrame, IResolverFrame
{
private static readonly MethodInfo _resolveMethod =
ReflectionHelper.GetMethod<Instance>(x => x.Resolve(null));



private Variable _scope;

private readonly string _name;

private Variable _scope;

public GetInstanceFrame(Instance instance)
{
Variable = new ServiceVariable(instance, this, ServiceDeclaration.ServiceType);

_name = instance.Name;
}

public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
{
writer.Write($"var {Variable.Usage} = {_scope.Usage}.{nameof(Scope.GetInstance)}<{Variable.VariableType.FullNameInCode()}>(\"{_name}\");");
Next?.GenerateCode(method, writer);
_name = instance.Name;
}

public override IEnumerable<Variable> FindVariables(IMethodVariables chain)
{
_scope = chain.FindVariable(typeof(Scope));
yield return _scope;
}

public ServiceVariable Variable { get; }

public void WriteExpressions(LambdaDefinition definition)
{
var scope = definition.Scope();
var expr = definition.ExpressionFor(Variable);

var instance = Variable.Instance;

var @call = Expression.Call(Expression.Constant(instance), _resolveMethod, scope);
var assign = Expression.Assign(expr, Expression.Convert(@call, Variable.VariableType));
var call = Expression.Call(Expression.Constant(instance), _resolveMethod, scope);
var assign = Expression.Assign(expr, Expression.Convert(call, Variable.VariableType));
definition.Body.Add(assign);

if (Next is IResolverFrame next)
{
next.WriteExpressions(definition);
}
else
if (Next is null)
{
throw new InvalidCastException($"{Next.GetType().GetFullName()} does not implement {nameof(IResolverFrame)}");
throw new InvalidCastException(
$"{typeof(GetInstanceFrame).GetFullName()}.{nameof(Next)} must not be null.");
}
}

public override void GenerateCode(GeneratedMethod method, ISourceWriter writer)
{
writer.Write(
$"var {Variable.Usage} = {_scope.Usage}.{nameof(Scope.GetInstance)}<{Variable.VariableType.FullNameInCode()}>(\"{_name}\");");
Next?.GenerateCode(method, writer);
}

public override IEnumerable<Variable> FindVariables(IMethodVariables chain)
{
_scope = chain.FindVariable(typeof(Scope));
yield return _scope;
}
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar/IoC/Frames/GetInstanceFrame.cs#L15-L68' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_getinstanceframe' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar/IoC/Frames/GetInstanceFrame.cs#L14-L67' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_getinstanceframe' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

When you write a `FindVariables()` method, be sure to keep a reference to any variable you need for later, and return that variable as part of the enumeration from this method. Lamar uses the dependency relationship between frames, the variables they depend on, and the creators of those variables to
Expand Down
18 changes: 12 additions & 6 deletions docs/guide/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ var container = new Container(x =>
{
// Using StructureMap style registrations
x.For<IClock>().Use<Clock>();

// Using ASP.Net Core DI style registrations
x.AddTransient<IClock, Clock>();

// and lots more services in all likelihood
});
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/GettingStarted.cs#L11-L22' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_start-a-container' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/GettingStarted.cs#L11-L24' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_start-a-container' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Now, to resolve services from your container:
Expand All @@ -65,7 +65,7 @@ var clock2 = provider.GetRequiredService<IClock>();
// Try to resolve a service if it's registered
var service2 = provider.GetService<IService>();
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/GettingStarted.cs#L24-L41' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_resolving-services-quickstart' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/GettingStarted.cs#L26-L45' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_resolving-services-quickstart' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Definitely note that the old StructureMap style of service resolution is semantically different than ASP.Net Core's DI resolution methods. That's been the cause of much user aggravation over the years.
Expand Down Expand Up @@ -159,20 +159,26 @@ builder.Host.UseLamar((context, registry) =>
{
// register services using Lamar
registry.For<ITest>().Use<MyTest>();

// Add your own Lamar ServiceRegistry collections
// of registrations
registry.IncludeRegistry<MyRegistry>();

// add the controllers
// discover MVC controllers -- this was problematic
// inside of the UseLamar() method, but is "fixed" in
// Lamar V8
registry.AddControllers();
});

var app = builder.Build();
app.MapControllers();

// Add Minimal API routes
app.MapGet("/", (ITest service) => service.SayHello());

app.Run();
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarWithMinimalApiOnNet6/Program.cs#L8-L31' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_lamar_with_minimal_api' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/LamarWithMinimalApiOnNet6/Program.cs#L8-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_using_lamar_with_minimal_api' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

::: tip
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/ioc/auto-wiring.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public ShippingScreenPresenter(IContainer container)
_repository = container.GetInstance<IRepository>();
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/SetterExamples.cs#L202-L215' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_shippingscreenpresenter-anti-pattern' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/SetterExamples.cs#L201-L216' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_shippingscreenpresenter-anti-pattern' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Instead of binding `ShippingScreenPresenter` so tightly to Lamar and having to explicitly fetch its dependencies, let's switch
Expand All @@ -37,7 +37,7 @@ public ShippingScreenPresenter(IShippingService service, IRepository repository)
_repository = repository;
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/SetterExamples.cs#L191-L200' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_shippingscreenpresenter-with-ctor-injection' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/SetterExamples.cs#L188-L199' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_shippingscreenpresenter-with-ctor-injection' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

As long as a Lamar `Container` knows how to resolve the `IRepository` and
Expand Down Expand Up @@ -69,5 +69,5 @@ public void ShowBuildPlan()
_output.WriteLine(buildPlan);
}
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/SetterExamples.cs#L237-L255' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_shippingscreenpresenter-build-plan' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Examples/SetterExamples.cs#L237-L257' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_shippingscreenpresenter-build-plan' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
10 changes: 3 additions & 7 deletions docs/guide/ioc/bootstrapping.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ To configure and bootstrap a Lamar container, you have a couple options. You can
<!-- snippet: sample_bootstrap-inline -->
<a id='snippet-sample_bootstrap-inline'></a>
```cs
var container = new Container(x =>
{
x.AddTransient<IClock, Clock>();
});
var container = new Container(x => { x.AddTransient<IClock, Clock>(); });
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/Bootstrapping.cs#L18-L23' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bootstrap-inline' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/Bootstrapping.cs#L16-L20' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bootstrap-inline' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Or pass in a configured `ServiceRegistry` object as shown below:
Expand All @@ -33,10 +30,9 @@ registry.For<IClockFactory>()
.Use<ClockFactory>()
.Singleton();


var container = new Container(registry);
```
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/Bootstrapping.cs#L32-L50' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bootstrap-with-registry' title='Start of snippet'>anchor</a></sup>
<sup><a href='https://github.com/JasperFx/lamar/blob/master/src/Lamar.Testing/Samples/Bootstrapping.cs#L25-L45' title='Snippet source file'>snippet source</a> | <a href='#snippet-sample_bootstrap-with-registry' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Lamar's `ServiceRegistry` supports a subset of StructureMap's old `Registry` class and should be used as a replacement when replacing StructureMap with
Expand Down
Loading

0 comments on commit e3ddc4f

Please sign in to comment.