Skip to content

Commit cf03922

Browse files
committed
ADVANCED RELATIONS
1 parent 6f0ae00 commit cf03922

40 files changed

+1136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace P01_BillsPaymentSystem.Core.Attributes
2+
{
3+
using System;
4+
5+
public class InjectAttribute : Attribute
6+
{
7+
}
8+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
namespace P01_BillsPaymentSystem.Core
2+
{
3+
using P01_BillsPaymentSystem.Core.Attributes;
4+
using P01_BillsPaymentSystem.Core.Commands.Contracts;
5+
using P01_BillsPaymentSystem.Core.Contracts;
6+
7+
using System;
8+
using System.Linq;
9+
using System.Reflection;
10+
11+
public class CommandInterpreter : ICommandInterpreter
12+
{
13+
private const string suffix = "Command";
14+
15+
private readonly IServiceProvider serviceProvider;
16+
17+
public CommandInterpreter(IServiceProvider serviceProvider)
18+
{
19+
this.serviceProvider = serviceProvider;
20+
}
21+
22+
public IExecutable InterpretCommand(string[] data)
23+
{
24+
var commandType = data[0] + suffix;
25+
data = data.Skip(1).ToArray();
26+
27+
Assembly assembly = Assembly.GetCallingAssembly();
28+
var model = assembly.GetTypes().FirstOrDefault(x => x.Name == commandType);
29+
30+
if (model == null)
31+
{
32+
throw new ArgumentException("Invalid type!");
33+
}
34+
35+
PropertyInfo[] propertiesToInject = model
36+
.GetProperties(BindingFlags.Instance | BindingFlags.Public)
37+
.Where(p => p.GetCustomAttributes<InjectAttribute>().Any())
38+
.ToArray();
39+
40+
var injectProps = propertiesToInject
41+
.Select(p => this.serviceProvider.GetService(p.PropertyType))
42+
.ToArray();
43+
44+
var joinedParams = new object[] { data }.Concat(injectProps).ToArray();
45+
46+
IExecutable command = (IExecutable)Activator.CreateInstance(model, joinedParams);
47+
48+
return command;
49+
}
50+
}
51+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands
2+
{
3+
using P01_BillsPaymentSystem.Core.Commands.Contracts;
4+
5+
public abstract class Command : IExecutable
6+
{
7+
public Command(string[] data)
8+
{
9+
this.Data = data;
10+
}
11+
12+
public string[] Data { get; protected set; }
13+
14+
public abstract void Execute();
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands.Contracts
2+
{
3+
public interface IExecutable
4+
{
5+
void Execute();
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands
2+
{
3+
using Attributes;
4+
using IO.Contracts;
5+
using P01_BillsPaymentSystem.Data;
6+
7+
public class PayBillsCommand : Command
8+
{
9+
public PayBillsCommand(string[] data, IUnitOfWork unitOfWork, IReader reader, IWriter writer)
10+
: base(data)
11+
{
12+
this.UnitOfWork = unitOfWork;
13+
this.Reader = reader;
14+
this.Writer = writer;
15+
}
16+
17+
[Inject]
18+
public IUnitOfWork UnitOfWork { get; set; }
19+
20+
[Inject]
21+
public IReader Reader { get; set; }
22+
23+
[Inject]
24+
public IWriter Writer { get; set; }
25+
26+
public override void Execute()
27+
{
28+
var id = int.Parse(this.Data[0]);
29+
var amount = decimal.Parse(this.Data[1]);
30+
31+
this.Writer.WriteLine(OutputMessages.Loading);
32+
var reuslt = this.UnitOfWork.Users.PayBills(id, amount);
33+
this.Writer.WriteLine(reuslt);
34+
35+
this.UnitOfWork.Complete();
36+
}
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
namespace P01_BillsPaymentSystem.Core.Commands
2+
{
3+
using Attributes;
4+
using IO.Contracts;
5+
using P01_BillsPaymentSystem.Data;
6+
7+
public class UserInfoCommand : Command
8+
{
9+
public UserInfoCommand(string[] data, IUnitOfWork unitOfWork, IReader reader, IWriter writer)
10+
: base(data)
11+
{
12+
this.UnitOfWork = unitOfWork;
13+
this.Reader = reader;
14+
this.Writer = writer;
15+
}
16+
17+
[Inject]
18+
public IUnitOfWork UnitOfWork { get; set; }
19+
20+
[Inject]
21+
public IReader Reader { get; set; }
22+
23+
[Inject]
24+
public IWriter Writer { get; set; }
25+
26+
public override void Execute()
27+
{
28+
var id = int.Parse(this.Data[0]);
29+
30+
this.Writer.WriteLine(OutputMessages.Loading);
31+
var reuslt = this.UnitOfWork.Users.GetUserAndAllPayments(id);
32+
this.Writer.WriteLine(reuslt);
33+
}
34+
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace P01_BillsPaymentSystem.Core.Contracts
2+
{
3+
using P01_BillsPaymentSystem.Core.Commands.Contracts;
4+
5+
public interface ICommandInterpreter
6+
{
7+
IExecutable InterpretCommand(string[] data);
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace P01_BillsPaymentSystem.Core.Contracts
2+
{
3+
public interface IEngine
4+
{
5+
void Run();
6+
}
7+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
namespace P01_BillsPaymentSystem.Core
2+
{
3+
using System;
4+
5+
using Contracts;
6+
using P01_BillsPaymentSystem.Core.IO.Contracts;
7+
8+
public class Engine : IEngine
9+
{
10+
private readonly IReader reader;
11+
private readonly IWriter writer;
12+
private readonly ICommandInterpreter commandInterpreter;
13+
14+
public Engine(IReader reader, IWriter writer, ICommandInterpreter commandInterpreter)
15+
{
16+
this.reader = reader;
17+
this.writer = writer;
18+
this.commandInterpreter = commandInterpreter;
19+
}
20+
21+
public void Run()
22+
{
23+
this.writer.WriteLine(OutputMessages.WelcomeString());
24+
25+
while (true)
26+
{
27+
try
28+
{
29+
this.writer.WriteLine(OutputMessages.MenuOptions());
30+
31+
var result = string.Empty;
32+
var input = this.reader.ReadLine().Split();
33+
34+
var command = this.commandInterpreter.InterpretCommand(input);
35+
command.Execute();
36+
}
37+
catch (Exception)
38+
{
39+
this.writer.WriteLine(OutputMessages.InvalidCommand);
40+
}
41+
}
42+
}
43+
}
44+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace P01_BillsPaymentSystem.Core.IO
2+
{
3+
using System;
4+
5+
using Contracts;
6+
7+
public class ConsoleReader : IReader
8+
{
9+
public string ReadLine()
10+
{
11+
return Console.ReadLine();
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)