我有一个问题在c#和asp.net中实现mvp模式 [英] i have a problem implementing the mvp pattern in c# and asp.net

查看:252
本文介绍了我有一个问题在c#和asp.net中实现mvp模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个论坛系统的MVP模式,但我不知道如果我实现了一个正确的方式。所以这里是我结束了:




  • 我创建了一个数据库有三个表:论坛,主题, >

  • 向项目中添加了一个新的类型数据集,然后将所有表格拖动到该项目中


  • 创建了三个新的calsses:ForumsModel, ThreadsPodel,PostsModel


  • 添加了三个界面:IForumView,IThreadView,IPostView


  • PostsPresenter



在模型类中,我只是调用类型化的数据集方法,而在presenter中我调用了模型方法。视图是 .aspx 页面。这是所有的论坛系统,但这里是棘手的部分:



因为MVP模式是一个UI模式我必须做的应用程序本身的数据验证。所以用我的设计MVP是应用程序!



$ b

我做错了什么? 首先,为什么我选择类型数据集与存储过程超过其他选项:它是最轻的数据提供程序,不会危及您的任何架构。其他选项是直接sql,这不是一个好的选择新创建的应用程序,LINQ to sql类,这太重了,无法实例化每个请求,Entity框架是伟大的,但是太多这样一个简单的任务!至少如果我是创建一个博客引擎,这将是我的第一选择。
为什么我没有选择MVC,这是因为我认为MVP是更好的模式,因为它会给我完全分隔我的应用程序的不同部分。到底是一个问题,但你应该能够实现任何模式,对吧?
我看到了不同的MVP类型,但我想实现的是这样:



model< --- ----------> presenter< -------------->查看



databind选项是针对演示模型模式,由martin fawler介绍,直到我记得,但我想创建的是一个微软版本,基本上是一个扩展版本的PM模式。



两天前我问了一个关于数据验证的问题,有人建议我应该在应用程序本身,所以当我问这个应用程序在哪里他说MVP是一个UI模式,它不应该是你的应用程序,你应该在用户界面层实现MVP。所以这是我在第一个问这个问题的原因!

解决方案

视图界面。



View界面包含Presenter需要绑定的页面事件,以便更改视图的状态,以及可能的属性和方法需要。例如,它可以钩上onLoad事件并调用View来改变它的状态。在这个过程中,它与View实现无关,对控件一无所知。这意味着你可以编写没有ASPX实例的单元测试。



要创建一个自动化连线的MVP引擎,你可以使用一个基本页面,泛型和IoC。 IoC允许你为只有那个请求管道可见的WebContextBase这样的东西设置依赖重载,并且可以传递给你的Presenter构造函数。



IoC也允许你注入其他依赖关系到您的演示者,以帮助保持轻量级。



MVP在您使用WebForms时非常有用,例如大多数CMS系统。如果你不受约束,那么使用MVC.Net进行新的Web开发。



更新:如所承诺的,这里是一个相当体面的例子。它缺少的是依赖覆盖可以这样做:

  var dependencies = new DependencyOverrides 
{
{typeof(HttpRequestBase),new HttpRequestWrapper(Request)},
{typeof(HttpResponseBase),new HttpResponseWrapper(Response)},
{typeof(IPrincipal),Context.User},
{typeof(IIdentity),Context.User.Identity},
{typeof(IUserProfile),Context.User.Profile},
{typeof(HttpSessionStateBase),new HttpSessionStateWrapper $ b};

presenter = container.Resolve< TPresenter>(dependencies);


i've created a forum system in MVP pattern but i'm not sure if i implemented it in a right way. so here is what i ended up with:

  • i've created a database which has three tables: forums, threads, posts
  • added a new typed dataset to the project and then dragged all the tables into it
  • created three new calsses: ForumsModel, ThreadsModel, PostsModel
  • added three interfaces: IForumView, IThreadView, IPostView
  • three more classes: ForumsPresenter, ThreadsPresenter, PostsPresenter

inside the model classes i just call the typed dataset methods and in presenter i've called the model methods. the views are .aspx pages. that's all there is for the forums system but here is the tricky part:

since MVP pattern is an UI pattern i have to do the validations of data in Application itself. so with my design the MVP is the application!

what did i do wrong??

edit 1: first of all about why i chose typed dataset with stored procedures over other options: it's the lightest data provider that doesn't compromise whatever architecture you have. other options are direct sql which is not a good option for newly created application, LINQ to sql classes which are too heavy to be instantiated with each request, Entity Framework which is great but is too much for such a simple task! at least if i was creating a blog engine it would have been my first choice. as for the why i didn't choose MVC, it's because i think MVP is the better pattern since it'll give me complete seperation of different parts of my application. at the end it's a matter of tase but you should be able to implement any pattern, right? i've seen different flavors of MVP but the one i'm trying to implement is this:

model<------------->presenter<-------------->view

the databind option is for the presentation model pattern which is introduced by martin fawler as far as i remember but the one i'm trying to create is a microsoft version which basically is an extended version of the PM pattern.

two days ago i asked a question about data validation and someone suggested that i should do it in "Application itself" so when i asked where is this "Application" he said that the MVP is an UI pattern and it shouldn't be your "Application" and you should implement the MVP in your user interface layer. so that's the reason i asked this question at the first place!

解决方案

With MVP the code behind becomes the View implementation based on a View interface.

The View interface contains the page events the Presenter will need to bind to in order to change the state of the View, as well as properties and methods it might need. For instance it could hook onLoad event and call into the View to change it's state. In doing this it's agnostic to the View implementation and knows nothing about controls. This means you can write unit tests without an ASPX instance.

To create a MVP engine that automates wire up, you would make use of a base page, generics and IoC. The IoC allows you to set dependency overrides for things like WebContextBase which is visible to only that request pipeline, and which can be passed to your Presenter constructor.

IoC also allows you to inject other dependencies Into your Presenter to help keep it light weight. This way you can move business and data access to other layers.

MVP is really useful where you have to use WebForms, such as with most CMS systems. If you aren't constrained, then use MVC.Net for new web development.

Update: As promised here's a fairly decent example. The bit it's missing is the dependency overrides which can be done like this:

var dependencies = new DependencyOverrides
                    {
                        {typeof (HttpRequestBase),new HttpRequestWrapper(Request)},
                        {typeof (HttpResponseBase),new HttpResponseWrapper(Response)},
                        {typeof (IPrincipal),Context.User},
                        {typeof (IIdentity),Context.User.Identity},
                        {typeof (IUserProfile),Context.User.Profile},
                        {typeof (HttpSessionStateBase),new HttpSessionStateWrapper(Session)}
                    };

presenter = container.Resolve<TPresenter>(dependencies);

这篇关于我有一个问题在c#和asp.net中实现mvp模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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