我的MVC自定义ControllerFactory可以工作,但是会更好吗? [英] My MVC Custom ControllerFactory works but could it be better?

查看:50
本文介绍了我的MVC自定义ControllerFactory可以工作,但是会更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经研究了Ninject,StructureMap和其他依赖项注入和服务定位器框架,但是这个问题更多地是关于学习它如何工作以及有什么更好的方法.更重要的是,我对框架的依赖注入的源代码并不感兴趣,但可以理解在实践/代码中从头到尾的实现方式.

I've looked at Ninject, StructureMap and Other Dependency Injection and Service Locator frameworks, but this question is more about learning how it works and what can be better. More to the point, I’m not interesting in looking at a Framework’s source code for Dependency Injection, but understanding how it’s achieved from beginning to end in practice/code.

下面的代码是针对一个小的内部项目的,因此请记住这一点.

The code below is for a small internal project, so with that in mind let me begin.

这是我的将域模型返回到控制器的界面.我已经决定,由于项目的规模(小),可以接受用于Controllers的单个界面.

Here is my interface for returning Domain Models to my controllers. I've decided that due to the size of the project (small), a single interface for Controllers was acceptable.

interface IModelFactory
{
    IEnumerable<User> GetUsers();
    User GetUser(Guid UserID);
    bool Add(User User);
    bool Delete(User User);
    bool Update(User User);
    IEnumerable<Car> GetCars();
    Car GetCar(Guid CarID);
    bool Add(Car Car);
    bool Delete(Car Car);
    bool Update(Car Car);
}

每个控制器都继承自 DIBaseController ,因此我不必为每个控制器创建私有成员.

Each controller has inherits from DIBaseController so I didn't have to create private members for every controller.

public abstract class DIBaseController : Controller
{
    protected IModelFactory ModelFactory { get; set; }

    public DIBaseController(IModelFactory ModelFactory)
    {
        this.ModelFactory = ModelFactory;
    }
}

public class HomeController : DIBaseController
{
    public HomeController (IModelFactory ModelFactory)
        : base(ModelFactory)
    {
    }
}

创建了自己的Controller Factory,使我可以将ModelFactory注入Controllers.

Created my own Controller Factory that allows me to inject my ModelFactory into Controllers.

internal class DIControllerFactory : DefaultControllerFactory 
{
    private IModelFactory _ModelFactory;

    internal DIControllerFactory(IModelFactory ModelFactory)
    {
        this._ModelFactory = ModelFactory;
    }

    public override IController CreateController(RequestContext requestContext, string controllerName)
    {
        IController result = null;
        string thisnamespace = this.GetType().Namespace;
        //This could be improved I bet.
        Type controller = Type.GetType(thisnamespace.Substring(0, thisnamespace.IndexOf('.')) + ".Controllers." + controllerName + "Controller");

        if (controller != null
            && controller.IsSubclassOf(typeof(DIBaseController)))
        {
            result = (IController)Activator.CreateInstance(controller, new object[] { this._ModelFactory });
        }
        else
        {
            result = base.CreateController(requestContext, controllerName);
        }

        return result;
    }
}

最后添加代码以将Concreate类注入到Factory中以注入到创建的Controllers中.

And finally added the code to Inject the Concreate class into the Factory to inject into created Controllers.

    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        RegisterRoutes(RouteTable.Routes);

        ControllerBuilder.Current.SetControllerFactory(new DIControllerFactory(new LinqSqlModelFactory()));
    }

我唯一没有探索过的领域(并且我现在不感兴趣)是创建一个 web.config 部分来动态创建 ModelFactory.这行得通,但是我有兴趣是否完全错过了船,靠近了还是发现了?

The only area I haven't explored (and I don't think I'm interested in at the moment) is to create a web.config section to dynamically create the ModelFactory. This works, but I'm interested if I've completely missed the boat, come close, or if I'm spot on?

推荐答案

而不是覆盖CreateController的使用

Instead of overriding CreateController use

protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)

它为您提供了控制器类型,并且实现的第一部分已过时.

It gives you the controller type and the first part of your implementation is obsolete.

接下来需要改进的一点是,您分析构造函数的参数,并传递使用某些配置创建的这些参数的实例,而不用猜测只有一个参数IModelFactory.

The next point you have to improve is that you analyze the parameters of the constructor and pass an instance of those parameters which are created using some configuration instead of guessing that there is exactly one parameter IModelFactory.

这篇关于我的MVC自定义ControllerFactory可以工作,但是会更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