Skip to content

Commit d0aaa73

Browse files
committed
feat: #526 implicitly inject interface method parameters from services
1 parent 92af64f commit d0aaa73

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
## Breaking Changes
44

55
- Vue 2 support has been dropped.
6-
- The CommonJS build of coalesce-vue has been dropped - only the ESM build remains. Most projects should be unaffected.
7-
- `ViewModel.$getErrors` now returns a `string[]` instead of a `Generator<string>`.
86
- `StandardBehaviors.AfterDelete` is now `AfterDeleteAsync` and has a different signature and semantics. Instead of modifying the resulting `item` and `includeTree` with `ref` parameters, these values can be optionally overridden by returning an ItemResult with its `Object` and `IncludeTree` properties populated with non-null values.
7+
- `ViewModel.$getErrors` now returns a `string[]` instead of a `Generator<string>`.
8+
- The CommonJS build of coalesce-vue has been dropped - only the ESM build remains. Most projects should be unaffected.
99

1010
## Features
1111

@@ -17,6 +17,7 @@
1717
- `ViewModel.$loadCleanData()` now returns the VM instance, to support call chaining.
1818
- `c-admin-display` now renders clickable links for binary values that will download the value as a file, instead of only showing the length in bytes.
1919
- `System.Uri` is now support as a type, mapping to a `string` in TypeScript.
20+
- Interfaces used as parameters in Coalesce-exposed methods are now automatically injected from services, without the need to specify `[InjectAttribute]`. This does not include known data-like interfaces including `IEnumerable`, `ICollection`, and `IFile`.
2021

2122
# 5.3.3
2223

src/IntelliTect.Coalesce.Tests/TargetClasses/TestDbContext/ComplexModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ ICollection<ExternalParentAsInputOnly> collection
269269
public InputOutputOnlyExternalTypeWithRequiredNonscalarProp MethodWithInputOutputOnlyExternalTypeWithRequiredNonscalarProp(InputOutputOnlyExternalTypeWithRequiredNonscalarProp i) => i;
270270

271271
[Coalesce, Execute]
272-
public void MethodWithSingleFileParameter(IFile file) { }
272+
public void MethodWithSingleFileParameter(IFile file, IWeatherService implicitInjectedService) { }
273273

274274
[Coalesce, Execute]
275275
public void MethodWithMultiFileParameter(ICollection<IFile> files) { }

src/IntelliTect.Coalesce/Api/DataSources/QueryableDataSourceBase`1.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ protected virtual IQueryable<T> ApplyListPropertyFilter(
214214

215215
object result = type == typeof(Guid)
216216
? Guid.Parse(item)
217-
: typeConverter.ConvertFromString(item);
217+
: typeConverter.ConvertFromString(item)!;
218218

219219
return (Success: true, Result: result);
220220
}

src/IntelliTect.Coalesce/TypeDefinition/ParameterViewModel.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ private protected ParameterViewModel(MethodViewModel parent, TypeViewModel type)
4545
/// <summary>
4646
/// True if this is an injected method parameter that should be represented by a controller action argument.
4747
/// </summary>
48-
public bool ShouldInjectFromServices => HasInjectAttribute || (!IsAutoInjectedContext && IsDbContext);
48+
public bool ShouldInjectFromServices =>
49+
HasInjectAttribute ||
50+
// Only DbContexts that are not the `IsAutoInjectedContext` are injected from services.
51+
// The auto-injected context is sourced from the controller, not the action parameters.
52+
(!IsAutoInjectedContext && IsDbContext) ||
53+
// Interfaces that are not known data types (file, collections) are always injected
54+
// because Coalesce couldn't otherwise possibly know the implementation type to use.
55+
(Type.IsInterface && !Type.IsFile && !Type.IsCollection)
56+
;
4957

5058
/// <summary>
5159
/// True if the parameter is marked with <see cref="InjectAttribute"/>

0 commit comments

Comments
 (0)