Personally, I think this is a very bad idea. If anything you want to create a new POCO for example and map your Entity to that new object and fire that down the wire.
However, 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, and we haven't run into any performance or architectural problems that using a separate data layer would have solved.
This is an example from an existing application using the POCO / DataContract idea.
public ServiceResponse<IList<Pdr>> GetAllPdrsForPrimaryJobProfile(PdrRequest request)
{
var result = new ServiceResponse<IList<Pdr>>();
try
{
// Get the user for the specified username
var user = GetUser(request.UserName);
if (user != null)
{
// Does the user have a current job profile?
if (user.JobProfile != null)
{
var assignment = new UserJobProfile(user, user.JobProfile);
var pdrs = assignment.GetPdrs(false);
result.Response = new List<Pdr>();
foreach (DataRow row in pdrs.Tables[0].Rows)
{
var pdr = new Pdr
{
PdrId = (Guid)row["PDRID"],
UserId = (Guid)row["UserID"],
JobProfileId = (Guid)row["JobProfileID"],
StartDate = Convert.ToDateTime(row["StartDate"]),
DueDate = Convert.ToDateTime(row["DueDate"]),
FormNumber = (int)row["FormNumber"],
Status = row["Status"].ToString(),
DateModified = (row.IsNull("DatePDRModified") ? DateTime.MinValue : Convert.ToDateTime(row["DatePDRModified"]))
};
result.Response.Add(pdr);
}
result.Status = Status.Success;
}
else
{
// No job profile for user
result = EmptyResponse<IList<Pdr>>(Status.Failed, "User does not have a primary job profile.");
}
}
else
{
// No user found
result = EmptyResponse<IList<Pdr>>(Status.Failed, string.Format("No user found for username {0}.", request.UserName));
}
}
catch (Exception ex)
{
// Unknown error
result = EmptyResponse<IList<Pdr>>(Status.Failed, ex.Message);
}
return result;
}
Madha Dhanas...
Member
391 Points
188 Posts
WCF with Entity Framework
Nov 06, 2012 09:38 AM|LINK
hi all,
i have one doubt, i have develop one application using entity framework and wcf
integrating wcf and entity framework., can we expose the entity layer through service contract or not ?
Ragards,
Madha.V
Dave_Winches...
Contributor
3051 Points
716 Posts
Re: WCF with Entity Framework
Nov 06, 2012 09:54 AM|LINK
Hi
Personally, I think this is a very bad idea. If anything you want to create a new POCO for example and map your Entity to that new object and fire that down the wire.
However, 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, and we haven't run into any performance or architectural problems that using a separate data layer would have solved.
Check out this article for a quick guide http://www.codeproject.com/Articles/127395/Implementing-a-WCF-Service-with-Entity-Framework
Hope this helps.
/D
Please mark as ANSWER if this is the solution.
Madha Dhanas...
Member
391 Points
188 Posts
Re: WCF with Entity Framework
Nov 06, 2012 10:34 AM|LINK
if possible can u give some example in framework 3.5 [wcf with EF]
Ragards,
Madha.V
Dave_Winches...
Contributor
3051 Points
716 Posts
Re: WCF with Entity Framework
Nov 06, 2012 10:48 AM|LINK
This is an example from an existing application using the POCO / DataContract idea.
public ServiceResponse<IList<Pdr>> GetAllPdrsForPrimaryJobProfile(PdrRequest request) { var result = new ServiceResponse<IList<Pdr>>(); try { // Get the user for the specified username var user = GetUser(request.UserName); if (user != null) { // Does the user have a current job profile? if (user.JobProfile != null) { var assignment = new UserJobProfile(user, user.JobProfile); var pdrs = assignment.GetPdrs(false); result.Response = new List<Pdr>(); foreach (DataRow row in pdrs.Tables[0].Rows) { var pdr = new Pdr { PdrId = (Guid)row["PDRID"], UserId = (Guid)row["UserID"], JobProfileId = (Guid)row["JobProfileID"], StartDate = Convert.ToDateTime(row["StartDate"]), DueDate = Convert.ToDateTime(row["DueDate"]), FormNumber = (int)row["FormNumber"], Status = row["Status"].ToString(), DateModified = (row.IsNull("DatePDRModified") ? DateTime.MinValue : Convert.ToDateTime(row["DatePDRModified"])) }; result.Response.Add(pdr); } result.Status = Status.Success; } else { // No job profile for user result = EmptyResponse<IList<Pdr>>(Status.Failed, "User does not have a primary job profile."); } } else { // No user found result = EmptyResponse<IList<Pdr>>(Status.Failed, string.Format("No user found for username {0}.", request.UserName)); } } catch (Exception ex) { // Unknown error result = EmptyResponse<IList<Pdr>>(Status.Failed, ex.Message); } return result; }Hope this helps.
/D
Please mark as ANSWER if this is the solution.
Madha Dhanas...
Member
391 Points
188 Posts
Re: WCF with Entity Framework
Nov 06, 2012 11:41 AM|LINK
Thanks for ur reply i ll try and let u know
Ragards,
Madha.V