Reputation: 612
I just got off a small project using a WCF/REST Web Service that used Entity Framework to access a database, apply some minor business logic, and pass the results to .NET Webforms front end. That worked pretty well, but the initial design was thrown together without a lot of time to investigate options, and now I'm being asked to do another very similar project. I have a bit more time now to see what's out there and potentially do this project better than the first.
I want to find out if there are better ways to accomplish the a few things, specifically around passing objects back and forth between the web service and the front end.
For the first project, I created a shared library with class definitions that both the web service and the Website used. When a request came in to the web service, DB queries were performed, business logic was applied, and an object of one of the shared types was created and passed back in the web service response.
When the website got the response, it deserialized it to the appropriate shared object and went on its merry way.
It worked fine, but there was a lot of code that had to be written to define a shared object, then map the query results/business logic results to the shared object.
Is there a better way? Specifically what I'm looking for would be a way to:
automatically generate objects. (I'm not sure there is - it seems like I could autogenerate objects that were specifically mapped to database entities like tables or views (and indeed, that's what the Entity Framework does) but what about objects that need to include non-DB info like stuff the business logic might add?)
remove the requirement for a shared library between the Web Service and the Website. I suspect this might exist, but I'm not even sure where to start on it.
Upvotes: 1
Views: 502
Reputation: 67085
I am working on a similar project and here is what I am doing (you can decide if it works for you or not).
So, I end up with 4 models as summarized below:
This, as already mentioned above, allows for a separation of concerns. I can potentially change my persistence model without the need to change my contracts set in the service (which will be public). This will make it so that internal changes do not break external applications. You do run the risk of having to change all models, though.
As to the boilerplate of mapping them all together, I am using AutoMapper. So, the code is not cluttered up with mappings due to AutoMapper, RestSharp, and ServiceStack. The code looks like code :)
I am not sure if that entirely answers your question, but it does sound kind of like what you are explaining.
UPDATE:
This is to try to take the above and make it even more specific to the BDW's ASP.NET solution:
Upvotes: 2