开发者

Entity framework use transport objects to send over WCF

开发者 https://www.devze.com 2023-02-08 22:06 出处:网络
I was reading this book about WCF services. What it said was to create a WCF service which contained an entity model of the database. For example, lets say this database had a table called User. The a

I was reading this book about WCF services. What it said was to create a WCF service which contained an entity model of the database. For example, lets say this database had a table called User. The author then went to create a new class called UserDto which contained almost exactly the same fields as those in the database table for the User. The reason, he said, for doing this was that when passing data through WCF you don't want to pass the actual entity created object as it contains data that is not needed (and uses up bandwidth).

What I wanted to find out, is there some kind of class generator in order to construct these transport classes for me? If I have a database of 10 or more ob开发者_如何学运维jects I don't want to sit and hard code 10 objects in the business layer. Is there a tool that can do something like this for me (i.e. a code generator)?

Or can anyone suggest a better way of doing this?


Using POCO objects is fine. But if you want to use the normal EF-generated objects for DTO, there's absolutely no reason not to. They were specifically designed to play well with WCF, so unless your tables have a very large number of columns that you don't want to see sent down to the client, it's perfectly legitimate to simplify your code and your life by using the EF-generated classes. We're doing precisely this on a reasonably large WCF service (~5000 lines of code, ~30 entities), and we haven't run into any performance or architectural problems that using a separate data layer would have solved.

My general rule of thumb: only add an extra layer in your architecture when it becomes really obvious that not doing so will cause you a lot of headache. Any layer that you can reasonably eliminate, do so.


Take a look at EF support for POCO. There's also a POCO template that will generate those POCO object based on the Model defined in the EDMX.

EF and POCO


Read about the T4 code generator that's been part of Visual Studio since version 2008.

It can do things like that. Actually: the EF code generation templates (for POCO and self-tracking entities) are all T4 templates... as are the templates for generating views in ASP.NET MVC, for instance.

Excellent resources to get started with T4:

  • Oleg Sych's web site
  • Intro to the T4 template programming (slides from a presentation)
  • Code Generation with T4 Templates – A must have for developers

  • Customizing Entity Classes in VS2010

  • T4 templates in Entity Framework

  • Screencast on the T4 templates used in ASP.NET MVC

  • Phil Haack on T4 templates in ASP.NET MVC

and a great many more sites, if you go ask your friends Bing and Google :-)


http://visualstudiogallery.msdn.microsoft.com/23df0450-5677-4926-96cc-173d02752313

This was exactly what I was looking for!

0

精彩评论

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