asp.net mvc的通用控制器 [英] asp.net mvc generic controller

查看:101
本文介绍了asp.net mvc的通用控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实施ASP.NET MVC的通用控制器。

I am thinking of implementing a generic Controller in ASP.NET MVC.

PlatformObjectController<T>

其中T是一个(生成)平台对象

where T is a (generated) platform object.

这可能吗?有经验/文件?

Is this possible? Is there experience / documentation?

例如,一个相关的问题是由此产生的URL如何。

One related question for example is how the resulting URLs are.

推荐答案

的确是这样,你不能直接使用它,但你可以继承它,并用孩子的

yes it is, you just can't use it directly but you can inherit it and use the childs

下面是一个我用:

     public class Cruder<TEntity, TInput> : Controller
        where TInput : new()
        where TEntity : new()
    {
        protected readonly IRepo<TEntity> repo;
        private readonly IBuilder<TEntity, TInput> builder;


        public Cruder(IRepo<TEntity> repo, IBuilder<TEntity, TInput> builder)
        {
            this.repo = repo;
            this.builder = builder;
        }

        public virtual ActionResult Index(int? page)
        {
            return View(repo.GetPageable(page ?? 1, 5));
        }

        public ActionResult Create()
        {
            return View(builder.BuildInput(new TEntity()));
        }

        [HttpPost]
        public ActionResult Create(TInput o)
        {
            if (!ModelState.IsValid)
                return View(o);
            repo.Insert(builder.BuilEntity(o));
            return RedirectToAction("index");
        }
    }

和用途:

 public class FieldController : Cruder<Field,FieldInput>
    {
        public FieldController(IRepo<Field> repo, IBuilder<Field, FieldInput> builder)
            : base(repo, builder)
        {
        }
    }

    public class MeasureController : Cruder<Measure, MeasureInput>
    {
        public MeasureController(IRepo<Measure> repo, IBuilder<Measure, MeasureInput> builder) : base(repo, builder)
        {
        }
    }

    public class DistrictController : Cruder<District, DistrictInput>
    {
        public DistrictController(IRepo<District> repo, IBuilder<District, DistrictInput> builder) : base(repo, builder)
        {
        }
    }

    public class PerfecterController : Cruder<Perfecter, PerfecterInput>
    {
        public PerfecterController(IRepo<Perfecter> repo, IBuilder<Perfecter, PerfecterInput> builder) : base(repo, builder)
        {
        }
    }

在code是在这里:
<一href=\"http://$c$c.google.com/p/asms-md/source/browse/trunk/WebUI/Controllers/FieldController.cs\">http://$c$c.google.com/p/asms-md/source/browse/trunk/WebUI/Controllers/FieldController.cs

更新:

在这里,现在使用这种方法: HTTP://prodinner.$c$cplex.com

using this approach here now: http://prodinner.codeplex.com

这篇关于asp.net mvc的通用控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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