Click here to Skip to main content
15,887,267 members
Articles / Desktop Programming / Windows Forms

MyHomeLibrary or How I Try Different Method of Data Managing in C# .NET

Rate me:
Please Sign up or sign in to vote.
4.40/5 (4 votes)
18 Jan 2014CPOL5 min read 17.5K   751   10   4
How to manage data using different tools from .NET

Introduction

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.

Background

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 
  • interfaces 
  • DLL library
  • db usage (connected, disconnected methods, EF)  
  • LINQ  

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:

Image 1

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.

Image 2

In WinForms, I try to isolate each engine type in a separate class, and use it in mainForm.cs.

Image 3

Also, for this application, a simple db was created. One table Book and another LibraryStock:

Image 4

As described above, there are a few classes for realizing different "engines" types, such as:

  • Txtengine;
  • XmlEngine;
  • CommonEngine;
  • 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 - Book and 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:

Keywords 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:

C#
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 + "," +
                           book.BookID);
                   }
                   sw.Close();
               }
               fs.Close();
           }
       }

Also result of saved data in TXT - something like in the picture below:

Image 5

Second engine - XMLEngine. For work with XML, I use XMLDocument and XMLElement from 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:

C#
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())
                        })
                        .ToList();
                return books;
         }  

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:

Image 6

These 2 classes implement interface IFileUsage - due to using file for storing and control library (with txt engine or XML engine).

IFileUsage:

C#
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:

Image 7

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 (DisconnectedLayer) and 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:

Image 8

History

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer
Ukraine Ukraine
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionnice Pin
BillW339-Apr-15 6:00
professionalBillW339-Apr-15 6:00 
Nicely written with some useful ideas.
Just because the code works, it doesn't mean that it is good code.

QuestionSuper! +5 Pin
RAND 4558664-Jan-15 3:08
RAND 4558664-Jan-15 3:08 
AnswerRe: Super! +5 Pin
Kirill__10-Jan-15 6:26
Kirill__10-Jan-15 6:26 
GeneralMy vote of 4 Pin
Dino.Liu19-Jan-14 21:58
Dino.Liu19-Jan-14 21:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.