Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I separate properties and methods in partial classes? [closed]

Tags:

c#

oop

I'm writing the foundational classes for an application, and want to construct them properly for long term maintainability. To that end, I'm researching which design patterns to implement, use of interfaces, abstract classes, and separating persistence from activity. My head is swimming with patterns, paradigms, and principles.

I have a class Product for which I have created an interface IProduct. I believe I need to make Product and abstract class, because any instance of it is required to be one of half a dozen values for the property Category. So, my first question: is the following the appropriate way to do that?

abstract class Product : IProduct
{
    // Fields
    // Properties
    // Methods
}

interface IProduct
{
    // Properties
}

public class Model : IProduct
{
    public Model()
    {
        Category = "Model";
    }

    // Model-specific fields
    // Model-specific properties
    // Model-specific methods
}

Articles I read, including questions previously answered here, indicate that I should design with separation of properties and methods (persistence and activity). To that end, should the above code really look like this?

abstract class Product : IProduct
{
    // Fields
    // Properties
    // Methods
}

interface IProduct
{
    // Properties
}

public partial class Model : IProduct
{
    public Model()
    {
        Category = "Model";
    }

    // Model-specific fields
    // Model-specific properties
}

public partial class Model : IProduct
{
    // Model-specific methods
}

Of course, that presumes I got the first part right, but perhaps the answers will enlighten me on how I should be doing things.

Lastly, if separation of properties and methods is a good thing, and Product has some methods that are applicable across all concrete versions of it, should I move them to a separate abstract class like this?

abstract class Product : IProduct
{
    // Fields
    // Properties
}
abstract class Product : IProduct
{
    // Methods
}
like image 963
J.D. Ray Avatar asked Mar 17 '23 04:03

J.D. Ray


1 Answers

The only use I see in keeping partial classes is when two separate systems update the two files. This is true for example when using Visual Studio designers (the Windows Forms designer for instance), that update their own class file. Another thing could be true for another auto-generated class you have. One is maintained by the system, one by you.

I never ever felt the urge to have two separate partial class files I maintain myself. I usually use #region directives to split the methods and the properties.

like image 112
Patrick Hofman Avatar answered Apr 26 '23 16:04

Patrick Hofman