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

tig*_*tig 6 asp.net asp.net-mvc-3

我希望在我的ASP.NET MVC 3应用程序中有一个由多个控制器提供的模型和视图.

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

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

我有/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);
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的Application_Start:

ControllerBuilder.Current.SetControllerFactory(new CustomControllerFactory());
Run Code Online (Sandbox Code Playgroud)

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

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

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

public class ExchangeController : CalendarController
{
Run Code Online (Sandbox Code Playgroud)

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

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
Run Code Online (Sandbox Code Playgroud)

哪个让我失望,因为我想在基础上放置一些常用的功能,现在我想我将不得不采用另一种方式.

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

编辑:关于我的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 { }
    }


}
Run Code Online (Sandbox Code Playgroud)

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

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");
    }
Run Code Online (Sandbox Code Playgroud)

所以它基本上是典型的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);
    }
Run Code Online (Sandbox Code Playgroud)

这是正确的方法吗?

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

Haa*_*ked 8

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

实现此目的的一种方法是将日历置于公共日历界面(或抽象基类)后面,然后通过构造函数参数将日历接受到控制器中.

public interface ICalendar {
  // All your calendar methods
}

public abstract class Calendar {
}

public class GoogleCalendar : Calendar {}

public class ExchangeCalendar : Calendar {}
Run Code Online (Sandbox Code Playgroud)

然后在CalendarController中,

public class CalendarController {
    public CalendarController(ICalendar calendar) {}
}
Run Code Online (Sandbox Code Playgroud)

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

Install-Package Ninject.Mvc3
Run Code Online (Sandbox Code Playgroud)

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

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

如果CalendarController不打算直接实例化(即它将始终被继承),那么我将在该类上创建Index方法protected virtual,然后在派生类中重写它.

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


Jon*_*øgh 7

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

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

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