asp.net-mvc – 重用MVC arhitecture;有两层UI:ASP.NET MVC和.NET Winforms

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 重用MVC arhitecture;有两层UI:ASP.NET MVC和.NET Winforms前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
虽然我的问题看似抽象,但我希望不是.假设我开发了一个应用程序,一个ASP.NET MVC站点,后来我的任务是为这个应用程序构建一个 Winforms客户端,以及如何从现有应用程序中重用它?

我定义了模型,我定义了控制器和视图.他们都运作良好.

现在老板要求Winforms客户端,我希望我可以重用模型和控制器(假设我把它们放在不同的程序集中)而不是只重用视图(ASPX视图).

可以这样做吗?怎么样?

解决方法

我以前做过这个,不是使用asp.net MVC,而是使用纯asp.net网页表单.我使用了自己开发的MVP(模型 – 视图 – 展示器)模式,并且允许在WinForms应用程序中使用Presenter(在您的情况下为== Controller)的绝对最重要的事情是不引用与系统有关的任何事情名.web

所以你需要做的第一件事是引入接口来包装任何请求,响应,web等东西,并让每个Presenter通过依赖注入接受这些接口(或通过其他技术使它们可供Presenters使用),然后如果Presenter使用那些而不是实际的system.web东西.

例:

想象一下,您想要将控制权从页面A转移到页面B(在您的winforms应用程序中,您可能希望关闭表单A然后打开表单B).

接口:

  1. public interface IRuntimeContext
  2. {
  3. void TransferTo(string destination);
  4. }

网络实施:

  1. public class AspNetRuntimeContext
  2. {
  3. public void TransferTo(string destination)
  4. {
  5. Response.Redirect(destination);
  6. }
  7. }

winforms实现:

  1. public class WinformsRuntimeContext
  2. {
  3. public void TransferTo(string destination)
  4. {
  5. var r = GetFormByName(destination);
  6. r.Show();
  7. }
  8. }

现在是演示者(在你的情况下控制器):

  1. public class SomePresenter
  2. {
  3. private readonly runtimeContext;
  4. public SomePresenter(IRuntimeContext runtimeContext)
  5. {
  6. this.runtimeContext = runtimeContext;
  7. }
  8.  
  9. public void SomeAction()
  10. {
  11. // do some work
  12.  
  13. // then transfer control to another page/form
  14. runtimeContext.TransferTo("somewhereElse");
  15. }
  16. }

我没有详细研究过asp.net MVC实现,但是我希望这能给你一些指示,即启用你所使用的场景可能需要做很多工作.

您可能希望考虑接受必须为不同平台重新编码View和Controller,而是集中精力保持控制器非常薄,并将大部分代码放在可共享的服务层中.

祝好运!

猜你在找的asp.Net相关文章