开发者

Any way to make a mutable object derived from an immutable object in C#?

开发者 https://www.devze.com 2022-12-12 14:04 出处:网络
I\'m interested in making an immutable class that has properties that cannot be modified, and a mutable class that derives from it.These objects would be simple data objects representing database reco

I'm interested in making an immutable class that has properties that cannot be modified, and a mutable class that derives from it. These objects would be simple data objects representing database records, so they would have only immutable values for the properties.

My goal is to have m开发者_运维知识库y data access layer create the mutable versions of the objects normally, by setting the properties based on the database values, but passing readonly versions of these objects back to the application. (I'm ok with the fact that the code could explicitly cast the immutable object back into a mutable one if the developer really wanted to.)

I can do this uglily (is that a word?), using methods for getters and setters and having a public new setter for the mutable class:

public class ImmutableClient {
    private int _clientId;
    public int getClientId() { return _clientId; }
    protected void setClientId(int clientId) { _clientId = clientId; }
}

public class Client : ImmutableClient {
    public new void setClientId(int clientId) { base.setClientId(clientId); }
}

I'd prefer to use auto-properties if possible - they're much nicer when debugging. Other than that, I don't really care how the code looks, since it's just going to all come from a code generator and never be looked at.

Any ideas?


I would use an interface. Have a read only interface that is returned to the client and a fully writeable class that implements it but is only used in the data access layer.

interface IReadOnlyObject
{
    int Property { get; }
}

class DALObject : IReadOnlyObject
{
    public int Property { get; set; }
}


You can have your data layer return a read-only interface:

interface IClient
{
     int getClientId();
}

Then you don't need to care if your concrete implementation has a setter or not - your callers will only use the getter.

As a side note - your code looks more like Java than C#, so I would actually use:

interface IClient
{
     int ClientId { get; }
}


Immutable means immutable. When I see that a type is immutable, I know that I can cache it and operate on it multiple times without it ever changing. Mutable state makes that assumption worthless.

class Foo
{
    // I'm immutable
}

class Bar : Foo
{
    // I add mutable properties
}

...

void F(Foo foo)
{
    // foo is immutable - therefore Bar (mutable) *cannot* be derived from Foo.
}


Have a base class which provides no mutators but provides no promise of immutability. Have an derived immutable class and a separately-derived mutable class. All classes should have a constructor that accepts the base class, and an overloadable/overloaded "AsImmutable" property which will return an object of the immutable derived class, either by calling the constructor or (if the object is already of the immutable class) returning itself.

0

精彩评论

暂无评论...
验证码 换一张
取 消