问题
What is the best, efficient and fastest way to expose an existing (class) library (which primarily defines CRUD operations ) as a service (WCF Service or WCF Data Service), so that it can be used with Silverlight or Ajax. Are there tools (code generators, RAD tools), which can support this ? Thanks in advance for your help and hints.
回答1:
The best approach is to use WCF to create a wrapper yourself.
You should do this, rather than using some automation to just expose the library directly because:
- Security, do you want anyone to call anything on the library anytime?
- Most libraries assume they are called directly rather than over a service (see fallacies of enterprise development).
- WCF is by default stateless: you need to work out how to manage any state the library assumes (you will no longer have a single client).
- Did I mention security?
回答2:
If you class is just a dumb collection of data, just throw a DataContract on it. (Don't forget the namespace, otherwise you will kick yourself later.) You can then expose it using a web project.
If you have actual logic in your class then you are in trouble. There is no good way to share business logic with Silverlight apps. They try with RIA Services, but it just doesn't make the grade.
回答3:
You should take a look at WCF Data Services, especially in .NET 4. While you'll have to create the data context class or classes to expose your entities along with exposing IQueryable and implementing IUpdatable, you then can take advantage of the supporting framework that WCF Data Services provides along with a standardized protocol (OData) for your data payloads.
In .NET 4 and Visual Studio 2010, WCF Data Services are becoming more accepted, and are being pushed by Microsoft as a good data access vehicle for Silverlight applications.
I think it's at least worth checking out. There's a lot of information about it on MSDN, although I don't think it's organized very well in places. Here's a link to the section in MSDN on rolling your own WCF Data Service using the built-in reflection provider. (The example only shows data retrieval because it's much simpler than data updates/inserts/deletes, but there's a link in the article on how to implement IUpdatable.)
Getting an IQueryable exposed through WCF Data Services should be pretty quick. IUpdatable will take a little more time (since you need to implement Insert/Update/Delete for each entity). But once you get it up and running (which shouldn't take too long), you can then tweak the security settings, add custom service methods, and add additional functionality and/or entities pretty easily. It's a nice framework for what you're describing.
I hope this helps.
来源:https://stackoverflow.com/questions/3235522/exposing-a-net-class-library-which-primarily-defines-crud-operations-as-a-ser