I have a complex, [Serializable] object stored in session. I have Silverlight 3.0 islands in my .aspx pages that need access to this data and its data type. It is my understanding that Silverlight does not support [Serializable], and since it is running on the client, it does not have easy access to session. I am looking for a solid way to access this data in my Page.xaml.cs file.
I am open to storing it in ISO Storage once it has been retrieved, but how to retrieve, read it from Silverlight? Hidden fields are not an option as it is a complex data type with dozens of properties, and a few dictionaries, lists of other objects.
The classic way of accessing this type of data would be with a silverlight-enabled WCF service on the ASP.NET site that accesses the data. You then add a service-reference from the silverlight client and ask the server for data (asynchronously).
Note that by default this will be a separate object model (proxies from "mex"). If you need the same type you'll have to repeat the code in the client (you can't really use assembly sharing between client and server here).
I don't know whether the silverlight version of svcutil will allow type re-use (the regular version does), but if not another option is to just return xml or binary from the service and deserialize locally. One option here would be something like protobuf-net.
Related
Not sure how to ask this question but I'm looking for a clean way to solve a problem for a project. I'm trying create a class library that will allow someone to create a randomized character for a game and use the data for a web app. I have a CharacterModel class that has all the needed data publicly exposed, however I need a way to edit this information without making the setters publicly available. Also, if I have multiple characters in the future, how do I get the editor to effect specific objects? Is there a design pattern for this type of problem?
Make the setters public. If someone is using your library to create instances of CharacterModel and they plan to use it in a web app, they will know how to wrap access to the data in a safe way.
They will most likely use your library to create instances of your class, then store them serialized in a data store. When they send the data to a user of their app to look at and interact with, they'll either expose it through a web api of some kind to be accessed by a front-end SPA client or they will use a framework to render the data as HTML (e.g. ASP.NET Core w/ razor pages or mvc).
Whenever the user of their app requests changes to the object they will pass requests from the webpage or from the SPA client back to the server (either controller endpoints or through form posts) and they will use an auth system, character model identifiers and other code to determine what changes the user can apply before they save the changes to the data store (usually a database or a document/nosql system).
In other words, in relation to your question:
"...if I have multiple characters in the future, how do I get the editor to effect specific objects?"
...you'll let the consumer of your library figure that out when they are building an application with it.
Currently I'm writing a Web Application that needs to access the database quite often to retrieve records.
Now, I want to retrieve records and store them in the cache, and that's not a problem, but assume that I need to cache also in a Windows Application, what kind of object is best chosen then to do in-memory caching? IList<>, List<>, array, ...
So, in fact I would like to setup something general, and based on the type of application the appropriate type of object to store the items in will be choosed.
Take a look at System.Runtime.Caching namespace. Maybe it fits to your scenario.
I'm putting together a plan for an Xml web service to go into a client's site to be consumed by third-parties so that they can access the client's data.
My question is really asking about best practises here, and at the moment I am deliberating over 2 different strategies:
1) Create an object model which represents my Xml data and serialize it (either explicitly, or implicitly by exposing the data through a Wcf REST endpoint)
2) Transforming my domain model directly into hand-crafted Xml using XLinq and returning this as a string from the service, setting up the response headers appropriately
I like (1) because I let the system do the generation of the physical Xml and I work purely within the object model, but versioning becomes a problem and I might need finer control over the output.
I like (2) because I do get the fine control and versioning becomes easier, but I'm now hand-crafting Xml and the opportunity for error escalates.
Any opinions? Am I missing something which gives me the best of both worlds? I would go straight for (1) if I knew the best way to 'version an object model' - would using different namespaces suffice?
I'd use serialization. As long as you don't try to use your domain objects for serialization you can get pretty fine grained control over the XML either via the DataContractSerializer or the XmlSerializer. You can then map between your domain objects and your serialization objects using something like AutoMapper
What are the pros and cons of the following 2 cases:
Case I:
Traditional way:
Add service reference in project. Create object and Get data from service on server side and bind to asp.net grid.
Case II:
Update Service for JSON behavior. Add service reference in project. Call service from javascript to get data. Bind data to jquery grid.
Which one is the best approach and why?(Not developer point of view)
If there is another approach which is more optimized, please explain it and consider for large data.
It depends on whether end-clients (browsers) are allowed to have access to the WCF data service, or just the app service. For simple security modes, having json allows a lot of very simply jQuery etc scenarios.
Of course, jQuery etc demands a compatible browser; these days that means "most", but by no means "all". So if you want to provide the same data to dumb browsers you'll need a way to get the data at the server.
If the intend is to provide server-to-server (B2B etc) access, json is generally a second choice; xml (SOAP etc) would be the de-facto standard, but it isn't the only option. For example, if you have high bandwidth needs you might choose a more compact binary transmission format (there are many).
second approach. any client can now consume that data, whether it'd be jquery grid or even an iphone client.
I am working on a webservice where we use LINQ-to-SQL for our database abstraction. When clients use our webservice, the objects are serialized to XML and all is dandy.
Now we wish to develop our own client that uses the native data types since there's no reason to do objects->xml->objects. However, from what I understand you can't transfer LINQ objects as they directly map to the database and as such the data is "live".
My question is whether there is a way to take a "snapshot" of the data you've extracted, set the LINQ object to "offline" and then transfer it. The data will not be changing after it's transferred to our client and we don't need the database access.
The LINQ-to-SQL classes can be used with DataContractSerializer (for WCF) easily enough (you need to enable the serialization property in the designer, though). With this in place, you should be able to share the data assembly with the client. As long as you don't use the data-context, the objects themselves should be well behaved (just disconnected - so no lazy loading).
The trick is that you need to re-use these types (from the existing assembly) in your serialization code. If you are using WCF, you can do this with svcutil /r, or via the IDE.
That said though; it is often cleaner to maintain separate DTO classes for these scenarios. But I'm guilty of doing it the above way on occasion.
If you're willing to use WCF (for the webservice and the client) you can decorate your Linq2SQL generated classes with the [DataContract] and [DataMember] attributes.
Check the following links for some guidance:
http://msdn.microsoft.com/en-us/library/bb546184.aspx
http://msdn.microsoft.com/en-us/library/bb546185.aspx
http://www.codeproject.com/KB/WCF/LinqWcfService.aspx
http://www.aspfree.com/c/a/Windows-Scripting/Designing-WCF-DataContract-Classes-Using-the-LINQ-to-SQL-Designer/