Exposing a .NET class library (which primarily defines CRUD operations) as a service

僤鯓⒐⒋嵵緔 提交于 2020-01-03 10:22:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!