开发者

WCF / Service Layer / Repository Layer: Returning DTO from Service layer? And creating ViewModel in Controller from the DTO returned

开发者 https://www.devze.com 2023-03-04 20:13 出处:网络
I have a WCF Service, behind the WCF service is a Service layer (http://martinfowler.com/eaaCatalog/serviceLayer.html) and repository layer.

I have a WCF Service, behind the WCF service is a Service layer (http://martinfowler.com/eaaCatalog/serviceLayer.html) and repository layer.

So a client calls the WCF Service layer and the WCF Service layer (acting as a service layer) calls the repository layer.

The repository layer retur开发者_开发技巧ns MODELS (POCOs) which represent the Database. Then the service layer I presume I need to convert the POCOs to DTO to transfer over the wire? Or should I just leave these as POCOs?

Once I have my given object on the service layer I return this to the client (ASP.NET MVC) whose controller is responsible for mapping the object returned from the WCF Service into a VIEWMODEL.

I want to know I am doing this right i.e. Service layer and Repository layer behind WCF Service?

And controller in charge of creating a ViewModel from the actual model that is returned from the WCF Service.

Also I wonder if it's really necessary to convert from the REAL models that the Repository would return to DTOs ready from returning from the WCF Service.


As far as your WCF service is concerned everything looks fine. You have models, a repository layer to access the data and you are returning those models to the clients. Whether you should use DTOs is debatable. If you have POCOs (not some ORM dependent objects) you should be fine. Although there are cases where DTOs might be required: for example circular object graphs which can be managed fine by the ORM but cannot be serialized over the wire (in an interoperable way).

Now to the client part: the fact that you are using WCF should be an implementation detail. So in your ASP.NET MVC application you would have a repository working with the POCOs you have imported when you svcutil.exeed your service and then an implementation of this repository which will perform the actual call to the service (probably using the ServiceClient autogenerated class). Then your controllers should work with the abstract repository and have the DI framework configured to inject the actual implementation. When a controller action calls some repository method it gets a POCO which will then by handled to the mapping layer (AutoMapper?) for conversion into a specific view model passed to the view. And the other way around: a controller action gets a view model from the view as action parameter, uses the mapping layer to map it back to a POCO which will passed to a repository which will take the responsibility of performing the required task with this model (in your case and the specific implementation that would be passing it to some remote WCF service).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号