DataAnnotations inheritance via interface

I have 2 twin classes representing “the same” objects: one defined for computational evaluations and the other for the UI binding. I would like to reduce the duplicated code and errors. I’ve defined a common interface and moved all the shared static methods by using Extension methods.

What is missing is how to define properties validation in just one place.

I’m currently using System.ComponentModel.DataAnnotations but I need to define the same annotations on both classes. In the base class I use DataAnnotation to check JSON deserialization while the same values are used in the UI.
I can not use inheritance because the UI class is already inheriting from a ViewModel class and multiple inheritance is not allowed.

Any other approach or idea?

Here a project with the same basic concept issue.

using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.CompilerServices;

namespace myRefactoring
{
    public interface IShape
    {
        // would be nice to define the DataAnnotation here
        // but it's not working with interfaces

        public int N { get; set; }
        public double side { get; set; }

        public double perimeter { get; }
        public double area { get; }
    }


    public class Shape : IShape
    {
        /// <summary>
        /// main class for math evaluations
        /// </summary>

        [Range(3, int.MaxValue, ErrorMessage = "at least 3 sides")]
        public int N { get; set; }

        [Range(double.Epsilon, double.MaxValue, ErrorMessage = "just positive size")]
        public double side { get; set; }

        public Shape(int N, double S) { this.N = N; this.side = S; }

        public double perimeter => ShapeExtension.perimeter(this);
        public double area => ShapeExtension.area(this);

    }

    public class ShapeUI : ViewModel, IShape, INotifyPropertyChanged
    {
        /// <summary>
        /// class for UI binding
        /// NotifyPropertyChanged ...
        /// </summary>

        public event PropertyChangedEventHandler? PropertyChanged;

        public ShapeUI(int N, double S) { this._N = N; this._side = S; }

        private int _N;

        [Range(3, int.MaxValue, ErrorMessage = "at least 3 sides")]
        public int N { 
            get => _N;
            set {
                _N = value;
                NotifyPropertyChanged();
                NotifyPropertyChanged(nameof(perimeter));
                NotifyPropertyChanged(nameof(area));
            }
        }

        private double _side;

        [Range(double.Epsilon, double.MaxValue, ErrorMessage = "just positive size")]
        public double side { 
            get => _side; 
            set {
                if (value != this._side)
                { 
                    this._side = value;
                    NotifyPropertyChanged();
                    NotifyPropertyChanged(nameof(perimeter));
                    NotifyPropertyChanged(nameof(area));
                }
            }
        }

        public double perimeter => ShapeExtension.perimeter(this);
        public double area => ShapeExtension.area(this);

        private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            Console.WriteLine("changing " + propertyName);
        }

    }


    public static class ShapeExtension
    {
        public static double perimeter(this IShape s) => s.N * s.side;
        public static double area(this IShape s) => Math.Pow(s.side / 2, 2) / Math.Tan(Math.PI / s.N) * s.N;
    }

    public class ViewModel
    {
        // class implementing ViewModel methods and variables
    }



    internal class Program
    {
        static void Main(string[] ars)
        {
            Console.WriteLine("Create triangle");
            Shape triangle = new(3, 1.0);
            Console.WriteLine($"area = {triangle.area:F3}");

            Console.WriteLine("nnCreate square");
            ShapeUI square = new(4, 1.0);
            Console.WriteLine($"area = {square.area:F3}");

            Console.WriteLine("nnModify square");
            square.side = 2.0;
            Console.WriteLine($"area = {square.area:F3}");
        }
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật