The WCF Data Services allows you to easily expose your data as a web service. db4o supports this framework. Look up the MSDN documentation on WCF Data Service for more details about it and its features.
The are two things which the data service need. An IQueryable implementation and a IUpdatable implementation. db4o provides both. For this you need to reference the Db4objects.Db4o.Linq.dll and Db4objects.Db4o.Data.Services.dll. The first one contains the LINQ-provider, the second contains the IUpdatable.
The first thing you need to do is to add a key to your objects. And you need specify at least one key for each object.
[DataServiceKey("Email")] public class Person { private string name; private string email; // Note that a default constructor is required for WCF public Person() { } public Person(string email, string name) { this.email = email; this.name = name; } public string Name { get { return name; } set { name = value; } } public string Email { get { return email; } set { email = value; } } }
[DataServiceKey("TeamName")] public class Team { private string teamName; private string motivation; private IList<Person> members = new List<Person>(); public Team() { } public Team(string teamName) { this.teamName = teamName; } public string TeamName { get { return teamName; } set { teamName = value; } } public string Motivation { get { return motivation; } set { motivation = value; } } public IList<Person> Members { get { return members; } set { members = value; } } }
After that, you can build a data context. For this, inherit from the Db4oDataContext. You need to overwrite the OpenSession() to provide the right object container. A good practice is to use a object container per request. For example you can use the OpenSession-operation for creating a container per request. Take also a look how you can provide a object container for each request.
After that, you can add operations and properties you want to expose. Remember to use the IQueryable interface to expose query options to the client.
public class TeamDataContext : Db4oDataContext { // Provide access to your data via properties public IQueryable<Person> Persons { get { return Container.AsQueryable<Person>(); } } public IQueryable<Team> Teams { get { return Container.AsQueryable<Team>(); } } /// You need to implement the open-session and return a object container /// The best practise is to use a separate object-container per request. /// For example use the /// <see cref="IObjectContainer"/>.<see cref="IObjectContainer.Ext"/>.<see cref="IExtObjectContainer.OpenSession"/> /// to open session-containers for each request. protected override IObjectContainer OpenSession() { return Db4oEmbedded.NewSession(); } }
The last step is to actually create a service. Visual Studio can assist to do this. Right click on the Project, choose 'Add'-> 'New Item'. Then Choose the 'WCF Data Service'. In older releases it's called 'ADO.NET Data Service'.
After that, you can rename the created classes and parameterize it with the previously created context. Then you need to specify which operation are allowed. Read more about how the configure the allowed operation in the data service documentation.
public class TeamDataService : DataService<TeamDataContext> { public static void InitializeService(DataServiceConfiguration config) { config.SetEntitySetAccessRule("*", EntitySetRights.All); config.SetServiceOperationAccessRule("*", ServiceOperationRights.All); } }
Now you can startup this service and you're ready to consume it. For example you can create a simple console application. There you import the service. Right click on the project choose 'Add Service Reference'. Then point the assistant to the URL of the running web service. After that you can use the service.
var serviceURL = new Uri("http://localhost:8080/TeamDataService.svc"); var serviceContext = new TeamDataContext(serviceURL); var teams = serviceContext.Teams; foreach (var team in teams) { Console.Out.WriteLine(team.TeamName); }