Need some advice for project - c#

I'm going to do a project task as the last part of a programming course in C#, and I have choosen to do a media library for storing information of DVD movie collections. The thing I need some advice is about how to save this information since the task should be solved by using simple text files and not databases which had been better.
So, which is the best way to save this information to be able to add, edit and allow search for titles and actors and also sort after genre to mention some of the idéas I have for this program.
Should I just save the information by just add the title, year, playtime, director, actor1, actor2, actor3, genre, grade, comment for each movie lika a row at the end of the file? Can I search in the file or should I, in some way, first read it all into an array, and then do the serach and perhaps edit and save the complete file again?
Or is it better to use XML like this:
<movie_collections>
<movie>
<title=Tron Legacy"></title>
<year=2010></year>
<playtime=120></playtime>
etc.
</movie>
</movie_collections>
If I use XML, can I search for a title and just load or edit that part? Are there better alternatives than these?

You may store the data in XML file. An XML file can store data similar to a database table. You can add multiple records, hierarchical data etc... You may easily query the data using LINQ to XML.
If you dont want to use LINQ to XML, You can use so typical XMLDocument to handle the XML data.

Maybe you're approaching this at too low a level. The file is realistically just for persistance rather than implement your own DB as a file.
Just implement the object that you're after and serialize it out imo.
public class MovieCollections
{
private ICollection<Movie> Movies {get; set;}
// etc...
}
public class Movie
{
public string Title {get; private set;}
public int PlayTime {get; private set;}
public DateTime ReleaseDate {get; private set;}
}
Serialize instance of MovieCollections.
Keep MovieCollections perhaps as a Singleton instance since you only want one collection.

By the way this seems to be very general question & a quite common homework question too!!
I think you should google for this, you will get better projects ideas.
Something similar on this lines :
1.Simple Movie Database in C# using Microsoft Access
2. Create a Movie Database Application
3. imdbapi
& finally
4. SO's similar post
And as far as comparison between a database & XML is concern, I did recommend you a database because several pros over XML as far such type of project is considered.

As #M_Affifi suggests, think of all your interaction with your data (e.g. sort, add, find, delete, update, etc) through your objects which reside in memory (e.g. Instance of MovieCollections class and instances of Movie class), the XML file is only used to store the state of your objects, so you have that data available next time you run your application.
Once you're done manipulating your data just serialize it to XML. Serialization is just the process to convert your objects into a format that you can store or transfer, in this case will be conversion to XML. Take a look at this reference, Your serialization needs are very basic, so you'll need just few lines of code.
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(YourObject.GetType());
x.Serialize(Console.Out, YourObject)
Finally, I would suggest thinking of your application logic. When program starts you may want to check if your Movies.xml exists, if so, then 'Deserialize' it, which means loading it from XML to your object collection. If XML file doesn't exist then just start manipulating your objects and when you're done give the option to Save(Serialize). I can help with the serialization/deserialization process if needed.
Hope this is helpful
;

Related

UWP/C# Advice with storing application user data?

I need a bit of advice/opinions on storing data.
Im looking at essentially storing user input data and then reference it on a frequent basis. I would like to avoid data base storage as this is all going to be referenced locally. Ive seen that you can do XML serialised objects etc and wanted to know what you think about data storage.
If i give you an idea of what im looking at doing in a step by step it may give a better idea of what im trying to achieve:
User loads the program
Program reads the storage file
Different controls such as GridView and text boxes displays specific parts of the document such as Names, Dates etc.
User can add to the file by entering data into different fields across the whole sheet.
User is free to save the file when required, can create new files when required and can open sheets at any point
All information needs to scale. i.e. user can add up to 50 dates, 30 names etc and each of these cannot interfere with another data field. All of this data for each specific type will be able to be referenced by the ui controls
Its a bit of a big task and i was looking at doing it all using XML. Im not sure if this is the best way to do it and wanted your opinions. Mainly due to the way the data can dynamically adjust all the time through user input.
Any input on this would be appreciated. I can provide images of what im trying to achieve if necessary.
I personally use Newtonsoft JSON for saving my user data. You can build all your classes as normal and then fairly easily integrate the JSON serialiser/deserialiser without too much hassle. There will be some exceptions that may require a custom serialiser, but it's not too difficult to make those, especially if you only need to interpret one small part and then can just pass the rest of the file off to the default serialiser.
This is a very quick example how you could store some key-value preferences for a number of users in a file.
using Newtonsoft.Json;
[JsonObject]
public class UserData {
[JsonProperty] //Add a JSON property "Username" which is a string
public string Username { get; set; }
//IEnumerable types are converted to/from arrays automatically by Newtonsoft
[JsonProperty("Options")] //Set the name in JSON file to "Options"
public Dictionary<string, string> Preferences { get; set; }
[JsonIgnore] //Excludes this property from the JSON output
public bool SaveRequired { get; set; } //Set true when a change is made, set false when saved
}
There are very similar libraries like this that do the exact same thing for XML, but I've not had much luck figuring them out and I'm usually on a very tight time scale when I just need something that I know works.
If you can find a decent XML library and understand how to use it correctly, I'd recommend XML over JSON due to its strictly structured nature, and you can include schema versions and aid with integration into other systems by providing a well written schema.

