public Material(MaterialId id, MaterialName name, MaterialType type, TypeAndSize typesize, Consumption consumption, Length length, Weight weight) { // 先にMaterialTypeがnullで無いことをチェックしないと、 // 後続のValidationが出来なくなる。(Typeがnullなので、nullReferenceErrorとかになるはず) if (id == null) { throw new ArgumentException(nameof(MaterialId)); } if (type == null) { throw new ArgumentException(nameof(MaterialType)); } this.Id = id; this.Type = type; if (!Type.ValidateName(name)) { throw new ArgumentException(nameof(name)); } if (!Type.ValidateLength(length)) { throw new ArgumentException(nameof(Length)); } if (!Type.ValidateWeight(weight)) { throw new ArgumentException(nameof(Weight)); } if (!Type.ValidateTypeAndSize(typesize)) { throw new ArgumentException(nameof(typesize)); } if (!Type.ValidateConsumption(consumption)) { throw new ArgumentException(nameof(consumption)); } this.Name = name; this.Length = length; this.Weight = weight; this.TypeAndSize = typesize; this.Consumption = consumption; }
public void Save(string id, string name, int materialTypeId, string productType, float?size, float?consumption, float?length, float?weight) { var Id = new MaterialId(id); var Name = new MaterialName(name); var Type = MaterialType.GetMaterialType(materialTypeId); var ProductType = new ProductType(productType); var Size = new Size(size); var TypeAndSize = new TypeAndSize(ProductType, Size); var Consumption = new Consumption(consumption); var Length = new Length(length); var Weight = new Weight(weight); // 値個別のバリデーションは、エンティティを生成する時に行う var target = new Material(Id, Name, Type, TypeAndSize, Consumption, Length, Weight); var service = new MaterialService(repository); if (service.IsDuplicatedId(target.Id)) { throw new Exception("IDが重複しています"); } if (Type.Id == MaterialType.A.Id && service.IsOverAddedMaterialA()) { throw new Exception("部材区分Aが2件登録されています"); } if (Type.Id == MaterialType.B.Id && service.IsOverAddedTypeAndSize(TypeAndSize)) { throw new Exception(nameof(TypeAndSize.Type.Value) + "と" + nameof(TypeAndSize.Size.Value) + "の組み合わせは2件登録されています"); } repository.Save(target); }
// 部材IDが重複しているか public bool IsDuplicatedId(MaterialId id) { Material material = repository.Find(id); return(material != null); }
public void Delete(MaterialId id) { repository.Delete(id); }
public Material Find(string id) { var Id = new MaterialId(id); return(repository.Find(Id)); }