Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
Hi All,

I am a newbie in Design Patterns.

Let’s assume that we have an N number of flat files inside the directory. Let the flat files having comma separated information and consider the first line will be column information that is improperly ordered and next lines will be row information. What we want to do means we have to process the flat files and put that file info into database table.

For Example:
Database Table: Student
Columns:
ID-int
Name- varchar
Address – varchar

Flat file1 : flatfile1.txt
ID , Name,,Address
1,Vijai,Salem
2,Anand,,Attur

Flat file2:flatfile2.csv
ID,Name,Age
1,Vijai,,20
2,anand,20

Flatfile3:flatfile3.csv
1,Vijai,20
2,anand

Can anyone help me  to design for this problem using any design pattern c# .Ultimately what I have to do is to validate the file information and put that in corresponding database as I mentioned above.

Thanks in Advance.


What I have tried:

public static class Common
   {
       public const char CONTENT_SPLIT = ',';
   }

interface IFile
    {
        IEnumerable<string> ReadFile(string filePath);
        DataTable FillFileContentintoDataTable(IEnumerable<string> fileContent);
        void WriteFile(DataTable dataTable,string tableName);
    }

public class TextFile : IFile
    {
        public IEnumerable<string> ReadFile(string filePath)
        {
            IEnumerable<string> fileContents=null;

            if (File.Exists(filePath))
            {
                fileContents = File.ReadLines(filePath);
            }
            return fileContents;
        }

        public DataTable FillFileContentintoDataTable(IEnumerable<string> fileContent)
        {
            int rowCount = 0;
            int colCount=0;

            StudentDataset objStudentDs = new StudentDataset();
            DataTable dt = objStudentDs.Tables[0];
            if (fileContent != null)
            {
                foreach (string content in fileContent)
                {
                    if (rowCount != 0)
                    {
                        colCount = 0;
                        string[] rowContent=content.Split(Common.CONTENT_SPLIT);
                        int noOfSplitDelimeters = rowContent.Length;
                        DataRow dr = dt.NewRow();
                        foreach (DataColumn column in dt.Columns)
                        {
                            if (colCount != noOfSplitDelimeters)
                            {
                                dr[column.ColumnName] = Convert.ToString(rowContent[colCount]);
                            }
                            else
                            {
                                break;
                            }
                            colCount += 1;
                        }
                        dt.Rows.Add(dr);
                    }
                    rowCount += 1;
                }
            }
            return dt;
        }

        public void WriteFile(DataTable dataTable,string tableName)
        {
            DatabaseHandler objDBHandler = new DatabaseHandler(dataTable);
            objDBHandler.WritetoDB(tableName);
        }

    }

class FileFactory
   {

       public static IFile CreateFileObject(string fileExtension)
       {
           IFile fileObject=null;
           switch (fileExtension)
           {
               case  ".txt":
                   fileObject= new TextFile();
                   break;

               default:
                   fileObject = null;
                   break;

           }

           return fileObject;
       }

   }

public class CheckValidFileContent
   {
       private IEnumerable<string> FileContent { get; set; }

       public CheckValidFileContent(IEnumerable<string> fileContent)
       {
           FileContent = fileContent;
       }

       public bool IsValidFileContent()
       {
           bool isValid = false;
           int rowCount = 0,colCount=0;

           StudentDataset objStudentDataSet = new StudentDataset();
           DataTable objStudentDataTable = objStudentDataSet.Tables[0];
           if (FileContent != null)
           {
               foreach (string content in FileContent)
               {
                   if (rowCount == 0)
                   {
                       string[] columnContent=content.Split(Common.CONTENT_SPLIT);
                       foreach (DataColumn column in objStudentDataTable.Columns)
                       {
                           string contentName = Convert.ToString(columnContent[colCount]);
                           if (contentName != "")
                           {
                               if (Convert.ToString(columnContent[colCount]).ToUpper() == column.ColumnName.ToUpper())
                               {
                                   isValid = true;
                               }
                               else
                               {
                                   isValid = false;
                                   break;
                               }
                           }
                           else
                           {
                               isValid = false;
                               break;
                           }

                           colCount += 1;
                       }
                   }
                   else
                   {
                       break;
                   }
                   rowCount += 1;
               }
           }
           else
           {
               isValid = false;
           }

           return isValid;

       }

   }

