我有一个描述某种业务逻辑的接口,即
public interface IFoo
{
void Bar();
}
Run Code Online (Sandbox Code Playgroud)
我也有一些该接口的实现,例如
public class ConcreteFoo1 : IFoo
{
public void Bar() {...}
}
public class ConcreteFoo2 : IFoo, IDisposable
{
public void Bar() {...}
public void Dispose {...}
}
Run Code Online (Sandbox Code Playgroud)
在客户端内,我现在通过依赖注入得到一个IFoo实例 - 所以我不知道我得到了哪一个.但是当我得到ConcreteFoo2的一个实例并完成它时,我需要确保调用Dispose.
我想我有两个选择,但如果我有更多更好的选择,请纠正我.
让IFoo扩展IDisposable
这不是很好,因为它要求IFoo的所有实现也实现IDisposable.但它允许我使用using {...}语法.
要求所有客户检查IDisposable并在必要时调用它.
这将使实现更简单,但将责任转移到客户端,这必须做这样的事情
public void DoStuff()
{
IFoo foo = ....;
//do stuff
IDisposable disposable = foo as IDisposable;
if(disposable != null)
{
disposable.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他/更好的方法可以确保Dispose()被确定性地调用?对这两种方法中的任何一种都有赞成和反对意见吗?
我有两个这样的课程
public abstract class Foo<T> where T : Bar {
public Bar Do(Bar obj) {
//I cast to T here and the call the protected one.
}
...
protected abstract Bar Do(T obj);
}
public abstract class FooWithGoo<T> : Foo<T> where T:Bar {
...
}
Run Code Online (Sandbox Code Playgroud)
尝试使用Moq使用此行在单元测试中模拟这个,这new Mock<FooWithGoo<Bar>>()给了我这个例外.
System.ArgumentException: Type to mock must be an interface or an abstract or non-sealed class. ---> System.TypeLoadException: Method 'Do' in type 'Castle.Proxies.FooWithGoo``1Proxy' from assembly 'DynamicProxyGenAssembly2, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' does not have an …
我最近将支持 Swagger 的 ASP.NET Core 项目升级到 2.2。我注意到我所有的错误响应现在都显示了一个 ProblemDetails 响应正文。
{
"type": "string",
"title": "string",
"status": 0,
"detail": "string",
"instance": "string",
"additionalProp1": {},
"additionalProp2": {},
"additionalProp3": {}
}
Run Code Online (Sandbox Code Playgroud)
据微软称,这是意料之中的——我对此很满意。
但是,出于某种原因,我的项目不会为某些默认返回代码(例如 401)返回这些代码。这是(我认为是)我的启动配置的相关部分。
services
.AddAuthentication(options => {
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(jwtOptions => {
jwtOptions.Authority = jwtConfiguration.Authority;
jwtOptions.TokenValidationParameters.ValidAudiences = jwtConfiguration.Audiences;
});
// Add framework services.
services
.AddMvcCore(options => {
options.Filters.Add<OperationCancelledExceptionFilterAttribute>();
})
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddAuthorization()
.AddApiExplorer()
.AddJsonFormatters()
.AddCors()
.AddJsonOptions(options => options.SerializerSettings.Converters.Add(new StringEnumConverter()));
services.AddVersionedApiExplorer(
options => {
//The format of the version added to …Run Code Online (Sandbox Code Playgroud)