Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This is my first time working with WCF DataServices but I was able to get a simple entity-backed service up and running by just referencing my DBContext when I implemented my service class, for example:

public class MyDataService : DataService<MyDBContext>

Then I can simply choose which entities I want to expose and it just works which is pretty cool. But things are never that simple as a developer are they? My scenario is more complicated tough so I have two questions:

  1. In the simple case of MyDBContext above, I don't have to worry about connection strings because that context reflects a single database that is shared across the entire application so the connection string is just stored in the config and is instantiated with the default constructor. However, I also need to expose access to a second DBContext that is department-specific (lets call it DepartmentDBContext). In other-words depending on who is making the request I need to route them to their specific database (so same context, but instantiated with a different connection string depending on the request). How should this best be handled?
  2. Aside from DepartmentDBContext which my consumers will have more or less complete access to I need to also expose limited bits and pieces of data that exist in different dbs (and thus different dbcontexts) so I need to build a mechanism to do this without exposing my entire data model. Really all I will need to expose is some Ids and Name properties (so imagine something like DepartmentId, CustomerId, CustomerName, AccountID, AccountName, etc). My thought was just to wrap these properties up into a single POCO and expose that to the consumer since that's all they'll need on their end but I'm not sure how I should write this via WCF DataServices since it will not be tied to a single context.

thanks in advance for any tips or advice you can provide

share|improve this question
1. There is a method you can overload, something like InitialiseContext. – Aron Apr 19 at 17:28
2. You can use the Query Interceptors – Aron Apr 19 at 17:29
Thanks for the reply...OK there is a CreateDataSource() method you can override, is that what you mean? So I would basically change my class def to: public class MyDataService : DataService<System.Data.Entity.DbContext> and then override that method to return the appropriately initialized context? – snappymcsnap Apr 19 at 17:59
Yes. I've never done anything like that before. So it might not work given that I assume they are different Entity Models. Also you might want to google "dbcontext DataService<T>". Last I heard they weren't directly compatible and required a little hack to get the ObjectContext out instead. – Aron Apr 19 at 18:10
My question is how you are hosting your service? Are you hosting it using WCF/IIS? If yes, just implement IHttpHandler and depending on who is making the request, initialize new DataService<MyDbContext> or new DataService<DepartmentDBContent> and then call ProcessRequestForMessage entry point on DataService class. Hope this helps. – Pratik Apr 19 at 18:19

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.