I've been experimenting lately with Windows Communication
Foundation (WCF) and OPF3. I created two classes, one for OPF3 and
one for my WCF service. It became increasingly tiresome to maintain
both classes whenever the table was updated. I really wanted to
clean up my code by removing any
"convert-from-opf3-object-to-wcf-object" functions, so I decided to
combine both of them!
This solution worked better than I thought it would. Now I have
an object that can be loaded straight from an SQL query and
returned from my WCF contract to the client Silverlight
application. Really all it involved was adding a couple more
attributes and viola! Here's an example of what the final class
looked like.
[Persistent( Rights = PersistentRights.Load )]
[DataContract( Name = "DiveSite", Namespace = Information.DiveService.DataObjects )]
public class DiveSite : BaseServiceContract, ISelfContainingObject, IContainsFaultDetail
{
[DataMember]
[Field( "DIVESITEID" )]
public int DiveSiteId { get; set; }
[DataMember]
[Field( "DIVESITENAME" )]
public string DiveSiteName { get; set; }
[DataMember]
[Field( "PRIMARYDIVESITETYPEID" )]
public int? PrimaryDiveSiteTypeId { get; set; }
[DataMember]
[Field( "MARKERTYPEID" )]
public int MarkerTypeId { get; set; }
[DataMember]
[Field( "ISBOOKMARKED" )]
public bool IsBookmarked { get; set; }
[DataMember]
[Field( "LATITUDE" )]
public decimal Latitude { get; set; }
[DataMember]
[Field( "LONGITUDE" )]
public decimal Longitude { get; set; }
[DataMember]
public DiveSiteDetail DiveSiteDetail { get; set; }
public FaultDetail FaultDetail { get; set; }
public ObjectInfo ObjectInfo { get; set; }
public static List<DiveSite> GetDiveSites( Location northWest, Location southWest )
{
return ObjectContextFactory.ObjectContext.GetObjectSet<DiveSite>( new SqlQuery( "EXEC spGetPublicDiveSites " ) ).ToList();
}
}
With the addition of the DataContact and DataMember attributes I
now could have one class that could be loaded with OPF3 and then
sent to the client. Really helped with cleaning up any "conversion"
functions.