On the client side, the projection is great when you are trying to reduce the payload. You need something server-side, when you need to make sure that certain data (for example, SSN) is really and safely hidden from the client.
@james suggestion - use the [NonSerialized] attribute (or JSON.NET [JsonIgnore] ) - this is a simple and efficient approach when the SSN should never go to the client.
It is too inflexible if the SSN is to be visible on the client in permitted circumstances (for example, a user viewing his own SSN or an HR person with the right to see the SSN). JSON.NET IContractResolver gives you tremendous flexibility with a dynamic solution based on authorization rules about which properties can cross the boundary of a service.
Some may consider the serializer problem as too much of a hack. They may be satisfied with the server-side projection you showed @chris_dotnet. By the way, it still makes sense to return IQueryable from the projection so that the client can reduce the network payload using a filter query.
Others prefer to define DTO ( ContactDTO ) and serialize it by wire.
[HttpGet]
public IQueryable GetContacts ()
{
return _contextProvider.Context.Contacts
.Select (p =>
new ContactDto
{
FirstName = p.FirstName,
ID = p.ID,
LastName = p.LastName
});
}
This IQueryable more reliable than the projection version because filtering can be done at the data level rather than at the server level.
On the client side, you can either define metadata for the ContactDTO type, or use the JsonResultsAdapter to map ContactDTO data to a Contact Breeze object.
Using the JsonResultsAdapter assumes that you really want the Contact type โ the type that it is formed in the business model on the server โ to be known on the client.
You may not need the server-side Contact form from your service. Many people take this very strongly. If you are one of those people, you'd better define a โDTO modelโ that represents entities as you want them to be seen on the client. This means learning to create metadata for your DTO model and writing server mapping logic to move between the DTO and your business model.
You can see how all this can become a big topic. This is the one that I will come to Breeze documentation soon. View this answer as a taste of future events. Conclusion ... you have a good choice for hiding data that users should not see.