- Notifications
You must be signed in to change notification settings - Fork744
Variance on a method returning ContainerResource or ProjectResource#13078
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I am currently just playing around with the idea to have an extension returning either the project (for env=development) or a deployed version of some (for env=production).
Both share a lot of interfaces like Is there some way using generics to have a method that returns It might also be possible to write a class that wraps it using constrains, but then I again don't know how to call it as I would need to pass it a type that's part of the inheritance chain. The idea is that the references between them, stuff like environment variables or endpoints are still the same, no matter the environment. |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1
I was able to find a light-weight solution that doesn't crash. Just in case someone else has a similar need.
internalinterfaceIProjectOrContainerResource:IResourceWithEnvironment,IResourceWithArgs,IResourceWithEndpoints,IResourceWithWaitSupport,IComputeResource;internalclassProjectOrContainerResourceBuilder<T>(IResourceBuilder<T>builder):IResourceBuilder<IProjectOrContainerResource>whereT:IResourceWithEnvironment,IResourceWithArgs,IResourceWithEndpoints,IResourceWithWaitSupport,IComputeResource{privateIResourceBuilder<T>_builder=builder;publicIResourceBuilder<IProjectOrContainerResource>WithAnnotation<TAnnotation>(TAnnotationannotation,…
Replies: 2 comments 6 replies
-
I've also run into this and have had the same observation/reflection when setting defaults for my resources automatically. I usually need a resource object with two or more interfaces mentioned. Right now we have to pattern match for ProjectResource, ContainerResource, etc and duplicate code for them. Basically I want |
BetaWas this translation helpful?Give feedback.
All reactions
-
C# 25 maybe 😬 |
BetaWas this translation helpful?Give feedback.
All reactions
👀 1
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thanks@davidfowl, I take this as "not supported by the language" instead of "you just missed some easy to use variance feature" 😁 I really thought I've just overlooked something I could apply to a method that hints the return value to be promised to contain those interfaces on the generic type. Like on the input type. But I assume due to limited scoping, it's a lot easier to have on inputs than on outputs. |
BetaWas this translation helpful?Give feedback.
All reactions
-
The closest you can get is something like: |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
@KennethHoff Thats what I did, but sadly this would only work to ensure the input parameter is already implementing those interfaces and passing through the original type. In my case, I would create the objects inside and therefore wouldn't be able to choose the The only other options I could come up were either using |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I was able to find a light-weight solution that doesn't crash. Just in case someone else has a similar need. internalinterfaceIProjectOrContainerResource:IResourceWithEnvironment,IResourceWithArgs,IResourceWithEndpoints,IResourceWithWaitSupport,IComputeResource;internalclassProjectOrContainerResourceBuilder<T>(IResourceBuilder<T>builder):IResourceBuilder<IProjectOrContainerResource>whereT:IResourceWithEnvironment,IResourceWithArgs,IResourceWithEndpoints,IResourceWithWaitSupport,IComputeResource{privateIResourceBuilder<T>_builder=builder;publicIResourceBuilder<IProjectOrContainerResource>WithAnnotation<TAnnotation>(TAnnotationannotation,ResourceAnnotationMutationBehaviorbehavior=ResourceAnnotationMutationBehavior.Append)whereTAnnotation:IResourceAnnotation{_builder=_builder.WithAnnotation(annotation,behavior);returnthis;}publicIDistributedApplicationBuilderApplicationBuilder=>_builder.ApplicationBuilder;publicIProjectOrContainerResourceResource=>Unsafe.As<IProjectOrContainerResource>(_builder.Resource);} This can then be used like Still not sure why directly using Sadly it's not possible to also inherit |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
/CC@jrunestone as this at least covers the ones you need. |
BetaWas this translation helpful?Give feedback.
All reactions
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
Thanks for the tip but I'd rather not use unsafe type casting :D I'm iterating the resource collection and casting to |
BetaWas this translation helpful?Give feedback.
All reactions
👍 1