I'm new to Flex, and I'm wondering about the best practices when it comes to getting data from a database and displaying it in a Flex (Flash) swf. Currently I have some C# code that gets the data from the DB and saves it to an XML file on my site. Then the .swf reads that xml file.
Is that the best way to do it, or is there a better or more standardized way? Thanks
protected void Page_Load(object sender, EventArgs e)
{
DataTable _dt = new DataTable();
_dt = ProductList.G开发者_如何学CetProductssForAdmin(10);
_dt.TableName = "Products";
_dt.WriteXml(Server.MapPath("xml/Flex.xml"), false);
}
You really should use WebORB or FluorineFX to achieve this. It sends binary data(amf) and it's way more performant. You can map flex classes to .net classes which makes it all very easy!
If you install WebORB or FluorineFX, you get a load of examples + very clear documentation.
I really wouldn't recommend using xml.
I think the answer really depends on a lot of factors. Although a using a three tiered approach (Database -> Web Service -> Frontend) is the SOP, consider the following:
1) The quantity of data: If your application is using a large amount of data, or a varying subset of a larger database, then using a three tiered approach is going to be best. However if it's a small amount of data then a flat file is a simple a straightforward solution.
2) How often does the data change: If it changes a lot, then the three tiered approach is best. You might even consider a solution outside of ASP.NET using Livecycle or Blaze if the data is changing very often and you want to push changes to the Flex frontend. However if the data you are using is updated infrequently, then your approach is again, simple and straightforward.
3) Security: Using an XML file is a pretty secure solution. It is disconnected from your database, and short of someone gaining write access to your web server the flow of data is going to be one way. However, if you create a web service and connect it to your database, you have to take steps to ensure the security of your data from SQL injection and other attacks. If this is sensitive data (as if there is any other kind) and you aren't experienced in creating a secure database service, you may want to stick with your current system.
That said my work almost always requires a three tiered approach, and it is the most powerful way to go. Just be sure that if you go that route you've considered all the factors, number 3 in particular.
EDIT:
There should really be another item in that list, which is closely related to number 1:
4) What are you doing with the data: If you are just rendering the same set of data as a table in the application or something along those lines then an XML file is a decent solution. If you are doing a lot of XPath type queries on the returned XML before it is being used then the three tiered approach has the advantage.
Another perfectly viable option that preserves the separation of concerns is to use JSON rather than XML. It's pretty well supported on both sides of the connection (web service & client), and it simplifies the marshaling and un-marshaling process in Flex quite a bit.
The best way to do it is to create a web service. In Visual Studio, create a new Web project, and select ASP.NET Web Service Application.
The following tutorial should help you get started in writing a web service using ASP.NET.
http://www.15seconds.com/Issue/010430.htm
Also, here's how to consume a web service with Flex.
http://www.adobe.com/devnet/flex/articles/flexbuilder_ws.html
精彩评论