我正在尝试使用Autofac创建一个"生成"工厂,它将基于枚举参数实时解析依赖关系.
给定以下接口/类:
public delegate IConnection ConnectionFactory(ConnectionType connectionType);
public enum ConnectionType
{
Telnet,
Ssh
}
public interface IConnection
{
bool Open();
}
public class SshConnection : ConnectionBase, IConnection
{
public bool Open()
{
return false;
}
}
public class TelnetConnection : ConnectionBase, IConnection
{
public bool Open()
{
return true;
}
}
public interface IEngine
{
string Process(ConnectionType connectionType);
}
public class Engine : IEngine
{
private ConnectionFactory _connectionFactory;
public Engine(ConnectionFactory connectionFactory)
{
_connectionFactory = connectionFactory;
}
public string Process(ConnectionType …Run Code Online (Sandbox Code Playgroud) 我想创建一个自定义异常过滤器,它将捕获返回JSON结果的控制器操作中抛出的异常.
我想重构以下操作方法:
public JsonResult ShowContent()
{
try
{
// Do some business logic work that might throw a business logic exception ...
//throw new ApplicationException("this is a business exception");
var viewModel = new DialogModel
{
FirstName = "John",
LastName = "Doe"
};
// Other exceptions that might happen:
//throw new SqlException(...);
//throw new OtherException(...);
//throw new ArgumentException("this is an unhandeled exception");
return
Json(
new
{
Status = DialogResultStatusEnum.Success.ToString(),
Page = this.RenderPartialViewToString("ShowContent", viewModel)
});
}
catch (ApplicationException exception)
{
return Json(new …Run Code Online (Sandbox Code Playgroud)