I'm playing around with WCF REST on .NET 4.0, and I ended up wanting to find out if the following idea is somehow possible to implement?
I have a very basic set of POCOs against an EF 4.1 CF project. All of them inherit from a base Entity
class (for the sole purpose of defining a PK).
public abstract Entity {
public int Id { get; set; }
}
Now, for the most part, I'll have separate operations for the CRUD functionality for each of my entities (or aggregates, on larger systems I guess).
e.g.
[ServiceContract]
public class UserService {
[OperationContract]
public void Add(User user) {
// commit to EF
fooContext.Users.Add(user);
fooContext.SaveChanges();
}
}
Now, this is primarily just for convenience, but I was thinking that something like this would be really handy:
[ServiceContract]
public abstract class BaseCrudService<T> where T : Entity {
[OperationContract]
public void Add(T entity) {
// commit to EF via generic methods
fooContext.Set<T>().Add(entity);
fooContext.SaveChanges();
}
}
public class UserService : BaseCrudService<User> {
// blah
}
... which would theoretically allow me to access that operation via http://<root>/userservice/add
and work with it as I would expect to, except that I can't inherit a ServiceContract
-decorated class, so there's no hope of automagically cascading functionality down to the endpoints themselves.
I was wondering: is there a way to do this somehow? I know that inheritance (in this manner) won't cut it, but are there other ways to do this? Repeating similar pieces of code across multiple service operations just sounds like something that somebody would have thought of before and fashioned a cleaner modus operandi of implementing.