如何使用设计模式C#设计这个实时问题 [英] How to design for this real time problem using design pattern C#

查看:96
本文介绍了如何使用设计模式C#设计这个实时问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是Design Patterns的新手。

假设我们在目录中有N个平面文件。让平面文件以逗号分隔信息并考虑第一行将是不正确排序的列信息,下一行将是行信息。我们想要做的是意味着我们必须处理平面文件并将该文件信息放入数据库表中。

例如:
数据库表:学生
列:
ID-int
Name- varchar
地址 - varchar

平面文件1:flatfile1.txt
ID,名称,地址
1,Vijai,Salem
2,Anand ,, Attur

平面文件2: flatfile2.csv
ID,姓名,年龄
1,Vijai ,, 20
2,anand,20

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

任何人都可以帮助我使用任何设计模式来设计这个问题c#。最后我要做的是验证文件信息并将其放入相应的我上面提到的数据库。

先谢谢。





我的尝试:



 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);
}



公共类TextFile:IFile 
{
public IEnumerable< string> ReadFile(string filePath)
{
IEnumerable< string> fileContents = NULL;

if(File.Exists(filePath))
{
fileContents = File.ReadLines(filePath);
}
返回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(fileContent中的字符串内容)
{
if(rowCount!= 0)
{
colCount = 0;
string [] rowContent = content.Split(Common.CONTENT_SPLIT);
int noOfSplitDelimeters = rowContent.Length;
DataRow dr = dt.NewRow();
foreach(dt.Columns中的DataColumn列)
{
if(colCount!= noOfSplitDelimeters)
{
dr [column.ColumnName] = Convert.ToString(rowContent) [colCount]);
}
其他
{
休息;
}
colCount + = 1;
}
dt.Rows.Add(dr);
}
rowCount + = 1;
}
}
返回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();
休息;

默认值:
fileObject = null;
休息;

}

return fileObject;
}

}



公共类CheckValidFileContent 
{
private IEnumerable< string> FileContent {get;组; }

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(FileContent中的字符串内容)
{
if(rowCount == 0)
{
string [] columnContent = content.Split(Common.CONTENT_SPLIT);
foreach(objStudentDataTable.Columns中的DataColumn列)
{
string contentName = Convert.ToString(columnContent [colCount]);
if(contentName!=)
{
if(Convert.ToString(columnContent [colCount])。ToUpper()== column.ColumnName.ToUpper())
{
isValid = true;
}
其他
{
isValid = false;
休息;
}
}
其他
{
isValid = false;
休息;
}

colCount + = 1;
}
}
其他
{
break;
}
rowCount + = 1;
}
}
其他
{
isValid = false;
}

return isValid;

}

}



公共类DatabaseHandler 
{
private string cnStr = ConfigurationManager.ConnectionStrings [cnStr]。ConnectionString.ToString();

private DataTable DBdataTable {get;组; }

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(DBdataTable.Rows中的DataRow dr)
{
colCount = 0;
string query =insert into+ tableName +(;
string colValues = null;

foreach(DBdataTable.Columns中的DataColumn dc)
{
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])+';
}
}
其他
{
if(dc.DataType == typeof(Int32))
{
colValues = colValues + ,+ Convert.ToString(dr [dc.ColumnName]);
}
其他
{
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();
}
}
}
}



课程
{
static void Main(string [] args)
{
string flatFilesDirectoryPath = @E:\ .FlatFiles;
string flatFileExtension = null;

foreach(Directory.GetFiles中的var filePath(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);
}
}
}
}



任何人都会告诉一些好方法来解决这个问题。

解决方案

求职面试?学习面试?如果是面试,而其他人为你做,他们会快速穿上。



获得一本书或开始在线研究的时间。有许多不同的设计模式,每个都有特定的目的。这是一个很好的起点: c#设计模式 - Google [ ^ ]


引用:

在我的最后一轮采访中,采访者要求我为这个问题设计使用设计模式。 / blockquote>

这是为了测试你的知识,而不是你乞求别人做你的工作的能力!

如果我们为你回答,我们是否得到这份工作? br />


可能是时候提高你的技能和学习模式了。 Google是您的朋友。


请参阅设计模式 - Google搜索 [ ^ ]。

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..

解决方案

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[^]


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.


See design patterns - Google Search[^].


这篇关于如何使用设计模式C#设计这个实时问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