Is it possible to deserialize only a portion of a serialized file

First of all, I am a beginner in this area of serialization in C#. So please bear with me if some things I mention sound weird.
I have a serialized file which consists of several List arrays. For example:-
List of Teachers;
List of Students;
Both Teachers and Students were derived from the ISerializable Interface and hold serializable attributes in it.
Now I have the serialized file, lets call it classes.data file.
Is there anyway I can deserialize only one portion of this serialized file?
For example I just want to read the List of Teachers. Do I have to read the complete serialized file classes.data and then retrieve the list of teachers?
Or is there anyway I can just deserialize the list of teachers as a List object hence making the deserializing faster?
Please help as this is one of my projects.
Thanks a lot in advance.
Update : I am working on c#
Samuel
A short answer; No. It may although depend on which language you are using, but if my guess (Java) is correct th answer is no. It sounds like you'd rather turn to for example JSON to store your data, or a database, or both.
Serialization is not meant to be used as a way to store large amounts of data (like lists), you should use a database or if it's simply a list or two; JSON.
Info about JSON: http://en.wikipedia.org/wiki/JSON
Library for working with JSON in Java: https://code.google.com/p/json-simple/

Adding side notes to each property in MongoDB document

I have a collection with a collection of documents. Each document has around 20 different properties with different data types (e.g. Int, Double, String).
I am searching for an efficient way or the appropriate way to add side notes to each property.
My thought (I am using C# to model the document structure) is for each property, instead of
:
public int PageRank {get; set; }
to use:
public Dictionary<int, string> PageRank {get; set;}
This means that each item in the document is a collection of both the value and the string for the side note.
The side notes will be seen at the front-end by the user.
Any better implementation?
Idan, for performance reasons, you should consider your use case from the MongoDB point of view -- not from the object oriented language point of view. The way it ends up looking in C# is an afterthought -- its the DB performance that counts. So, when querying your documents, if the side notes are mostly not needed, it will be better to place them into a separate collection (possibly) thus reducing the size of each document and enabling MongoDB to read more of them into the available memory. If the user does need to look at the side notes, you would do this with a separate query. You know your usage scenario better, so its up to you to decide how to do this, but its these kinds of design decisions that you need to concern yourself with -- and the C# code will be shaped according to your schema

C# Sort object with listcollection member

I have an MVC web app where users upload a text file and I parse it out.
The requirement just changed and they will be uploading multiple files of the same kind now. I parse a single file by sending a file-path to the method below, ReadParts which opens a stream and calls the method parseReplicateBlock to retrieve desired fields. For multiple files I could read all the files into one big stream but I am afraid it could exceed the buffer limit etc.
So I am thinking to parse file by file and populate results into an object. My requirement then, is to sort the records based on a date field.
I just need some help in how to write this method ReadLogFile in a better way, espceially for sorting based on initialtionDate and initiationTime. I want to find the minimum record based on initiationDate and Time and then do some other logic.
The problem is if I sort the list member within the object, I would loose positiong of the other records.
You appear to be storing each field of the record in a separate collection within LogFile. This seems a very strange way to store your data.
If you sort one of these collections, then of course it will bear no relationship to the other fields any longer since they are unrelated. There are huge areas for bugs too if you are relying on all the collections tallying up (eg if a field is missing from one of the parsed records)
Instead you should be have a class that represents a SINGLE record, and then Logfile has a SINGLE collection of these records. eg:
public class ReplicateBlock
{
public string ReplicateId { get; set; }
public string AssayNumber { get; set; }
public DateTime InitiationDate { get; set; }
//etc
}
public class LogFile
{
public List<ReplicateBlock> ReplicateBlocks = new List<ReplicateBlock>();
}
I have to say that your code is very difficult to follow. The fact that all your functions are static makes me think that you're not particularly familiar with object oriented programming. I would suggest getting a good book on the subject.

c#: Using Assemblies (via Reflection) as a (meta)data store

SOME CONTEXT
one of my projects requires carrying around some of "metadata" (yes I hate using that word).
What the metadata specifically consists of is not important, only that it's more complex than a simple "table" or "list" - you could think of it as a mini-database of information
Currently I have this metadata stored in an XML file and have an XSD that defines the schema.
I want to package this metadata with my project, currently that means keeping the XML file as a resource
However, I have been looking for a more strongly-typed alternative. I am considering moving it from an XML file to C# code - so instead of using XML apis to traverse my metadata, relying on .NET code via reflection on types
Besides the strong(er) typing, some useful characteristics I see from using an assembly are for this: (1) I can refactor the "schema" to some extent with tools like Resharper, (2) The metadata can come with code, (3) don't have to rely on any external DB component.
THE QUESTIONS
If you have tried something like this, I am curious about what you learned.
Was your experience positive?
What did you learn?
What problems in this approach did you uncover?
What are some considerations I should take into account?
Would you do this again?
NOTES
Am not asking for how to use Reflection - no help is needed there
Am fundamentally asking about your experiences and design considerations
UPDATE: INFORMATION ABOUT THE METADATA
Because people are asking I'll try describing the metadata a bit more. I'm trying to abstract a bit - so this will seem a bit artificial.
There are three entities in the model:
A set of "groups" - each group has a unique name and several properites (usually int values that represent ID numbers of some kind)
Each "group" contains 1 or more "widgets" (never more than 50) - each item has properties like name (therea are multiple names), IDs, and various boolean properties.
Each widget contains a one or more "scenarios". Each "scenario" is documentation- a URL to a description of how to use the widget.
Typically I need to run these kinds of "queries"
Get the names of all the widgets
Get the names of all groups that contain at least one widget where BoolProp1=true
Get given the ID of a widget, which group contains that widget
How I was thinking about modelling the entities in the assembly
There are 3 classes: Group, Widget, Documentation
There are 25 Groups so I will have 25 Group classes - so "FooGroup" will derive from Group, same pattern follows for widgets and documentation
Each class will have attributes to account for names, ids, etc.
I have used and extended Metadata for a large part of my projects, many of them related to describing components, relationships among them, mappings, etc.
(Major categories of using attributes extensively include O/R Mappers, Dependency Injection framework, and Serialization description - specially XML Serialization)
Well, I'm going to ask you to describe a little bit more about the nature of the data you want to embed as resource. Using attributes are naturally good for the type of data that describes your types and type elements, but each usage of attributes is a simple and short one. Attributes (I think) should be very cohesive and somehow independent from each other.
One of the solutions that I want to point you at, is the "XML Serialization" approach. You can keep your current XMLs, and put them into your assemblies as Embedded Resource (which is what you've probably done already) and read the whole XML at once into a strongly-typed hierarchy of objects.
XML Serialization is very very simple to use, much simpler than the typical XML API or even LINQ2XML, in my opinion. It uses Attributes to map class properties to XML elements and XML attributes. Once you've loaded the XML into the objects, you have everything you want in the memory as "typed" data.
Based on what I understand from your description, I think you have a lot of data to be placed on a single class. This means a large and (in my opinion) ugly attribute code above the class. (Unless you can distribute your data among members making each of them small and independent, which is nice.)
I have many positive experiences using XML Serialization for large amount of data. You can arrange data as you want, you get type safety, you get IntelliSence (if you give your XSD to visual studio), and you also get half of the Refactoring. ReSharper (or any other refactoring tool that I know of) don't recognize XML Serialization, so when you refactor your typed classes, it doesn't change the XML itself, but changes all the usage of the data.
If you give me more details on what your data is, I might be able to add something to my answer.
For XML Serialization samples, just Google "XML Serialization" or look it up in MSDN.
UPDATE
I strongly recommend NOT using classes for representing instances of your data. Or even using a class to encapsulate data is against its logical definition.
I guess your best bet would be XML Serialization, provided that you already have your data in XML. You get all the benefits you want, with less code. And you can perform any query on the XML Serializable objects using LINQ2Objects.
A part of your code can look like the following:
[XmlRoot]
public class MyMetadata
{
[XmlElement]
public Group[] Groups { get; set; }
}
public class Group
{
[XmlAttribute]
public string Name { get; set; }
[XmlAttribute]
public int SomeNumber { get; set; }
[XmlElement]
public Widget[] Widgets { get; set; }
}
public class Widget
{
...
}
You should call new XmlSerializer(typeof(MyMetadata)) to create a serializer, and call its Deserialize method giving it the stream of your XML, and you get a filled instance of MyMetadata class.
It's not clear from your description but it sounds like you have assembly-level metadata that you want to be able to access (as opposed to type-level). You could have a single class in each assembly that implements a common interface, then use reflection to hunt down that class and instantiate it. Then you can hard-code the metadata within.
The problems of course are the benefits that you lose from the XML -- namely that you can't modify the metadata without a new build. But if you're going this direction you probably have already taken that into account.

Categories