Working with C# Object on Disk - c#

I understand how serialization works and was wondering if there is a way to store an object on the disk and work with the object and save the changes.
I am trying to avoid directly doing this:
Opening the file
Deserializing the object
Changing the object
Serializing the changes by overwriting the old file
Is there a class that allows a file to be used as an object store, namely List<object> and working with it directly on the disk without having to complete the above processes?

Try DB4O. It seems to be a solution for your requirements.

I dont believe there is an out of the box solution for this. Just search on how to save arbitrary data in a file, and think up your own format.

You probably want to look at something like ESE which comes with Windows. There is a managed interface for it. Never used it though.
Either that or use a lightweight database e.g. SQLite, since effectively, if you want to add, remove and modify data on the disk, some kind of database is what you need.

Objectivity is another alternative. It supports computing across vast distributed networks or embedded in stand-alone devices that simply must not fail, enables persistent object management, virtually instantaneous traversal of complex, many-to-many relationships and graphs, and much more.

Related

Best way to handle large amount of permanent data

I'm developing a PC app in Visual Studio where I'm showing the status of hundreds of sensors that are connected via WiFi. The thing is that I need to hold on to the sensor data even after I close the app, so I'm considering some form of permanent storage. These are the options I've considered:
1) My Sensor object is relatively compact with only a few properties. I could serialize all the objects before closing the app and load them every time the app starts anew.
2) I could throw all the properties (which are mostly strings and doubles) into a simple text file and create a custom protocol for storage and retrieval.
3) I could integrate a database with my app. Someone told me this is the best way to go about it, but I'm a bit hesitant seeing as I'm not familiar with DBs.
Which method would yield the best results in terms of resource usage and speed? Or is there some other, better way to go about this?
First thing you need is to understand is your problem. For example, when the program is running do you need to have everything in memory at the same time or do you work with your sensors one at a time?
What is a "large amount of data"? For example, to me that will never be less than million (or billion in some cases).
Once you know that you shouldn't be scared of using something just because you are not familiar to it. Otherwise you are not looking for the best solution for your problem, you are just hacking around it in a way that you feel comfortable.
This being said, you have several ways of doing this. Like you said you can serialize data, using json to store and a few other alternatives but if we are talking about a "large amount of data that we want to persist" I would always call for the use of Databases (the name says a lot). If you don't need to have everything in memory at the same time then I believe that this is you best option.
I personally don't like them (again, personal choice) but one way of not learning SQL (a lot) while you still use your objects is to use an ORM like NHibernate (you will also need to learn how to use it so you don't get things a slower).
If you need to have everything loaded at the same time (most often that is not the case so be sure of this) you need to know what you want to keep and serialize it. If you want that data to be readable by another tool or organize in a given way consider a data format like XML or JSON.
Also, you can use mmap-file.
File is permanent, and keep data between program run.
So, you just keep your data structs in the mmap-ed area, and no more.
MSDN manual here:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366556%28v=vs.85%29.aspx
Since you need to load all the data once at the start of the program, the database case seems doubtful. The DB necessary when you need to load a bit of data many times.
So first two cases seem more preferred. I would advice to hide a specific solution behind an interface, then you'll can change it later.
Standard .NET serialization of sensors' array is more simple probably, and it will be easier to expand.

How to properly save application data for later use

