-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Reconciling base classes and interfaces in Microsoft.Extensions #57596
Comments
I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label. |
Tagging subscribers to this area: @eerhardt, @maryamariyan Issue DetailsBackground and motivationWe keep getting asked to add functionality to features in
We can't add methods to interfaces, so we're adding more and more APIs to the classes only. We should make a decision on what to do with the interfaces. My proposal is hide and obsolete. API ProposalTBD API UsageTBD RisksNo response
|
IMemoryCache is used in many public TagHelper APIs like: Most other ASP.NET uses seem to be internal. |
I can see 2 high-level approaches to fixing this. 1. Embrace the interfaces and use DIMsTaking #45593 as a case study, it would mean this change: namespace Microsoft.Extensions.Caching.Memory
{
public interface IMemoryCache
{
+#if NET6_OR_GREATER
+ void Clear() { throw new NotImplementedException(); }
+#endif
}
public class MemoryCache : IMemoryCache
{
+ public void Clear();
}
} The advantages:
Disadvantages:
2. Deprecate the interfaces and replace with base classesAgain, using #45593 as a case study: namespace Microsoft.Extensions.Caching.Memory
{
+ [Obsolete("Use MemoryCacheBase instead")]
public interface IMemoryCache
{
...
}
+ public abstract class MemoryCacheBase
+ {
+ public virtual void Clear() { throw new NotImplementedException(); }
+ }
- public class MemoryCache : IMemoryCache
+ public class MemoryCache : MemoryCacheBase, IMemoryCache
{
} (Note: we wouldn't strictly need to obsolete the interfaces. So we have options 2.a - obsolete interfaces and 2.b - don't obsolete the interfaces.) The advantages/disadvantages are basically flipped from Option 1 above. ThoughtsFollowing Option 2 is what we would traditionally do. It is the "safer" and more traveled road. Do we think Option 1 has enough advantages to try to blaze a new trail here? |
Option 2 would cause significant public API breaks up stack and is not DI friendly. Option 1 is much less invasive if you can make it work. If you find Option 1 to be insufficient or problematic after one or more releases, you can still fall back to Option 2. |
Can you elaborate? Why is an abstract base class "not DI friendly" vs. an interface? |
I'd overlooked MemoryCacheBase, yes that would work for DI. I still think DIMs are better for being less disruptive to the ecosystem. We've used them successfully in aspnetcore, though we didn't have the netstandard limitation. |
There's also the possibility of: namespace Microsoft.Extensions.Caching.Memory;
+ public interface IMemoryCache2 : IMemoryCache
+ {
+ void Clear();
+ }
- public class MemoryCache : IMemoryCache
+ public class MemoryCache : IMemoryCache, IMemoryCache2 With the option to obsolete |
Background and motivation
We keep getting asked to add functionality to features in
Microsoft.Extensions
, such as:We can't add methods to interfaces, so we're adding more and more APIs to the classes only. We should make a decision on what to do with the interfaces. My proposal is hide and obsolete.
API Proposal
TBD
API Usage
TBD
Risks
No response
The text was updated successfully, but these errors were encountered: