[API Proposal]: ObjectPool.Create without new() constraint
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 290
Description
### Background and motivation
Today the existing `ObjectPool.Create` has `new()` constraint on the generic type parameter, because `DefaultPooledObjectPolicy` requires it.
Suggestion is a non-nullable `IPooledObjectPolicy` overload, so with a custom pooled object policy, we can create object pools for `T` does not have a default ctor (that would be the benefit of creating a custom `IPooledObjectPolicy`)
```csharp
public static ObjectPool Create(IPooledObjectPolicy policy) where T : class
```
### API Proposal
```csharp
namespace Microsoft.Extensions.ObjectPool;
public static class ObjectPool
{
public static ObjectPool Create(IPooledObjectPolicy policy) where T : class
}
```
### API Usage
```csharp
ObjectPool pool = ObjectPool.Create(new MyTypePolicy());
class MyType(int value) { }
class MyTypePolicy : IPooledObjectPolicy
{
public MyType Create() => new MyType(0);
public bool Return(MyType obj)
{
return true;
}
}
```
### Alternative Designs
Instead of an overload (which might require changing and removing the parameter of the existing method), have a separate method name.
(I am happy to address the issue if there are no objections during the discussion.)
Contributor guide
Assessment
This issue has not been assessed yet.