asp.net mvc универсальный контроллер

Я думаю о реализации универсального Контроллера в ASP.NET MVC.

PlatformObjectController<T>

где T является (сгенерированным) объектом платформы.

Действительно ли это возможно? Есть ли опыт / документация?

Один связанный вопрос, например, состоит в том, как получающиеся URL.

12
задан Frank Michael Kraft 5 August 2010 в 10:01
поделиться

1 ответ

да, вы просто не можете использовать его напрямую, но вы можете унаследовать его и использовать дочерние элементы

вот тот, который я использую:

     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)
        {
        }
    }

код здесь: http://code.google.com/p/asms-md/source/browse/trunk/WebUI/Controllers/FieldController.cs

обновить:

используя этот подход сейчас: http : //prodinner.codeplex.com

19
ответ дан 2 December 2019 в 18:51
поделиться
Другие вопросы по тегам:

Похожие вопросы: