重用MVC arhitecture; UI有两层:ASP.NET MVC和.NET的WinForms [英] Reuse MVC arhitecture; Have two layers of UI : ASP.NET MVC and .NET Winforms

查看:144
本文介绍了重用MVC arhitecture; UI有两层:ASP.NET MVC和.NET的WinForms的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

虽然我的问题似乎是抽象的,我希望它不是。假设我开发一个应用程序,一个ASP.NET MVC的网站,后来我负责建立一个客户端的WinForms为这个应用程序,我多少,以及如何从现有的应用程序重用?

Although my question might seem abstract I hope it's not. Suppose I develop an application, an ASP.NET MVC site and later I am tasked to build an Winforms client for this application how much and how can I reuse from the existing application?

我所定义的模型,我定义控制器和视图。他们都很好地工作。

I defined the models, I defined controllers and views. They all work well.

现在老板而要求一个WinForms客户端,我希望我可以重复使用的模型和控制器(只要我把它们放在不同的程序集),而不是仅仅重复使用的意见(ASPX视图)。

Now the boss comes asking for a Winforms client and I am hoping I can reuse the models and the controllers (provided I put them in different assemblies) and not reuse just the views (ASPX views).

可以这样做?怎么样?

推荐答案

我已经pviously这样做$ P $,不与asp.net MVC而是纯粹的asp.net web表单。我用了一个土生土长的MVP(模型 - 视图 - presenter)模式,绝对是最重要的事情,让presenter(==你的情况控制器)是在一个WinForms应用程序使用的是不参考的任何的做的System.Web

I have done this previously, not with asp.net MVC but with pure asp.net web forms. I used a home-grown MVP (Model-View-Presenter) pattern, and the absolute most important thing to allow the Presenter (== Controller in your case) to be used in a WinForms app was to not reference anything to do with system.web

所以,你需要做的第一件事就是引入接口(S)来包装的任何请求,响应,网络等东西,有充分的presenter接受通过依赖注入这些接口(或使它们提供给$ P通过一些其它技术$ psenters),那么如果presenter使用这些而不是实际的System.Web东西

So the first thing you need to do is introduce interface(s) to wrap any request, response, web etc stuff, and have every Presenter accept these interfaces via Dependency Injection (or make them available to the Presenters by some other technique), then if the Presenter uses those rather than the actual system.web stuff.

例如:

想象一下,你想转让从网页A网页B的控制(在您的WinForms应用程序可能要关闭A形再打开表格B)。

Imagine you want to transfer control from Page A to Page B (which in your winforms app you might want to close form A then open form B).

接口:

public interface IRuntimeContext
{
  void TransferTo(string destination);
}

Web实现:

public class AspNetRuntimeContext
{
  public void TransferTo(string destination)
  {
    Response.Redirect(destination);
  }
}

的WinForms实现:

winforms implementation:

public class WinformsRuntimeContext
{
  public void TransferTo(string destination)
  {
    var r = GetFormByName(destination);
    r.Show();
  }
}

现在的presenter(控制器在您的情况):

Now the Presenter (Controller in your case):

public class SomePresenter
{
  private readonly runtimeContext;
  public SomePresenter(IRuntimeContext runtimeContext)
  {
    this.runtimeContext = runtimeContext;
  }

  public void SomeAction()
  {
    // do some work

    // then transfer control to another page/form
    runtimeContext.TransferTo("somewhereElse");
  }
}

我还没有看详细asp.net MVC实现,但我希望这给你一些迹象表明,它可能会大量的工作,让你以后的情况。

I haven't looked at the asp.net MVC implementation in detail but I hope this gives you some indication that it will probably be a lot of work to enable the scenario you are after.

您可能不是要考虑接受,你将不得不重新code视图和控制器为不同的平台,而集中于保持你的控制器非常薄,并且把你的大部分code在可以共享一个服务层。

You may instead want to consider accepting that you will have to re-code the View and Controller for the different platforms, and instead concentrate on keeping your controllers extremely thin and putting the bulk of your code in a service layer that can be shared.

祝您好运!

这篇关于重用MVC arhitecture; UI有两层:ASP.NET MVC和.NET的WinForms的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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