0

I have lots of class who inherit abstract class and many interface. but problem is class should implement members declared in interface. so I should type lots of duplicate code.

I think that violates OOP's DRY principle, So I tried many things but below is my best.

Is there any way better than mine?

Current

public interface InterfaceWithProperties
{
    public int IntegerProperty { get; set; }
    public string StringProperty { get; set; }
}

public abstract class AbstractClass1 
{
    public abstract Guid GuidProperty1;
}

public abstract class AbstractClass2
{
    public abstract Guid GuidProperty2;
}

public sealed class DerivativeClass1 : AbstractClass1, InterfaceWithProperties
{
    [TargetAttribute(ValueForDerivativeClass1)] public override Guid GuidProperty1 { get; set; }

    [SomeAttribute] public int IntegerProperty { get; set; }
    [SomeAttribute] public string StringProperty { get; set; }
}

public sealed class DerivativeClass2 : AbstractClass2, InterfaceWithProperties
{
    [TargetAttribute(ValueForDerivativeClass2)] public override GuidProperty2 { get; set; }

    [SomeAttribute] public int IntegerProperty { get; set; }
    [SomeAttribute] public string StringProperty { get; set; }
}

There could be a lot of sealed derivative class with different base class but same interface. so below parts duplicate many times

{
    [SomeAttribute] public int IntegerProperty { get; set; }
    [SomeAttribute] public string StringProperty { get; set; }
}

So Is There a way to declare this part of code for my purpose?

for example I imagine some "augmentation" type achieve like this.

public augmentation AugmentationWithProperties : InterfaceWithProperties
{
    [SomeAttribute] public int IntegerProperty { get; set; }
    [SomeAttribute] public string StringProperty { get; set; }
}

so now derivative classes look like these

public sealed class DerivativeClass1 : AbstractClass1, AugmentationWithProperties
{
    [TargetAttribute(ValueForDerivativeClass1)] public override Guid GuidProperty1 { get; set; }
}

public sealed class DerivativeClass2 : AbstractClass2, AugmentationWithProperties
{
    [TargetAttribute(ValueForDerivativeClass2)] public override Guid GuidProperty2 { get; set; }
}

Is There a Way to achieve this?

Please Give me your advice, Thank you.

  • **You code doesn't compile**. You cannot have `abstract` fields. (`public abstract Guid GuidProperty1;`) –  Dec 15 '22 at 02:37
  • What do GuidProperty1 and GuidProperty2 signify? Do they represent something like id? Perhaps they could be called Id in that case (i.e. one property). What is TargetAttribute's purpose here? – ProgrammingLlama Dec 15 '22 at 02:39
  • Each class have their own Identity, and by TargetAttribute Classes are linked. Finally, they make a big data tree – SangYoung Ha Dec 15 '22 at 02:42
  • Unfortunately I still don't really understand what you're trying to achieve here. The problem is too abstract because it lacks detail. – ProgrammingLlama Dec 15 '22 at 02:44

0 Answers0