i have a problem implementing the mvp pattern in c

2019-08-22 07:12发布

问题:

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!

回答1:

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);


回答2:

You made an architecture mistake by choosing webforms over the ASP.NET MVC framework. Also typed datasets are so 2003, you should use Entity Framework. Get with the times!



回答3:

Have you looked at ASP.NET MVC? It's the Model-View-Controller pattern, and will handle all the routing and plumbing for you. You'll get easy model validation with DataAnnotations, and there's plenty of Resources and Tutorials

Or is there a specific reason why you need to use the MVP pattern with classic ASP.NET?



回答4:

Since your question was not about the merits of MVC vs MVP or Typed Dataset vs Entity Framework, I am assuming you have a compelling reason to do things the way you are doing them.

With that out of the way, you don't seem to be doing anything wrong. With MVP, validation is supposed to take place in the Presenter implementations, as they have access to your view instances during postbacks. Of course you could also use client side validation with javascript.