多个控制器,一个视图和一个模型ASP.NET MVC 3

时间:2011-11-27 18:40:33

标签: asp.net asp.net-mvc-3

我想要一个型号和一个我的ASP.NET MVC 3应用程序中由多个控制器提供的视图。

我正在实施一个与用户的在线日历交互的系统,我支持Exchange,Google,Hotmail,Yahoo,Apple等等......每个都有不同的日历API实现,但我可以抽象出来远离我自己的模特。我想通过在控制器级实现多态,我将能够清楚地处理不同的API和身份验证问题。

我有一个很好的清洁模型和视图,到目前为止我已经实现了两个控制器,证明我可以读取/查询/写入/更新到Exchange和Google:ExchangeController.csGoogleController.cs。< / p>

我有/Views/Calendar,其中包含我的观看代码。我还有/Models/CalendarModel.cs包含我的模型。

我希望测试用户在我的ControllerFactory中使用哪个日历系统。我已经实现了这样:

public class CustomControllerFactory : DefaultControllerFactory
{
    protected override IController GetControllerInstance(RequestContext requestContext, Type controllerType)
    {
        if (controllerType == typeof(CalendarController))
        {                
            if(MvcApplication.IsExchange) // hack for now
                return new ExchangeController();
            else
                return new GoogleController();
        }
        return base.GetControllerInstance(requestContext, controllerType);
    }
}

和我的Application_Start

ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());

这很有效。如果我到http://.../Calendar这个工厂代码工作,并创建了正确的控制器!

这很好用,我没有真正理解我在做什么。现在我想我得到了它但我想确保我没有错过任何东西。我真的花时间寻找这样的东西而没有找到任何东西。

我担心的一件事是,我认为我能够在CalendarControllerExchangeController / GoogleController之间建立继承关系:

public class ExchangeController : CalendarController
{

但如果我这样做,我会得到:

The current request for action 'Index' on controller type 'GoogleController' is ambiguous between the following action methods:
System.Web.Mvc.ViewResult Index(System.DateTime, System.DateTime) on type Controllers.GoogleController
System.Web.Mvc.ActionResult Index() on type Controllers.CalendarController

让我感到厌烦,因为我想在基础上放置一些常用功能,现在我想我将不得不采用另一种方式。

这是为一个视图/模型设置多个控制器的正确方法吗?还有什么我需要考虑的?

编辑:关于我的impl的更多详情

基于下面的回答(谢谢!)我想我需要展示更多代码,以确保你们看到我正在尝试做的事情。我的模型实际上只是一个数据模型。它始于:

/// <summary>
/// Represents a user's calendar across a date range.
/// </summary>
public class Calendar
{
    private List<Appointment> appointments = null;

    /// <summary>
    /// Date of the start of the calendar.
    /// </summary>
    public DateTime StartDate { get; set; }

    /// <summary>
    /// Date of the end of the calendar
    /// </summary>
    public DateTime EndDate { get; set; }

    /// <summary>
    /// List of all appointments on the calendar
    /// </summary>
    public List<Appointment> Appointments
    {
        get
        {
            if (appointments == null)
                appointments = new List<Appointment>();
            return appointments;
        }
        set { }
    }


}

然后我的控制器有以下方法:

public class ExchangeController : Controller
{
    //
    // GET: /Exchange/
    public ViewResult Index(DateTime startDate, DateTime endDate)
    {
        // Exchange specific gunk. The MvcApplication._service thing is a temporary hack
        CalendarFolder calendar = (CalendarFolder)Folder.Bind(MvcApplication._service, WellKnownFolderName.Calendar);

        Models.Calendar cal = new Models.Calendar();
        cal.StartDate = startDate;
        cal.EndDate = endDate;

        // Copy the data from the exchange object to the model
        foreach (Microsoft.Exchange.WebServices.Data.Appointment exAppt in findResults.Items)
        {
            Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service, exAppt.Id);
            Models.Appointment appt = new Models.Appointment();
            appt.End = a.End;
            appt.Id = a.Id.ToString();

... 
        }

        return View(cal);
    }

    //
    // GET: /Exchange/Details/5
    public ViewResult Details(string id)
    {
...
        Models.Appointment appt = new Models.Appointment();
...
        return View(appt);
    }


    //
    // GET: /Exchange/Edit/5
    public ActionResult Edit(string id)
    {
        return Details(id);
    }

