• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

MVC3 and WebService objects as models

I am on a project that is considering tightly binding a Razor Page (cshtml file)  to a web service object.  And I have been told to find out if there are any pitfalls to this approach.

In my past MVC projects, I have create a View Model as a POCO class, and used Data Transfer Objects to transfer values from the POCO to the back end methodology, with that's to a backend DLL, a Web Service or Entity Framework.  But here the suggestion is to strongly type the model with the object.  

Does any see any problems with that approach?

I need to dig into the exception handling plans for that, in the event the web service is not reachable.

Any other problems that anyone sees?

Thanks.
0
newbieweb
Asked:
newbieweb
  • 3
  • 3
4 Solutions
 
Bob LearnedCommented:
Hmmm...web service objects lose some in the transfer.  You can't have any logic in the web service object, since it won't transfer through the serialization process.  That's OK, if you like to have property-only classes, but a lot of times, it is nice to have behavior in your objects, especially with MVC.

What problem is the web service solving?  Traditionally, web services are designed to provide a centralized service.  That is over-simplified, but is useful in this case.  There are bandwidth limits to consider, versus directly hitting the database.  

One example, that I can think of, is using a web service to hit an Oracle database.  This can really help, if you have a lot of users, and you don't want to install the Oracle client on all the machines, but have it installed one place (on the server).
0
 
Kelvin McDanielSr. Developer/ConsultantCommented:
I don't see a problem with your approach that at all.

As long as your front-end models inherit from the services objects, you can put as much behavior in them as you like.
From the front-end to the back-end, all they need to be are property bags... and in my opinion the same is true going the other direction. From what I've seen, the only scenario where this isn't true is when two or more layers need the object to "do" the same exact "thing"... which usually indicates a flaw in the design EXCEPT for formatted string output.
This would also help towards proper separation of concerns through your tiers/layers.
0
 
newbiewebAuthor Commented:
I did discover that the plan is to use strongly typed DTO objects which were not defined in the web service as I had thought, but instead in our back-end core logic.  So right-clicking the model and selecting "Go To Definition" opens Reference.cs and points to the object as defined in the DLL for the backend.

So, now my question has become more refined...

Is there any direct value to passing a POCO between the the View and the Controller which is MORE THAN a DTO?  What kinds of useful properties could be added to the POCO for greater local value to the UI solution?

Thanks.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Kelvin McDanielSr. Developer/ConsultantCommented:
There is huge value. Also, this is the preferred way of dealing with submitted data, either via a HttpPost or method call.

This can be used to differentiate which method the Controller will use when specific things happen... as well as it makes processing the submitted values (either in the Controller method or in the View) much simpler and well-defined because everything is strongly typed. Instead of having to pass a slew of parameters or "guess" at which fields are going to be in the FormCollection, you pass one object and know what is or isn't in there. As a bonus you also get IntelliSense without having to do any extra work.
0
 
newbiewebAuthor Commented:
I refined my question in my prior post.

What do I lose when the model is just properties: a DTO object, which in this case, is defined on the backend.  What cool things can I do when the strongly typed model has function and "can do things"?
0
 
Kelvin McDanielSr. Developer/ConsultantCommented:
As long as the web project builds you should be fine... which means that whatever functions the DTO can "do" should be possible from within the web project.

I'll caution you that this doesn't necessarily mean you *should* so somethings, just because you can or it's possible. For example, you probably don't want to update the database directly from your Controller method, if the DTO exposes that capability.
0
 
newbiewebAuthor Commented:
Thanks!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now