Questions about saving objects and databases
-
Hi! I am renewing a program I made in a university-class in Java. The program is a library system where you can save books as objects, customer as objects and then make a loan. In the old program we saved to disc in a binary-file. How would you do it in C#? I also been thinking about LINQ (which I don´t know very much about), is that and alternative? I final question is if It is a good design or if it would have been better to man a traditional relationship database with no objects.
-
Hi! I am renewing a program I made in a university-class in Java. The program is a library system where you can save books as objects, customer as objects and then make a loan. In the old program we saved to disc in a binary-file. How would you do it in C#? I also been thinking about LINQ (which I don´t know very much about), is that and alternative? I final question is if It is a good design or if it would have been better to man a traditional relationship database with no objects.
larsp777 wrote:
In the old program we saved to disc in a binary-file.
How would you do it in C#?The direct "translation" is .net serialization[^]. You don't need to use the binary formatter described, there is an XML one too (and a SOAP one, but the isn't useful in this context) or you could write your own. Note that it (the binary one) almost certainly won't deserialize the Java files properly. Personally, I wouldn't store anything "complicted" (say with relations or mutliple rows) this way unless I had really good reasons to. You mention LINQ, "LINQ to SQL" and "Entity Framework" are good ways of persisting data and are similar to eachother. You should Google these. A third (non-Microsofty) option is NHibernate, it has similarities to the Entity Framework, but comes with the added advantage, from your point of view, that there is a Java Equivalent ("Hibernate") so by learning one, you learn the Java version (mod any documented differences).
larsp777 wrote:
I final question is if It is a good design or if it would have been better to man a traditional relationship database with no objects.
If I understand you correctly, I'd always go for proper relational design, but that doesn't mean you lose good OO at all. The Entity Framework pretty much expects proper relational design (if you design the DB first, it creates well normalised Schema from an OO model if you start with the model first). NHibernate works better under a properly normalised DB too, but is more forgiving of mismatches between the OO and the DB. That said the same things that drive good OO desgin tend to drive DB schemas: A type is pretty much a table, a property to a simple type pretty much a field and a property to a custom class a relationship to another table. Many-to-One maps to a list property at one end and a single property at the other, Many-to-many has maps to lists at both ends. My advice is to try Entity Framework, starting with the model. Some people have reported performance problems on large data sets, but you can pre-compile LINQ queries and even go the whole hog and use Stored Procs so the perforance is comparable with a little work and you gain more than you lose IMO.
-
larsp777 wrote:
In the old program we saved to disc in a binary-file.
How would you do it in C#?The direct "translation" is .net serialization[^]. You don't need to use the binary formatter described, there is an XML one too (and a SOAP one, but the isn't useful in this context) or you could write your own. Note that it (the binary one) almost certainly won't deserialize the Java files properly. Personally, I wouldn't store anything "complicted" (say with relations or mutliple rows) this way unless I had really good reasons to. You mention LINQ, "LINQ to SQL" and "Entity Framework" are good ways of persisting data and are similar to eachother. You should Google these. A third (non-Microsofty) option is NHibernate, it has similarities to the Entity Framework, but comes with the added advantage, from your point of view, that there is a Java Equivalent ("Hibernate") so by learning one, you learn the Java version (mod any documented differences).
larsp777 wrote:
I final question is if It is a good design or if it would have been better to man a traditional relationship database with no objects.
If I understand you correctly, I'd always go for proper relational design, but that doesn't mean you lose good OO at all. The Entity Framework pretty much expects proper relational design (if you design the DB first, it creates well normalised Schema from an OO model if you start with the model first). NHibernate works better under a properly normalised DB too, but is more forgiving of mismatches between the OO and the DB. That said the same things that drive good OO desgin tend to drive DB schemas: A type is pretty much a table, a property to a simple type pretty much a field and a property to a custom class a relationship to another table. Many-to-One maps to a list property at one end and a single property at the other, Many-to-many has maps to lists at both ends. My advice is to try Entity Framework, starting with the model. Some people have reported performance problems on large data sets, but you can pre-compile LINQ queries and even go the whole hog and use Stored Procs so the perforance is comparable with a little work and you gain more than you lose IMO.
-
Hi! I am renewing a program I made in a university-class in Java. The program is a library system where you can save books as objects, customer as objects and then make a loan. In the old program we saved to disc in a binary-file. How would you do it in C#? I also been thinking about LINQ (which I don´t know very much about), is that and alternative? I final question is if It is a good design or if it would have been better to man a traditional relationship database with no objects.
larsp777 wrote:
I final question is if It is a good design or if it would have been better to man a traditional relationship database with no objects.
Depends what you really mean. If you really want to store a binary image of a book into a database then you have two choices 1. Store it as a blob 2. Store it as a file with a link reference in the database. However if you want to store information about a book, then you should be using a relational database.