Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Яценко Ирина #194

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@

![Getting Started](./TEST.png)

# Dependency Injection Container

DI-контейнеры используется во многих команд Контура для управления зависимостями с соблюдением Dependency Inversion Principle.
Expand Down Expand Up @@ -32,4 +35,4 @@ DI-контейнеры используется во многих команд
Найди в своем проекте интересные места, которые сильно расходятся с идеями из видеолекций. Либо наоборот иллюстрируют и дополняют эти идеи. Обрати внимание на:
- Нарушение явного управления зависимостями / Dependency Injection. Чем это было обосновано, какую выгоду получили?
- Активное, нестандартное, хитрое использование DI-контейнера. Чему могли бы научиться на этом примере другие?
- Проекты, в которых есть сборка сложного графа зависимостей, но DI-контейнера нет. Почему нет? Есть ли причины его не использовать?
- Проекты, в которых есть сборка сложного графа зависимостей, но DI-контейнера нет. Почему нет? Есть ли причины его не использовать?
Binary file added TEST.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions TagCloudGenerator/ITextProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace TagCloudGenerator
{
public interface ITextProcessor
{
string[] ProcessText(string[] file);
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}
}
17 changes: 17 additions & 0 deletions TagCloudGenerator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using CommandLine;
using TagCloudGenerator;

public class Program
{
static TagCloudDrawer tagCloudDrawer = new TagCloudDrawer();
public class Options
{
[Option('p', "path", Required = true, HelpText = "The path for the text file.")]
public string Path { get; set; }
}
public static void Main(string[] args)
{
Parser.Default.ParseArguments<Options>(args)
.WithParsed<Options>(o => tagCloudDrawer.DrawWordsCloud(o.Path));
}
}
8 changes: 8 additions & 0 deletions TagCloudGenerator/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
"profiles": {
"TagCloudGenerator": {
"commandName": "Project",
"commandLineArgs": "-p \"C:\\Users\\lholy\\Documents\\GitHub\\di\\TagCloudGeneratorTest\\TestsData\\test3.txt\""
}
}
}
54 changes: 54 additions & 0 deletions TagCloudGenerator/TagCloudDrawer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System.Drawing;
using TagsCloudVisualization.PointDistributors;
using TagsCloudVisualization;
using System.Reflection;

namespace TagCloudGenerator
{
public class TagCloudDrawer
{
public VisualizingSettings settings;
public void DrawWordsCloud(string filePath)
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
var tagCloudDrawer = new TagCloudDrawer();
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved

var words = ReadTextFromFile(filePath);
tagCloudDrawer.Draw(words);

Console.WriteLine($"The tag cloud is drawn, the path to the image: {Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase)}");
}

private string[] ReadTextFromFile(string filePath)
{
TextProcessor textProcessor = new TextProcessor();

var text = File.ReadAllLines(filePath);
return textProcessor.ProcessText(text);
}

private void Draw(string[] text)
{
var center = new Point(500, 500);
var distributor = new Spiral(1,center, 0.1);
var layouter = new CircularCloudLayouter(center, distributor);
var brush = new SolidBrush(Color.Aqua);
var font = new Font("Arial", 24);
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved

var bitmap = new Bitmap(1000, 1000);
var graphics = Graphics.FromImage(bitmap);
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved

for (var i = 0; i < text.Length; i++)
{
SizeF size = graphics.MeasureString(text[i], font);
var rect = layouter.PutNextRectangle(size.ToSize());

if (i == 0 )
distributor.centerOnPoint = true;

graphics.DrawString(text[i], font, brush, rect.X, rect.Y);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если смотреть на это и на Console.WriteLine() чуть выше, то кажется что класс делает два дела. Может его надо разделить?
И, bitmap.Save можно считать третьим делом. Например, если будет GUI клиент и надо не сохранять в файл, а рисовать на экран?

}

bitmap.Save("Test33.png");
}
}
}
24 changes: 24 additions & 0 deletions TagCloudGenerator/TagCloudGenerator.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0-windows</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="CommandLineParser" Version="2.9.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="NuGet.CommandLine" Version="6.8.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="WeCantSpell.Hunspell" Version="5.0.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TagsCloudVisualization\TagsCloudVisualization.csproj" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions TagCloudGenerator/TextProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace TagCloudGenerator
{
public class TextProcessor : ITextProcessor
{
public string[] ProcessText(string[] file)
{
for (var i = 0; i < file.Length; i++)
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
file[i] = file[i].ToLower();
}

return file;
}
}
}
16 changes: 16 additions & 0 deletions TagCloudGenerator/TextProcessorWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace TagCloudGenerator
{
public abstract class TextProcessorWrapper : ITextProcessor
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
protected readonly ITextProcessor textProcessor;
public TextProcessorWrapper(ITextProcessor textProcessor)
{
this.textProcessor = textProcessor;
}

public virtual string[] ProcessText(string[] file)
{
return textProcessor.ProcessText(file);
}
}
}
21 changes: 21 additions & 0 deletions TagCloudGenerator/WordCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace TagCloudGenerator
{
public class WordCounter
{
public Dictionary<string, int> words;
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved

public Dictionary<string, int> CountWords(string text)
{
words = new Dictionary<string, int>();

foreach (string word in text.Split(' '))
{
if (words.ContainsKey(word))
words[word]++;
else words[word] = 1;
}

return words;
}
}
}
26 changes: 26 additions & 0 deletions TagCloudGeneratorTest/TagCloudGeneratorTest.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.3.0" />
<PackageReference Include="NUnit.Analyzers" Version="3.5.0" />
<PackageReference Include="coverlet.collector" Version="3.1.2" />
</ItemGroup>