Ok, so I am working on a c# windows forms application and it uses different types of structures that hold data and display to the user. I want to use a saveDialogBox to allow the user to save the information(i.e configuration, state). The only way I can think to do this is to make a routine that goes through the structures and write the corresponding elements to a text file. Upon loading this routine would be used to load the data back.
This is of course a dumb way to do it I'll admit. Anything I've done in school was only writing to text files. Is there other ways to make some formatted file to save and load from?
I've been looking at serialization to save objects to files. I am not too sure how all this works though. help.
to save your application setting .. I think these links will help you
http://msdn.microsoft.com/en-us/library/aa730869%28VS.80%29.aspx
http://www.thescarms.com/dotnet/AppSettings.aspx
and
How to use settings in Visual C#
My 'Old School' way of doing this has always been to save settings during the program execution to a database (providing that you take the time to ensure you're not hammering the database with updates / inserts).
If my application needs to be more efficient AND I need to easily be able to recall the saved settings I serialize to XML using System.Xml.Serialization (from memory). XML serialization is human readable which is helpful (but not the most efficient in terms of processing time).
If I need even more efficiency you can go the whole way and serialize to binary.
I'd suggest reading / understanding http://msdn.microsoft.com/en-us/library/Vstudio/ms233843.aspx in it's entirety before coming back here. I'd say once you read this you'll be far better equipped to make a decision on which way you want to take your application.
In my experience there aren't that many DUMB ways to solve problems however there is almost always a better way to solve them given enough time and research.

How To Serialize A .net Class To Disk

I'm trying to serialize an object cache to disk so it can be loaded the next time the program is loaded.
One of the features of the class being saved is it contains references to other objects.
For example:
I have a list of an image class that stores remote url, local filepath, if it's been downloaded etc.... I then bind visibility to downloaded and the source to the local filepath.
Other Objects have a reference to this image so when the image is downloaded it's updated once and all the bindings update across all items that are pointing at it.
As a quick fix I implemented a binary formatter and all is working correctly. All my lists are serialized to disk and when I reload them all the references remain (I.E 1 image object is created and everything that uses it has a reference as opposed to deserialisation creating a new instance of Image everytime it appears)
My question is what kind of Serialier I should be using to store to disk whilst not breaking my references? I've read that BinaryFormatter is a BAD choice for serializing to disk and expecting it to work across different releases. Although I've had no issues with it so far I don't want to run into problems a year down the road and force all my users so re-aquire all their cached metadata.
I'm not 100% sure how all the different serializers work but I presume I may need to write some kind of convertor if I were to use XML. If it helps, all of my image objects have a GUID assigned to them so I have something unique about every object.
UPDATE: I've just found the following question which looks similar Maintain object references through Serialize/Deserialize
Can anyone tell me if Datacontractserializer is a good choice for long term serialization storage across different versions of an applciation Vs the downsides of binaryformatter?
No need for any converters and stuff, just check this out:
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
SqLite may help in it, its easy to use.
MongoDb may be useful. See MongoDB for C#
or other type of DataBase: DB4Object
and seems be SQLite useful. use blobs for store images as binary.

Looking for the most painless non-RDBMS storage method in C#

I'm writing a simple program that will run entirely client-side. (Desktop programming? do people still do that?) and I need a simple way to store trivial amounts of data in a structured form, but really don't see any need to use a database system. What's more, some of the data needs to be serialized and passed around to different users, like some kind of "file" or perhaps a "document". (has anyone ever done that before?)
So, I've looked at using .Net DataSets, LINQ, direct XML manipulation, and they all seem like they would get the job done, but I would like to know before I dive into any of them if there's one method that is generally regarded as easier to code than others. As I said, the amount of data to be stored is trivial, even if one hundred people all used the same machine we're not talking about more than 10 MB, so performance is not as large a concern as is codeability/maintainability. Thank you all in advance!
Sounds like Linq-to-XML is a good option for this.
Link 1
Link 2
Tons of info out there on this.
Without knowing anything else about your app, the .Net DataSets would likely be your easiest option because WriteXml and ReadXml already exist.
Any serialization API should do fine here. I would recommend something that is contract based (not BinaryFormatter, which is type-based) as that will keep it usable over time (as your assembly changes).
So I would build a basic object model (DTO) and use any of:
XmlSerializer
DataContractSerializer
protobuf-net (you all knew it was coming...)
OO, simple, and easy. And easy to use for passing fragments of the data (either between users of to a central server).
I would choose an embedded database. Using something like sqlite doesn't seem to be an overkill for me. You may even try its c# port (http://code.google.com/p/csharp-sqlite/).

C#: Storing a .Net Object in the Registry

Is it possible to store a .Net object in the registry?
I want to store a generic List<> in the registry, and then retrieve it and parse it back to the List.
Is this possible, or do I need to manually serialize it and then deserialize it back?
[UPDATE]
Following the posted answers, I am going to serialize the object and save it in the Current User's AppData folder.
It's possible, if the type included in the list is serializable. If that's the case, you can serialize it and store it in a string value.
But it's probably a very bad idea. The registry gets big enough as it is. Instead, put this kind of thing on the file system, in the All Users Application Data folder.
Why the registry?
The appropriate place to store these kind of serialised objects is usually in the users' Application Data folder or Isolated Storage. Though of course the method of serialisation is up to you. XML, binary etc, it's essentially a file on disk.
You could consider things like "Local Database" or SQL Server Express, depending on your data and concurrency needs.
Some applications do store a Most Recently Used (MRU) list in the registry, but that's just by iterating all the values of a given key. I don't recommend that approach.
Yeah, I think you'd have to serialize and deserialize it yourself. But you could store it either as a binary block or text/xml. It's possible that there is a size limit to registry data...
The big question is "is this a good thing to do?"
You will have to serialize it yourself. Beware that there might be limitations on the amount of data you can store, depending on the Windows version.
http://msdn.microsoft.com/en-us/library/ms724872(VS.85).aspx
I'll just say this first: this sounds like a really bad idea.
If you insist on doing this, you're going to have to serialize it first. The registry doesn't support inserting .NET objects.
You would have to serialize it. The registry only stores primitive values.
You would need to manually serialize it.
Not quite sure why you'd want to store .NET objects in the registry, as there's already existing functionality in the BCL that allows you to do this with XML configuration files... but saying that, it is of course possible to store .NET objects in the registry. You'd probably only want to do it if the size of the object was relatively small, but it shouldn't be a problem anyway. I guess that the obvious way to do it would be to use XML Serialization (without formatting/whitespace) and store the object as a serialized string value.

Categories