Avoid SRP violation in Console application
up vote
-1
down vote
favorite
I'm developing a simple Console application. Program.cs initialize dependency injection, set file names, accept inputs and print the result.
I got a note that my Program.cs violates the SRP principle ,Program.cs must accept the inputs and print the results, must initialize dependencies. I don't know how to fix that, please advice.
using Autofac;
using Mynamespace.Core;
using Mynamespace.Core.Interfaces;
using Mynamespace.InfraSturcture;
using Mynamespace.Services;
using System;
using System.Collections.Generic;
namespace Mynamespace
{
internal class Program
{
private static IProcessTransactions _processTransactions;
private static IGetTransaction _getTransaction;
private static IApplyBusinessRoles _applyBusinessRoles;
private static List<string> lstMerchantName = new List<string>();
private static void Main()
{
try
{
const string filename = "Transactions.txt";
InjectDependicies();
SetFileName(filename);
var transactionsCount = _getTransaction.GetTransactionsCount();
if (transactionsCount <= 0) return;
while (true)
{
Console.WriteLine("User Id:1 or 2 or 3 or 4 or 5");
Console.WriteLine("To End The Program Type Exit");
Console.WriteLine("Enter User Id:");
var input = Console.ReadLine();
if (input?.ToLower() == "exit")
break;
if (!Helper.ValidateInput(input))
continue;
DisplayRawTransactions(transactionsCount);
if (input != null)
DisplayProcessedTransaction(transactionsCount, int.Parse(input));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void SetFileName(string filename)
{
_processTransactions.FileName = filename;
_getTransaction.FileName = filename;
}
private static void InjectDependicies()
{
var container = AutoFacBootstrapper.Init();
container.Resolve<IReadTransactionFromFile>();
container.Resolve<IGetClients>();
container.Resolve<IGetUsers>();
container.Resolve<IGeBusinessRoles>();
_getTransaction = container.Resolve<IGetTransaction>();
_processTransactions = container.Resolve<IProcessTransactions>();
_applyBusinessRoles = container.Resolve<IApplyBusinessRoles>();
}
private static void DisplayRawTransactions(int transactionsCount)
{
WriteHeaders("Raw");
for (var i = 0; i < transactionsCount; i++)
{
var rawTransaction = GetRawTransaction(i);
DisplayTransactions(rawTransaction.TransacionDate,
rawTransaction.MerchantName,
rawTransaction.Amount.ToString("F"));
}
}
private static Transaction GetRawTransaction(int i)
{
var rawTransaction = _getTransaction.GetSingleTransaction(i);
lstMerchantName?.Add(rawTransaction.MerchantName);
return rawTransaction;
}
private static void DisplayProcessedTransaction(int transactionsCount, int userId)
{
WriteHeaders("Processed");
for (var i = 0; i < transactionsCount; i++)
{
var processedTransaction = GetProcessedTransaction(userId, i);
DisplayTransactions(processedTransaction.TransacionDate,
processedTransaction.MerchantName,
processedTransaction.Fees.ToString("F"));
}
}
private static TransactionResult GetProcessedTransaction(int userId, int transactionIndex)
{
return _processTransactions.GetProcessedTransaction(transactionIndex,
_applyBusinessRoles.ApplyFees(userId, lstMerchantName[transactionIndex]));
}
private static void WriteHeaders(string transactionType)
{
Console.WriteLine("+++++ " + transactionType + " Transactions Data +++++");
Console.WriteLine("{0}t{1}t{2}", "Date", "Merchant Name",
transactionType == "Processed" ? "Fees" : "Amount");
Console.WriteLine("----------------------------");
}
private static void DisplayTransactions(DateTime transactionDate, string merchantName, string money)
{
if (money != null)
{
Console.WriteLine("{0}t{1}t{2}",
transactionDate.ToString("yyyy-MM-dd"),
merchantName,
money);
}
}
}
}
c# console
New contributor
add a comment |
up vote
-1
down vote
favorite
I'm developing a simple Console application. Program.cs initialize dependency injection, set file names, accept inputs and print the result.
I got a note that my Program.cs violates the SRP principle ,Program.cs must accept the inputs and print the results, must initialize dependencies. I don't know how to fix that, please advice.
using Autofac;
using Mynamespace.Core;
using Mynamespace.Core.Interfaces;
using Mynamespace.InfraSturcture;
using Mynamespace.Services;
using System;
using System.Collections.Generic;
namespace Mynamespace
{
internal class Program
{
private static IProcessTransactions _processTransactions;
private static IGetTransaction _getTransaction;
private static IApplyBusinessRoles _applyBusinessRoles;
private static List<string> lstMerchantName = new List<string>();
private static void Main()
{
try
{
const string filename = "Transactions.txt";
InjectDependicies();
SetFileName(filename);
var transactionsCount = _getTransaction.GetTransactionsCount();
if (transactionsCount <= 0) return;
while (true)
{
Console.WriteLine("User Id:1 or 2 or 3 or 4 or 5");
Console.WriteLine("To End The Program Type Exit");
Console.WriteLine("Enter User Id:");
var input = Console.ReadLine();
if (input?.ToLower() == "exit")
break;
if (!Helper.ValidateInput(input))
continue;
DisplayRawTransactions(transactionsCount);
if (input != null)
DisplayProcessedTransaction(transactionsCount, int.Parse(input));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void SetFileName(string filename)
{
_processTransactions.FileName = filename;
_getTransaction.FileName = filename;
}
private static void InjectDependicies()
{
var container = AutoFacBootstrapper.Init();
container.Resolve<IReadTransactionFromFile>();
container.Resolve<IGetClients>();
container.Resolve<IGetUsers>();
container.Resolve<IGeBusinessRoles>();
_getTransaction = container.Resolve<IGetTransaction>();
_processTransactions = container.Resolve<IProcessTransactions>();
_applyBusinessRoles = container.Resolve<IApplyBusinessRoles>();
}
private static void DisplayRawTransactions(int transactionsCount)
{
WriteHeaders("Raw");
for (var i = 0; i < transactionsCount; i++)
{
var rawTransaction = GetRawTransaction(i);
DisplayTransactions(rawTransaction.TransacionDate,
rawTransaction.MerchantName,
rawTransaction.Amount.ToString("F"));
}
}
private static Transaction GetRawTransaction(int i)
{
var rawTransaction = _getTransaction.GetSingleTransaction(i);
lstMerchantName?.Add(rawTransaction.MerchantName);
return rawTransaction;
}
private static void DisplayProcessedTransaction(int transactionsCount, int userId)
{
WriteHeaders("Processed");
for (var i = 0; i < transactionsCount; i++)
{
var processedTransaction = GetProcessedTransaction(userId, i);
DisplayTransactions(processedTransaction.TransacionDate,
processedTransaction.MerchantName,
processedTransaction.Fees.ToString("F"));
}
}
private static TransactionResult GetProcessedTransaction(int userId, int transactionIndex)
{
return _processTransactions.GetProcessedTransaction(transactionIndex,
_applyBusinessRoles.ApplyFees(userId, lstMerchantName[transactionIndex]));
}
private static void WriteHeaders(string transactionType)
{
Console.WriteLine("+++++ " + transactionType + " Transactions Data +++++");
Console.WriteLine("{0}t{1}t{2}", "Date", "Merchant Name",
transactionType == "Processed" ? "Fees" : "Amount");
Console.WriteLine("----------------------------");
}
private static void DisplayTransactions(DateTime transactionDate, string merchantName, string money)
{
if (money != null)
{
Console.WriteLine("{0}t{1}t{2}",
transactionDate.ToString("yyyy-MM-dd"),
merchantName,
money);
}
}
}
}
c# console
New contributor
1
Welcome to Code Review. Could you clarify, does this code produce the right output or not? It's hard for us to tell what's going on, because it calls code that you haven't shown us.
– 200_success
6 hours ago
add a comment |
up vote
-1
down vote
favorite
up vote
-1
down vote
favorite
I'm developing a simple Console application. Program.cs initialize dependency injection, set file names, accept inputs and print the result.
I got a note that my Program.cs violates the SRP principle ,Program.cs must accept the inputs and print the results, must initialize dependencies. I don't know how to fix that, please advice.
using Autofac;
using Mynamespace.Core;
using Mynamespace.Core.Interfaces;
using Mynamespace.InfraSturcture;
using Mynamespace.Services;
using System;
using System.Collections.Generic;
namespace Mynamespace
{
internal class Program
{
private static IProcessTransactions _processTransactions;
private static IGetTransaction _getTransaction;
private static IApplyBusinessRoles _applyBusinessRoles;
private static List<string> lstMerchantName = new List<string>();
private static void Main()
{
try
{
const string filename = "Transactions.txt";
InjectDependicies();
SetFileName(filename);
var transactionsCount = _getTransaction.GetTransactionsCount();
if (transactionsCount <= 0) return;
while (true)
{
Console.WriteLine("User Id:1 or 2 or 3 or 4 or 5");
Console.WriteLine("To End The Program Type Exit");
Console.WriteLine("Enter User Id:");
var input = Console.ReadLine();
if (input?.ToLower() == "exit")
break;
if (!Helper.ValidateInput(input))
continue;
DisplayRawTransactions(transactionsCount);
if (input != null)
DisplayProcessedTransaction(transactionsCount, int.Parse(input));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void SetFileName(string filename)
{
_processTransactions.FileName = filename;
_getTransaction.FileName = filename;
}
private static void InjectDependicies()
{
var container = AutoFacBootstrapper.Init();
container.Resolve<IReadTransactionFromFile>();
container.Resolve<IGetClients>();
container.Resolve<IGetUsers>();
container.Resolve<IGeBusinessRoles>();
_getTransaction = container.Resolve<IGetTransaction>();
_processTransactions = container.Resolve<IProcessTransactions>();
_applyBusinessRoles = container.Resolve<IApplyBusinessRoles>();
}
private static void DisplayRawTransactions(int transactionsCount)
{
WriteHeaders("Raw");
for (var i = 0; i < transactionsCount; i++)
{
var rawTransaction = GetRawTransaction(i);
DisplayTransactions(rawTransaction.TransacionDate,
rawTransaction.MerchantName,
rawTransaction.Amount.ToString("F"));
}
}
private static Transaction GetRawTransaction(int i)
{
var rawTransaction = _getTransaction.GetSingleTransaction(i);
lstMerchantName?.Add(rawTransaction.MerchantName);
return rawTransaction;
}
private static void DisplayProcessedTransaction(int transactionsCount, int userId)
{
WriteHeaders("Processed");
for (var i = 0; i < transactionsCount; i++)
{
var processedTransaction = GetProcessedTransaction(userId, i);
DisplayTransactions(processedTransaction.TransacionDate,
processedTransaction.MerchantName,
processedTransaction.Fees.ToString("F"));
}
}
private static TransactionResult GetProcessedTransaction(int userId, int transactionIndex)
{
return _processTransactions.GetProcessedTransaction(transactionIndex,
_applyBusinessRoles.ApplyFees(userId, lstMerchantName[transactionIndex]));
}
private static void WriteHeaders(string transactionType)
{
Console.WriteLine("+++++ " + transactionType + " Transactions Data +++++");
Console.WriteLine("{0}t{1}t{2}", "Date", "Merchant Name",
transactionType == "Processed" ? "Fees" : "Amount");
Console.WriteLine("----------------------------");
}
private static void DisplayTransactions(DateTime transactionDate, string merchantName, string money)
{
if (money != null)
{
Console.WriteLine("{0}t{1}t{2}",
transactionDate.ToString("yyyy-MM-dd"),
merchantName,
money);
}
}
}
}
c# console
New contributor
I'm developing a simple Console application. Program.cs initialize dependency injection, set file names, accept inputs and print the result.
I got a note that my Program.cs violates the SRP principle ,Program.cs must accept the inputs and print the results, must initialize dependencies. I don't know how to fix that, please advice.
using Autofac;
using Mynamespace.Core;
using Mynamespace.Core.Interfaces;
using Mynamespace.InfraSturcture;
using Mynamespace.Services;
using System;
using System.Collections.Generic;
namespace Mynamespace
{
internal class Program
{
private static IProcessTransactions _processTransactions;
private static IGetTransaction _getTransaction;
private static IApplyBusinessRoles _applyBusinessRoles;
private static List<string> lstMerchantName = new List<string>();
private static void Main()
{
try
{
const string filename = "Transactions.txt";
InjectDependicies();
SetFileName(filename);
var transactionsCount = _getTransaction.GetTransactionsCount();
if (transactionsCount <= 0) return;
while (true)
{
Console.WriteLine("User Id:1 or 2 or 3 or 4 or 5");
Console.WriteLine("To End The Program Type Exit");
Console.WriteLine("Enter User Id:");
var input = Console.ReadLine();
if (input?.ToLower() == "exit")
break;
if (!Helper.ValidateInput(input))
continue;
DisplayRawTransactions(transactionsCount);
if (input != null)
DisplayProcessedTransaction(transactionsCount, int.Parse(input));
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
private static void SetFileName(string filename)
{
_processTransactions.FileName = filename;
_getTransaction.FileName = filename;
}
private static void InjectDependicies()
{
var container = AutoFacBootstrapper.Init();
container.Resolve<IReadTransactionFromFile>();
container.Resolve<IGetClients>();
container.Resolve<IGetUsers>();
container.Resolve<IGeBusinessRoles>();
_getTransaction = container.Resolve<IGetTransaction>();
_processTransactions = container.Resolve<IProcessTransactions>();
_applyBusinessRoles = container.Resolve<IApplyBusinessRoles>();
}
private static void DisplayRawTransactions(int transactionsCount)
{
WriteHeaders("Raw");
for (var i = 0; i < transactionsCount; i++)
{
var rawTransaction = GetRawTransaction(i);
DisplayTransactions(rawTransaction.TransacionDate,
rawTransaction.MerchantName,
rawTransaction.Amount.ToString("F"));
}
}
private static Transaction GetRawTransaction(int i)
{
var rawTransaction = _getTransaction.GetSingleTransaction(i);
lstMerchantName?.Add(rawTransaction.MerchantName);
return rawTransaction;
}
private static void DisplayProcessedTransaction(int transactionsCount, int userId)
{
WriteHeaders("Processed");
for (var i = 0; i < transactionsCount; i++)
{
var processedTransaction = GetProcessedTransaction(userId, i);
DisplayTransactions(processedTransaction.TransacionDate,
processedTransaction.MerchantName,
processedTransaction.Fees.ToString("F"));
}
}
private static TransactionResult GetProcessedTransaction(int userId, int transactionIndex)
{
return _processTransactions.GetProcessedTransaction(transactionIndex,
_applyBusinessRoles.ApplyFees(userId, lstMerchantName[transactionIndex]));
}
private static void WriteHeaders(string transactionType)
{
Console.WriteLine("+++++ " + transactionType + " Transactions Data +++++");
Console.WriteLine("{0}t{1}t{2}", "Date", "Merchant Name",
transactionType == "Processed" ? "Fees" : "Amount");
Console.WriteLine("----------------------------");
}
private static void DisplayTransactions(DateTime transactionDate, string merchantName, string money)
{
if (money != null)
{
Console.WriteLine("{0}t{1}t{2}",
transactionDate.ToString("yyyy-MM-dd"),
merchantName,
money);
}
}
}
}
c# console
c# console
New contributor
New contributor
New contributor
asked 11 hours ago
Daina Hodges
12
12
New contributor
New contributor
1
Welcome to Code Review. Could you clarify, does this code produce the right output or not? It's hard for us to tell what's going on, because it calls code that you haven't shown us.
– 200_success
6 hours ago
add a comment |
1
Welcome to Code Review. Could you clarify, does this code produce the right output or not? It's hard for us to tell what's going on, because it calls code that you haven't shown us.
– 200_success
6 hours ago
1
1
Welcome to Code Review. Could you clarify, does this code produce the right output or not? It's hard for us to tell what's going on, because it calls code that you haven't shown us.
– 200_success
6 hours ago
Welcome to Code Review. Could you clarify, does this code produce the right output or not? It's hard for us to tell what's going on, because it calls code that you haven't shown us.
– 200_success
6 hours ago
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Daina Hodges is a new contributor. Be nice, and check out our Code of Conduct.
Daina Hodges is a new contributor. Be nice, and check out our Code of Conduct.
Daina Hodges is a new contributor. Be nice, and check out our Code of Conduct.
Daina Hodges is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f209385%2favoid-srp-violation-in-console-application%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Welcome to Code Review. Could you clarify, does this code produce the right output or not? It's hard for us to tell what's going on, because it calls code that you haven't shown us.
– 200_success
6 hours ago