小编Eli*_*lie的帖子

Autofac - 如何使用参数创建生成的工厂

我正在尝试使用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)

.net c# autofac

17
推荐指数
2
解决办法
1万
查看次数

如何从自定义异常过滤器返回JSON结果?

我想创建一个自定义异常过滤器,它将捕获返回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)

.net c# json asp.net-mvc-2

8
推荐指数
1
解决办法
7711
查看次数

标签 统计

.net ×2

c# ×2

asp.net-mvc-2 ×1

autofac ×1

json ×1