Первые два класса моделей.

This commit is contained in:
Programmist73 2023-02-13 23:46:35 +04:00
parent 3cd62dec9e
commit 2f4c7b4db4
5 changed files with 200 additions and 2 deletions

View File

@ -11,7 +11,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopContracts
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopBusinessLogic", "BlacksmithWorkshopBusinessLogic\BlacksmithWorkshopBusinessLogic.csproj", "{B3C97222-2894-4D74-B0D7-B3BEB347081D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopListImplement", "BlacksmithWorkshopListImplement\BlacksmithWorkshopListImplement.csproj", "{2942D468-0C3B-4B8D-A15F-184F5D7C969A}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopListImplement", "BlacksmithWorkshopListImplement\BlacksmithWorkshopListImplement.csproj", "{2942D468-0C3B-4B8D-A15F-184F5D7C969A}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{58737D93-9A12-4D07-BF3F-86AC512CE626}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -39,6 +41,10 @@ Global
{2942D468-0C3B-4B8D-A15F-184F5D7C969A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2942D468-0C3B-4B8D-A15F-184F5D7C969A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2942D468-0C3B-4B8D-A15F-184F5D7C969A}.Release|Any CPU.Build.0 = Release|Any CPU
{58737D93-9A12-4D07-BF3F-86AC512CE626}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58737D93-9A12-4D07-BF3F-86AC512CE626}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58737D93-9A12-4D07-BF3F-86AC512CE626}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58737D93-9A12-4D07-BF3F-86AC512CE626}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@ -0,0 +1,108 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BlacksmithWorkshopFileImplement.Models
{
//класс реализующий интерфейс модели изделия
public class Manufacture : IManufactureModel
{
public int Id { get; private set; }
public string ManufactureName { get; private set; } = string.Empty;
public double Price { get; private set; }
public Dictionary<int, int> WorkPieces { get; private set; } = new();
private Dictionary<int, (IWorkPieceModel, int)>? _manufactureWorkPieces = null;
public Dictionary<int, (IWorkPieceModel, int)> ManufactureWorkPieces
{
get
{
if (_manufactureWorkPieces == null)
{
var source = DataFileSingleton.GetInstance();
_manufactureWorkPieces = WorkPieces.ToDictionary(x => x.Key,
y => ((source.WorkPieces.FirstOrDefault(z => z.Id == y.Key) as IWorkPieceModel)!, y.Value));
}
return _manufactureWorkPieces;
}
}
public static Manufacture? Create(ManufactureBindingModel model)
{
if (model == null)
{
return null;
}
return new Manufacture()
{
Id = model.Id,
ManufactureName = model.ManufactureName,
Price = model.Price,
WorkPieces = model.ManufactureWorkPieces.ToDictionary(x => x.Key, x => x.Value.Item2)
};
}
public static Manufacture? Create(XElement element)
{
if (element == null)
{
return null;
}
return new Manufacture()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
ManufactureName = element.Element("ManufactureName")!.Value,
Price = Convert.ToDouble(element.Element("Price")!.Value),
WorkPieces = element.Element("ManufactureWorkPieces")!.Elements("ManufactureComponent").ToDictionary(
x => Convert.ToInt32(x.Element("Key")?.Value),
x => Convert.ToInt32(x.Element("Value")?.Value))
};
}
public void Update(ManufactureBindingModel model)
{
if (model == null)
{
return;
}
ManufactureName = model.ManufactureName;
Price = model.Price;
WorkPieces = model.ManufactureWorkPieces.ToDictionary(x => x.Key, x => x.Value.Item2);
_manufactureWorkPieces = null;
}
public ManufactureViewModel GetViewModel => new()
{
Id = Id,
ManufactureName = ManufactureName,
Price = Price,
ManufactureWorkPieces = ManufactureWorkPieces
};
public XElement GetXElement => new("Manufacture",
new XAttribute("Id", Id),
new XElement("ManufactureName", ManufactureName),
new XElement("Price", Price.ToString()),
new XElement("ManufactureWorkPieces", WorkPieces.Select(
x => new XElement("ManufactureComponent",
new XElement("Key", x.Key),
new XElement("Value", x.Value))
).ToArray()));
}
}

View File

@ -0,0 +1,75 @@
using BlacksmithWorkshopContracts.BindingModels;
using BlacksmithWorkshopContracts.ViewModels;
using BlacksmithWorkshopDataModels.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace BlacksmithWorkshopFileImplement.Models
{
//реализация интерфейса модели заготовки
public class WorkPiece : IWorkPieceModel
{
public int Id { get; private set; }
public string WorkPieceName { get; private set; } = string.Empty;
public double Cost { get; set; }
public static WorkPiece? Create(WorkPieceBindingModel model)
{
if (model == null)
{
return null;
}
return new WorkPiece()
{
Id = model.Id,
WorkPieceName = model.WorkPieceName,
Cost = model.Cost
};
}
public static WorkPiece? Create(XElement element)
{
if (element == null)
{
return null;
}
return new WorkPiece()
{
Id = Convert.ToInt32(element.Attribute("Id")!.Value),
WorkPieceName = element.Element("WorkPieceName")!.Value,
Cost = Convert.ToDouble(element.Element("Cost")!.Value)
};
}
public void Update(WorkPieceBindingModel model)
{
if (model == null)
{
return;
}
WorkPieceName = model.WorkPieceName;
Cost = model.Cost;
}
public WorkPieceViewModel GetViewModel => new()
{
Id = Id,
WorkPieceName = WorkPieceName,
Cost = Cost
};
public XElement GetXElement => new("WorkPiece",
new XAttribute("Id", Id),
new XElement("WorkPieceName", WorkPieceName),
new XElement("Cost", Cost.ToString()));
}
}

View File

@ -9,7 +9,7 @@ using System.Threading.Tasks;
namespace BlacksmithWorkshopListImplement.Models
{
//класс реализующий интерфейс модели изделия
//класс, реализующий интерфейс модели изделия
public class Manufacture : IManufactureModel
{
//методы set делаем приватным, чтобы исключить неразрешённые манипуляции