    //
    // POST: /Exchange/Edit/5
    [HttpPost]
    public ActionResult Edit(MileLogr.Models.Appointment appointment)
    {
        if (ModelState.IsValid)
        {
            Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service, new ItemId(appointment.Id));

           // copy stuff from the model (appointment)
           // to the service (a)
           a.Subject = appointment.Subject            

...
            a.Update(ConflictResolutionMode.AlwaysOverwrite, SendInvitationsOrCancellationsMode.SendToNone);

            return RedirectToAction("Index");
        }
        return View(appointment);
    }

    //
    // GET: /Exchange/Delete/5
    public ActionResult Delete(string id)
    {
        return Details(id);
    }

    //
    // POST: /Exchange/Delete/5
    [HttpPost, ActionName("Delete")]
    public ActionResult DeleteConfirmed(string id)
    {
        Microsoft.Exchange.WebServices.Data.Appointment a = Microsoft.Exchange.WebServices.Data.Appointment.Bind(MvcApplication._service, new ItemId(id));
        a.Delete(DeleteMode.MoveToDeletedItems);
        return RedirectToAction("Index");
    }

所以它基本上是典型的CRUD东西。我已经提供了ExchangeCalendar.cs版本的示例。 GoogleCalendar.cs在实现方面显然相似。

我的模型(日历)和相关的类(例如约会)是从控制器传递到视图的内容。我不希望我的观点看到正在使用的基础在线服务的详细信息。我不明白如何使用接口(或抽象基类)实现Calendar类将为我提供我正在寻找的多态性。

我必须根据用户选择要使用的实现。

我可以这样做:

  • 在我的模特中。我不想这样做,因为我的模型使用特定于服务的代码变得非常苛刻。
  • 在控制器中。例如。使用重定向到正确实现的内容启动每个控制器方法
  • 控制器下方。例如。正如我上面提到的一个新的控制器工厂。

以下回复提到“服务层”。我想这也许是我离开轨道的地方。如果你看看MVC正常使用数据库的方式,dbContext代表“服务层”,对吧?那么也许你们所建议的是第四个我可以做间接的地方?例如,上面的Edit将是这样的:

    private CalendarService svc = new CalendarService( e.g. Exchange or Google );

    //
    // POST: /Calendar/Edit/5
    [HttpPost]
    public ActionResult Edit(MileLogr.Models.Appointment appointment)
    {
        if (ModelState.IsValid)
        {
            svc.Update(appointment);
            return RedirectToAction("Index");
        }
        return View(appointment);
    }

这是正确的方法吗?

对不起,这已经变得如此啰嗦,但这是我知道如何获得足够的背景的唯一方式...... 结束编辑

3 个答案:

答案 0 :(得分:8)

我不会这样做。正如Jonas指出的那样,控制器应该非常简单,并且旨在协调用于响应请求的各种“服务”。请求流是否真的从日历到日历都有所不同?或者是获取不同数据所需的数据调用。

执行此操作的一种方法是将日历纳入公共日历界面(或抽象基类),然后通过构造函数参数将日历接受到控制器中。

public interface ICalendar {
  // All your calendar methods
}

public abstract class Calendar {
}

public class GoogleCalendar : Calendar {}

public class ExchangeCalendar : Calendar {}

然后在CalendarController中

public class CalendarController {
    public CalendarController(ICalendar calendar) {}
}

除非您注册依赖项解析程序,否则默认情况下这不起作用。一种快速的方法是使用NuGet安装一个设置一个的包。例如:

Install-Package Ninject.Mvc3

我认为这将是一个更好的架构。但是假设你不同意,让我回答你原来的问题。

你得到不明确的异常的原因是你有两个公共Index方法,这些方法没有通过一个属性来区分,该属性表明一个应该响应GET而另一个应该响应POST。控制器的所有公共方法都是动作方法。

如果CalendarController不打算直接实例化(即它将永远被继承),那么我将在该类protected virtual上创建Index方法,然后在派生中覆盖它类。

如果CalendarController本身要实例化,而其他派生类只是它的“风味”,那么你需要制作Index方法public virtual和然后让每个派生类重写Index方法。如果他们没有覆盖它,他们会添加另一个Index方法(C#规则,而不是我们的),你需要为MVC区分它们。

答案 1 :(得分:7)

我认为你在这里走的是一条危险的道路。控制器通常应该尽可能简单,并且只包含例如“胶水”之间的“胶水”。您的服务层和模型/视图。通过将一般日历抽象和供应商特定实现移出控制器,您可以摆脱路由与日历实现之间的耦合。

编辑:我将在服务层实现多态,并在服务层中有一个工厂类检查您的用户数据库以查找当前用户的供应商并实例化CalendarService类的相应实现。这样就不需要检查控制器中的日历供应商,保持简单。

通过耦合到路线,我的意思是您的自定义网址是目前导致AFAICT出现问题的网址。通过使用单个控制器并将复杂性转移到服务层,您可以只使用MVC的默认路由。

答案 2 :(得分:0)

正如其他答案所示,你真的应该重构你的代码,以便首先不需要多个控制器。

但是,您可以仍然让您的控制器继承自基类控制器 - 您只需要确保在Global.asax.cs中注册路由时,使用重载指定查找给定路径的控制器和操作方法的命名空间

e.g。

 routes.MapRoute(null, "{controller}/{action}", new[] { "Namespace.Of.Controllers.To.USe" });