public class DatabaseHandler
   {
       private string cnStr = ConfigurationManager.ConnectionStrings["cnStr"].ConnectionString.ToString();

       private DataTable DBdataTable { get; set; }

       public DatabaseHandler( DataTable dataTable)
       {
           DBdataTable = dataTable;
       }

       public void WritetoDB(string tableName)
       {
           int colCount=0;
           if (DBdataTable != null)
           {
               using (SqlConnection cn = new SqlConnection(cnStr))
               {
                   cn.Open();
                   foreach (DataRow dr in DBdataTable.Rows)
                   {
                       colCount = 0;
                       string query = "insert into " + tableName + " ( ";
                       string colValues = null;

                       foreach (DataColumn dc in DBdataTable.Columns)
                       {
                           if (colCount == 0)
                           {
                               query = query + dc.ColumnName;
                           }
                           else
                           {
                               query = query + "," + dc.ColumnName;
                           }

                           if (colValues == null)
                           {
                               if (dc.DataType == typeof(Int32))
                               {
                                   colValues = Convert.ToString(dr[dc.ColumnName]);
                               }
                               else
                               {
                                   colValues = "'" + Convert.ToString(dr[dc.ColumnName]) + "'";
                               }
                           }
                           else
                           {
                               if (dc.DataType == typeof(Int32))
                               {
                                   colValues = colValues + "," + Convert.ToString(dr[dc.ColumnName]);
                               }
                               else
                               {
                                   colValues = colValues + "," + "'" + Convert.ToString(dr[dc.ColumnName]) + "'";
                               }

                           }
                           if ((DBdataTable.Columns.Count - 1) == colCount)
                           {
                               query = query + " ) values ( " + colValues + ")";

                               SqlCommand cmd = new SqlCommand();
                               cmd.Connection = cn;
                               cmd.CommandText = query;
                               cmd.ExecuteNonQuery();
                           }

                           colCount += 1;
                       }
                   }
                   cn.Close();
               }
           }
       }
   }

class Program
   {
       static void Main(string[] args)
       {
           string flatFilesDirectoryPath = @"E:\FlatFiles";
           string flatFileExtension = null;

           foreach(var filePath in Directory.GetFiles(flatFilesDirectoryPath))
           {
               flatFileExtension = Path.GetExtension(filePath);
               IFile objFile = FileFactory.CreateFileObject(flatFileExtension);
               IEnumerable<string> fileContents = objFile.ReadFile(filePath);
               CheckValidFileContent objCheckValidFile = new CheckValidFileContent(fileContents);
               bool isValid = objCheckValidFile.IsValidFileContent();
               if (isValid == true)
               {
                   DataTable dbTable = objFile.FillFileContentintoDataTable(fileContents);
                   objFile.WriteFile(dbTable, "student");
               }
           }
       }
   }

anyone tell some good approach to solve this problem..
Posted
Updated 26-Feb-17 1:19am
v3

Job Interview? Study interview? If it is a job interview and someone else does it for you, they'll quickly cotton on.

Time to get a book or start researching online. There are many different design pattern out there, each having a specific purpose. Here is a good starting point: c# design patterns - Google[^]
 
Share this answer
 
v2
Comments
Vijai Anand.G 26-Feb-17 0:39am    
Tq..I already started research..let us look into some specific design pattern like Factory Method Creational Design Pattern.how we can use this pattern effectively to solve this problem.please help..
Graeme_Grant 26-Feb-17 0:48am    
You are going for a job interview. You need to write the code as you will need to explain to the interviewer(s):
1. Design pattern used & meaning
2. Why you selected the patterns and not others (they may ask why you did not select xyz)
3. How you implemented the pattern.

If we do it for you, you won't be able to answer these question.
Vijai Anand.G 26-Feb-17 0:55am    
forgot about the interview.I just need the best approach for this problem..tq..
Graeme_Grant 26-Feb-17 1:05am    
We are her not to design or write code for you. Quick Answers is here for when you get stuck with your code.
Quote:
In one of my final round Interview, Interviewer ask me to design using design pattern for this problem.

This is to test your knowledge, not your ability to beg others to do your work !
If we answer for you, are we getting the job ?

May be it is time to sharpen your skills and study patterns. Google is your friend.
 
Share this answer
 
Comments
Vijai Anand.G 26-Feb-17 1:07am    
For your kind attention i am not begging my friend.i m asking for some good approach to solve the problem.any how tq for your suggestion..
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900