Добавление сеттера в производном интерфейсе или классе
Имеется объекты со свойствами: Id, Name, Value.
Объекты могут быть разных типов:
- ReadOnly - все свойства доступны только для чтения;
- IdReadOnly -
Idдоступно только для чтения, свойстваNameиValueдля чтения и записи; - NameReadOnly -
Nameдоступно только для чтения, свойстваIdиValueдля чтения и записи;
Предполагалось создание интерфейса или класса с доступом ко всем свойствам только для чтения, а в производных интерфейсах или классах добавление нужным свойствам доступа на запись.
На данный момент делаю так:
public interface IProperty
{
int Id { get; }
string Name { get; }
string Value { get; }
}
public abstract class ReadOnlyPropertyBase
{
public int Id { get; }
public string Name { get; }
public string Value { get; }
}
public sealed class ReadOnlyProperty : ReadOnlyPropertyBase, IProperty { }
public sealed class IdReadOnlyProperty : ReadOnlyPropertyBase, IProperty
{
public string Name { get; set; }
public string Value { get; set; }
}
public sealed class NameReadOnlyProperty : ReadOnlyPropertyBase, IProperty
{
public int Id { get; set; }
public string Value { get; set; }
}
В принципе это работает.
Но как можно видеть выше, приходится в ручном режиме "синхронизировать" интерфейс IProperty и абстрактный класс.
Также имеется вот такой вариант:
public interface IProperty
{
int Id { get; }
string Name { get; }
string Value { get; }
}
public sealed class ReadOnlyProperty : IProperty
{
public int Id { get; }
public string Name { get; }
public string Value { get; }
public ReadOnlyProperty(int id, string name, string value)
{
Id = id;
Name = name;
Value = value;
}
}
public sealed class IdReadOnlyProperty : IProperty
{
public int Id { get; }
public string Name { get; set; }
public string Value { get; set; }
public IdReadOnlyProperty(int id, string name, string value)
{
Id = id;
Name = name;
Value = value;
}
}
public sealed class NameReadOnlyProperty : IProperty
{
public int Id { get; set; }
public string Name { get; }
public string Value { get; set; }
public NameReadOnlyProperty(int id, string name, string value)
{
Id = id;
Name = name;
Value = value;
}
}
Но не знаю какими подводными камнями он обладает...
И ещё один...
public interface IProperty
{
int Id { get; }
string Name { get; }
string Value { get; }
}
public abstract class PropertyBase : IProperty
{
public int Id { get; }
public string Name { get; }
public string Value { get; }
protected PropertyBase(int id, string name, string value)
{
Id = id;
Name = name;
Value = value;
}
}
public sealed class ReadOnlyProperty : PropertyBase
{
public ReadOnlyProperty(int id, string name, string value) : base(id, name, value) { }
}
public sealed class IdReadOnlyProperty : PropertyBase
{
public string Name { get; set; }
public string Value { get; set; }
public IdReadOnlyProperty(int id, string name, string value) : base(id, name, value) { }
}
public sealed class NameReadOnlyProperty : PropertyBase
{
public int Id { get; set; }
public string Value { get; set; }
public NameReadOnlyProperty(int id, string name, string value) : base(id, name, value) { }
}
В связи с чем появились некоторые вопросы:
- какой из представленных вариантов правильный?
- каким образом принято решать подобные задачи?
P.S.: большое спасибо VladD'у за помощь!