Witam serdecznie.
Ostatnio zagłębiam się w tematykę zasad SOLID i postanowiłem napisać cokolwiek żeby sprawdzić czy dobrze to rozumiem i czy w praktyce jestem w stanie zastosować te zasady.
W związku z tym ma prośbę. Czy możecie "rzucić okiem" na ten kod i powiedzieć czy waszym zdaniem jest on zgodny z zasadami SOLID ?
Bardzo proszę o wytykanie wszelkich błędów i sugerowanie co należy zrobić inaczej.
Największy problem mam z interfejsami. Jakoś nie jestem w stanie intuicyjnie znaleźć dla nich zastosowania.
Na warsztat wziąłem NIP i kod poczty, bo to chyba niezły przykład do testowania.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace Utility
{
public enum Tytuly { Pan, Pani, Panna }
public enum CountryCode { PL, EN, DE }
public enum TypAdresu { AdresDoKorespondencji, AdresZamieszkania, AdresSiedziby, AdresDostawy, AdrsDoFaktury }
public abstract class Osoba
{
public Tytuly Tytul { get; set; }
public string Imie { get; set; }
public string Nazwisko { get; set; }
BaseAdres[] Adresy { get; set; }
}
public abstract class Firma
{
public string NazwaSkrot { get; set; }
public string NazwaPelna { get; set; }
BaseAdres[] Adresy { get; set; }
NumerNIP Nip { get; set; }
}
public abstract class KlientOsoba : Osoba
{
public NumerNIP Nip { get; set; }
}
public abstract class BaseAdres
{
public TypAdresu Typ { get; set; }
public string Miejscowosc { get; set; }
public string Ulica { get; set; }
public string NrDomu { get; set; }
public string NrLokalu { get; set; }
public string Poczta { get; set; }
public KodPocztowy KodPocztowy { get; set; }
}
interface IValidatable
{
bool IsValid();
}
public abstract class KodPocztowy : IValidatable
{
public string Kod { get; set; }
public abstract bool IsValid();
public static bool FormatCorrect(string format, string kodPoczowy)
{
Regex rgx = new Regex(format);
Match match = rgx.Match(kodPoczowy);
return match.Success;
}
}
public class KodPocztowyPL : KodPocztowy
{
string PostalCodeRGX = @"^\b{2}-\b{3}";
public override bool IsValid()
{
return (FormatCorrect(PostalCodeRGX, this.Kod));
}
}
public class KodPocztowyEN : KodPocztowy
{
string PostalCodeRGX = @"^[A-Z][A-Z]\d+$";
public override bool IsValid()
{
return (FormatCorrect(PostalCodeRGX, this.Kod));
}
}
public abstract class NumerNIP : IValidatable
{
public NumerNIP() { }
public NumerNIP(string nip) { Nip = nip; }
public string Nip { get; set; }
public abstract bool IsValid();
protected abstract bool IsValidChecksum(string nip);
internal static bool IsValidFormat(string[] format, string nrNIP)
{
bool isCorrect = false;
foreach (string s in format)
{
Regex rgx = new Regex(s);
Match match = rgx.Match(nrNIP);
if (match.Success)
{
isCorrect = true;
break;
}
}
return isCorrect;
}
}
public class NumerNIP_PL : NumerNIP
{
public NumerNIP_PL() : base() { }
public NumerNIP_PL(string n) : base(n) { }
string[] NipFormat = {
@"^PL\d{3}-\d{3}-\d{2}-\d{2}$",
@"^PL\d{3}-\d{2}-\d{3}-\d{2}$",
@"^PL\d{3}-\d{2}-\d{2}-\d{3}$",
@"^PL\d{10}$",
@"^\d{3}-\d{3}-\d{2}-\d{2}$",
@"^\d{3}-\d{2}-\d{3}-\d{2}$",
@"^\d{3}-\d{2}-\d{2}-\d{3}$",
@"^\d{10}$"
};
protected override bool IsValidChecksum(string nip)
{
return true; //TODO Weryfikacja sumy kontrolnej NIP dla PL
}
public override bool IsValid()
{
return (IsValidFormat(NipFormat, this.Nip) && IsValidChecksum(this.Nip));
}
}
public class NumerNIP_EN : NumerNIP
{
string[] NipFormat = {
@"^EN\d{3}-\d{3}-\d{2}-\d{2}$",
@"^EN\d{3}-\d{2}-\d{3}-\d{2}$",
@"^EN\d{3}-\d{2}-\d{2}-\d{3}$",
@"^EN\d{10}$"
};
protected override bool IsValidChecksum(string nip)
{
return true; //TODO Weryfikacja sumy kontrolnej NIP dla EN
}
public override bool IsValid()
{
return (IsValidFormat(NipFormat, this.Nip) && IsValidChecksum(this.Nip));
}
}
}
```