问题描述
Hello Guys,
最近我遇到了一个单一责任和开放式关闭原则在我的c#代码中被违反的情况。以下是场景:
我有两节课:
1.出口调查问卷 - 出口问卷功能
2.出口初始出价 - 出口初始出价功能
1.出口第一
Hello Guys,
Recently I came across a scenario where Single responsibility and Open close principle get violated in my c# code. below is the scenario :
I am having two classes :
1. Export Questionnaire - export questionnaire functionality
2. Export Initial Bid - export initial bid functionality
1. Export First
internal class ExportQuestionnaire
{
// startrow, startcolumn and end column is position where exporting will start on a given template.
public int startRowPosition { get; set; }
public int startColumnPosition { get; set; }
public int endColumnPosition { get; set; }
public List<int> unSupportedQuestion { get; set; }
// Constructor will initialize hard coded values for the class
public ExportQuestionnaire(int _startRowPosition, int _startColumnPosition, int _endColumnPosition)
{
startRowPosition = _startRowPosition;
startColumnPosition = _startColumnPosition;
endColumnPosition = _endColumnPosition;
unSupportedQuestion = new List<int>() { 3, 4, 5, 10, 11, 15, 16 };
}
// ProcessRow - function will process row by row exporting for a worksheet - aspose
public DataRow ProcessRow(DataRow drif, Question question, int countQuestion, long eventCode)
{
}
// GenerateSecureCode_Q - will generate secure code for exported sheet. first column of each sheet will have some secure code which will be hidden from end user.
public string GenerateSecureCode_Q(Question question)
{
// will call GenerateSecureCode internally.
}
public string GenerateSecureCode(List<FirstRowChoice> lstQuestionchoice)
{
}
public int GetFirstIdFromSheetColumn(string questionString)
{
}
// ProcessDocument - will process each worksheet in a workbook [Aspose]
public void ProcessDocument(Workbook templateWorkbook, ExcelHelper excelHelper, DataTable dt, SecureString _ExcelProtection)
{
}
public void ApplyStyle(Worksheet worksheet, int count)
{
}
}
2.出口第二
2. Export Second
internal class ExportInitialBid
{
public int startRowPosition { get; set; }
public int startColumnPosition { get; set; }
public int endColumnPosition { get; set; }
public ExportInitialBid(int _startRowPosition, int _startColumnPosition, int _endColumnPosition)
{
startRowPosition = _startRowPosition;
startColumnPosition = _startColumnPosition;
endColumnPosition = _endColumnPosition;
}
public DataRow ProcessRow(DataRow drif, Second lib, List<Second> SecondList)
{
}
public void ProcessDocument(Workbook templateWorkbook, ExcelHelper excelHelper, DataTable dt, int Resolution, SecureString _ExcelProtection)
{
}
private void ApplyStyle(Worksheet worksheet, int rowCount, int columnCount)
{
}
private void ApplyDecimalStyle(Worksheet worksheet, int Count, int columnCount, int Resolution)
{
}
}
现在,我正在尝试干它并为ApplyStyle,ApplyDecimalStyle,ProcessRow,ProcessDocument创建单独的类,为每个类保留一个基本抽象类。
将来如果我需要再一个ExportSomething,我将创建一对新的ApplyStyle,ApplyDecimalStyle,ProcessRow和ProcessDocument,扩展了过去的抽象类。
但是这样巨大的没有。类的创建。虽然以前只有两个班级。
这是正确的方法来分离c#中不同类别中的每一个和所有内容吗?
你可以忽略回答,如果这个长问题很烦人的话。但如果你这样做,我会学到新东西。
谢谢
[更新1]:重命名类和提供的评论。
Now, I am trying to DRY it and creating separate classes for ApplyStyle, ApplyDecimalStyle, ProcessRow, ProcessDocument keeping one base abstract class for each of these.
in future if i need to require one more ExportSomething, I will create one new pair of ApplyStyle, ApplyDecimalStyle, ProcessRowand ProcessDocument , extending abstract class of past.
But in this way huge no. of classes get created. while previously only two classes i had.
Is this correct approach to separate each and everything in different classes in c# ?
you can ignore answering, if this long question is annoying. but if you do, i will learn something new.
Thanks
[UPDATE 1] : Rename classes and provided comments.
推荐答案
// Note : no need to modify existing code, only available for extension.
// right now implemented for questionnaire/ intial bid export.
// Interface for exported sheet start Row and column hard values
interface IRowColumnStart
{
int startRowPos { get; set; }
int startColPos { get; set; }
int endColPos { get; set; }
}
interface unSupportedQ
{
List<int> unSupportedQ { get; set; }
}
// IRowColumnStart extended class for initial bid export
public class startRowsColumn : IRowColumnStart
{
public int startRowPos { get; set; }
public int startColPos { get; set; }
public int endColPos { get; set; }
public startRowsColumn(int _startRowPos, int _startColPos, int _endColPos)
{
startRowPos = _startRowPos;
startColPos = _startColPos;
endColPos = _endColPos;
}
}
// IRowColumnStart extended class for questionnaire export
public class startRowColumnQ : IRowColumnStart, unSupportedQ
{
public int startRowPos { get; set; }
public int startColPos { get; set; }
public int endColPos { get; set; }
public List<int> unSupportedQ { get; set; }
public startRowColumnQ(int _startRowPos, int _startColPos, int _endColPos, List<int> _unSupportedQ)
{
startRowPos = _startRowPos;
startColPos = _startColPos;
endColPos = _endColPos;
unSupportedQ = _unSupportedQ;
}
}
// Note : no need to modify existing code, only available for extension.
// Main Interface for processing export request
interface IProcessExport<t,>
{
DataRow ProRow(T param);
void ProWsheet(T1 param);
}
// IProcessExport<t,> extended class for questionnaire export
internal class ProcessExportQ : IProcessExport<rowqparam,>
{
private IProcessRow<rowqparam> IProcessRow;
private IProcessWorksheet<worksheetqparam,> IProcessDoc;
public ProcessExportQ(IProcessRow<rowqparam> i, IProcessWorksheet<worksheetqparam,> j)
{
IProcessRow = i;
IProcessDoc = j;
}
public DataRow ProRow(RowQParam qParam)
{
return IProcessRow.ProRow(qParam);
}
public void ProWsheet(WorksheetQParam qParam)
{
IProcessDoc.ProcessWorksheet(qParam);
}
}
// IProcessExport<t,> extended class for initial bid export
internal class ProcessExportIBid : IProcessExport<rowibidparam,>
{
private startRowsColumn objRow;
private IProcessRow<rowibidparam> IProcessRow;
private IProcessWorksheet<worksheetibidparam,> IProcessDoc;
public ProcessExportIBid(IProcessRow<rowibidparam> i, IProcessWorksheet<worksheetibidparam,> j)
{
IProcessRow = i;
IProcessDoc = j;
}
public DataRow ProRow(RowIBidParam iBParam)
{
return IProcessRow.ProRow(iBParam);
}
public void ProWsheet(WorksheetIBidParam iBParam)
{
IProcessDoc.ProcessWorksheet(iBParam);
}
}
这篇关于在c#中应用单一责任和开放式原则是否正确?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!