locked
Best practices for exposing DTOs via Service Operations on ADO.NET Data Services RRS feed

  • Question

  • I would like to know what the best practices are for utilizing and exposing DTOs in an ADO.NET Data Service. As much as I love all the possibilities that can be achieved with projections, there are scenarios in which I'll need to do a custom query that returns a DTO which doesn't conform to an entity in my domain model. I've tried to find more information on this subject, but so far I've only read that custom service operations can only return entities from the Entity Data Model that is being exposed by the service. Is my only option to create an Entity in the model that isn't mapped to a table in the database? Because when I try this method, I get the error:

    The server encountered an error processing the request. The exception message is 'Service operation 'MY_DTO' produces instances of type 'MY_DTO', but there are no visible entity sets for that type. The service operation should be hidden or a resource set for type 'MY_DTO' should be made visible.'.

    here is the service operation:  
    [WebGet]
    public IEnumerable<MY_DTO> TestDto(int parentId)
    {
    	return CurrentDataSource.MY_ENTITY
    		.Where(x => x.Parent.Id == parentId)
    		.Select(x => new MY_DTO { Id = x.Id, Name = x.Name, ChildCount = x.Children.Count });
    }
    

    Monday, November 9, 2009 10:32 PM

Answers

  • Sam

    I'm afraid that you would have to create a new entity set which will make My_DTO a resource type. The easy way here I think is to use "DefiningQuery" in the SSDL. For more info see http://msdn.microsoft.com/en-us/library/cc982038.aspx. Keep in mind that if you don't have the Insert/Update/Delete queries defined, this "virtual" entity set will be read only.

    This is a pretty tedious experience and is not as flexible as we would like. However, we have a few ideas in how we can improve this in the next few iterations of data services,

    Regards,
    PQ

    Peter Q. http://blogs.msdn.com/peter_qian
    Wednesday, November 11, 2009 11:19 PM
    Answerer

All replies