I started reading about .NET a few months ago. As a result, I see that there are a lot of different ways to do anything you want - and this is amazing. Idea for this program - try different ways to manage data - from simple txt files, XML files to database different type engines. This program has been created as
HomeLibrary application - store data about your book - just like a main flow of this app.
In the background, I would like to say few words about code for this program. So, the interesting point that was used described in list below:
- Configuration - *.config files (App.clonfig) - storing keys, parameters, connection string for database, etc.
- Input/Output - allow to use files - I use it for storing and opening library in txt and XML formats
- DLL library
- db usage (connected, disconnected methods, EF)
The design of the program is very simple - just few
dataGridViews for representing library and fields with buttons for controlling it. I try to prepare same GUI for all engines (txt, XML, db usage). You can see the program in the picture below:
Using the Code
For this project, I'll try to use such points as Interface and DLL library.
The logic of the program is described in few pictures below. At first, you can see DLL classes for DB usage. I try to separate code for usage Db in DLL for future usage. In this application, I use connected and disconnected methods of bd usage, and also try to use EntityFramework.
In WinForms, I try to isolate each engine type in a separate class, and use it in mainForm.cs.
Also, for this application, a simple db was created. One table
Book and another
As described above, there are a few classes for realizing different "engines" types, such as:
- DLL with 3 classes - Connected Layer, Disconnected Layer and EntityFrameworkLayer.
Points of Interest
Let's look a little bit for each 'engine' class.
Before starting to describe classes, I would like to say few words about Entities for program -
LibraryStock. In this class, I create simple properties, that help me a lot in realizing this idea in app. In the attached file, you can find it as Book.cs and LibrayStock.cs.
The first one is
TxtEngine - most simple variant for storing data - use txt.Files. In this class, all is very simple, but some things were interesing for me:
using - it can be a very useful thing - allow to free resources after usage. MSDN: As a rule, when you use an
IDisposable object, you should declare and instantiate it in a
using statement. The
using statement calls the
Dispose method on the object in the correct way, and it also causes the object itself to go out of scope as soon as
Dispose is called. Within the
using block, the object is read-only and cannot be modified or reassigned.
In my code usage - save lib to file:
public void SaveLibrary(string p, List<Book> l)
using (FileStream fs = new FileStream(p, FileMode.Create, FileAccess.ReadWrite))
using (StreamWriter sw = new StreamWriter(fs))
foreach (Book book in l)
sw.WriteLine(book.BookAuthor + "," +
book.BookName + "," +
book.BookYear + "," +
book.BookGenre + "," +
book.BookCategory + "," +
Also result of saved data in TXT - something like in the picture below:
Second engine -
XMLEngine. For work with XML, I use
System.Xml.Linq. This is not one variant for realizing this class, but more logical and simplest that previous one.
During creating this class, I try to use some
LINQ. As an example in code below, you can find method for reading library from file:
public List<Book> OpenLibrary(string f)
XDocument myDoc = XDocument.Load(f);
List<Book> books = myDoc.Root.Elements("Book")
.Select(x => new Book
BookAuthor = (string)x.Element("Author"),
BookName = (string)x.Element("Name"),
BookYear = (int)x.Element("Year"),
BookGenre = (string)x.Element("Genre"),
BookID = Guid.Parse(x.Element("BookID").Value.Trim().ToString())
Regarding this XML elements - for better understanding usage of it is required just create 2 or 3 methods with it - and all becomes very clear and logical.
And the result of work with this class:
These 2 classes implement interface
IFileUsage - due to using file for storing and control library (with txt engine or XML engine).
internal interface IFileUsage
void SaveLibrary(string p, List<Book> l);
List<Book> OpenLibrary(string f);
Also as I wrote before, I create class with
CommonEngine - include methods for adding, removing and updating entries from library.
Next point in program - usage of DB. For this purpose, I create DT (in VS) like:
As you can see, it's very simple and contains only 2 tables, without any procedures or another things.
As mentioned before, I try to create DLL for separating code for db usage in file. LibraryLib.dll - This DLL includes 3 different type of DB method usage - connected (
ConnectedLayer), disconnected (
EntityFramework(entityFrameWorkLayer). For choosing type of engine, I add radio buttons on form at
DBEngine tab at GUI.
Regarding these 3 classes, I can say only that it was interesting to understand how to do this, but a lot of things still need to read and try.
While creating this library, I try such things as LINQ to SQL, App.config usage, find a lot of alternative methods to do required thing (in code it's commented but not deleted - some of them work, some no - but later it will be modified and fully investigated from my side), understand methods and logic of
System.Data usage, try to use different SqlQuery, found for myself many interesting methods (example -
Distinct() - at first try to realize it and spend more than one day for this purpose) and a lot of another thing (I can write here till tomorrow =)).
Hope this short description can help someone. Also, I would like to say thanks to all people (from different places) for help.
Result of work:
Currently this is version 0.0.0.4
- realized basic variant of each type of Engine
What to do next
- Realize Threading for DB loading - currently it's a little bit "thinking" before showing result
- Move project to WPF
- Modify logic for DBEngine
- Implement skins