<ItemGroup>
<Folder Include="TestsData\" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\TagCloudGenerator\TagCloudGenerator.csproj" />
</ItemGroup>

</Project>
42 changes: 42 additions & 0 deletions TagCloudGeneratorTest/TagCloudGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using FluentAssertions;
using NUnit.Framework;
using TagCloudGenerator;
using System.IO;
using System;

namespace TagCloudGeneratorTest
{
public class Tests
{
private TextProcessor textProcessor;

[SetUp]
public void Setup()
{
var processor = new TextProcessor();
textProcessor = processor;
}

[Test]
public void WhenPassWordsInUppercase_ShouldReturnWordsInLowerCase()
{
var filePath = @"C:\Users\lholy\Documents\GitHub\di\TagCloudGeneratorTest\TestsData\test1.txt";
var file = File.ReadAllLines(filePath);
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved

var fileText = textProcessor.ProcessText(file);

var result = "";
for(var i = 0; i < fileText.Length; i++)
{
if (i == fileText.Length - 1)
{
result += fileText[i];
continue;
}
result += (fileText[i] + Environment.NewLine);
}

result.Should().Be("��������\r\n������\r\n�����\r\n��\r\n�����");
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
5 changes: 5 additions & 0 deletions TagCloudGeneratorTest/TestsData/test1.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Создание
Облака
Тегов
Из
Файла
3 changes: 3 additions & 0 deletions TagCloudGeneratorTest/TestsData/test2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
из
оно
на
26 changes: 26 additions & 0 deletions TagCloudGeneratorTest/TestsData/test3.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Sun
of
the
sleepless
Melancholy
star
Whose
tearful
beam
glows
tremulously
far
That
show’st
the
darkness
thou
canst
not
dispel
How
like
art
thou
to
joy
52 changes: 52 additions & 0 deletions TagsCloudVisualization/CircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Drawing;
using System.Linq;
using TagsCloudVisualization.PointDistributors;

namespace TagsCloudVisualization
{
public class CircularCloudLayouter : ICircularCloudLayouter
{
private readonly Cloud cloud;
private readonly IPointDistributor distributor;

public CircularCloudLayouter(Point center, IPointDistributor type)
LevShisterov marked this conversation as resolved.
Show resolved Hide resolved
{
cloud = new Cloud(center);
distributor = type;
}

public Rectangle PutNextRectangle(Size rectangleSize)
{
if (rectangleSize.Width <= 0 || rectangleSize.Height <= 0)
throw new ArgumentException();

if (cloud.Rectangles.Count == 0)
return AddToCenterPosition(rectangleSize);

var rectangle = new Rectangle(distributor.GetPosition(), rectangleSize);

while (HaveIntersection(rectangle))
{
rectangle.Location = distributor.GetPosition();
}

cloud.Rectangles.Add(rectangle);

return rectangle;
}

private Rectangle AddToCenterPosition(Size rectangleSize)
{
var newRectangle = new Rectangle(new Point(cloud.Center.X - rectangleSize.Width / 2,
cloud.Center.Y - rectangleSize.Height / 2), rectangleSize);

cloud.Rectangles.Add(newRectangle);

return newRectangle;
}

private bool HaveIntersection(Rectangle newRectangle) =>
cloud.Rectangles.Any(rectangle => rectangle.IntersectsWith(newRectangle));
}
}
16 changes: 16 additions & 0 deletions TagsCloudVisualization/Cloud.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;
using System.Drawing;

namespace TagsCloudVisualization
{
public class Cloud
{
public Cloud(Point center)
{
Center = center;
}

public readonly Point Center;
public readonly List<Rectangle> Rectangles = new();
}
}
9 changes: 9 additions & 0 deletions TagsCloudVisualization/ICircularCloudLayouter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace TagsCloudVisualization
{
public interface ICircularCloudLayouter
{
Rectangle PutNextRectangle(Size rectangleSize);
}
}
9 changes: 9 additions & 0 deletions TagsCloudVisualization/PointDistributors/IPointDistributor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Drawing;

namespace TagsCloudVisualization.PointDistributors
{
public interface IPointDistributor
{
Point GetPosition();
}
}
Loading