views:

62

answers:

1

Hi,

I am using LINQ to SQL for the DataAccess layer. I have similar business objects to what is in the data access layer.

I have got the dataprovider getting the message #23. On instantiation of the message, in the message constructor, it gets the MessageType and makes a new instance of MessageType class and fills in the MessageType information from the database.

Therefore; I want this to get the Name of the MessageType of the Message.

user.Messages[23].MessageType.Name

I also want an administrator to set the MessageType

user.Messages[23].MessageType = MessageTypes.LoadType(3);

but I don't want the user to publicly set the MessageType.Name. But when I make a new MessageType instance, the access modifier for the Name property is public because I want to set that from an external class (my data access layer).

I could change this to property to internal, so that my class can access it like a public variable, and not allow my other application access to modify it.

This still doesn't feel right as it seems like a public property. Are public access modifiers in this situation bad? Any tips or suggestions would be appreciated.

Thanks.

A: 

You can always define your property like that:

public MessageType MessageType
{
    get { return this._messageType; }
    internal set { this._messageType = value; }
}

If that is what you're looking for, but it's a bit unclear what you want. What is an "administrator" who is allowed to set that property? If that means the property should be set only in code by the same module or a "friend" module, then that's the solution.

Another way would be to publicly expose only an interface IMessage with only a getter á la MessageType MessageType { get; } for "public" modules. In your core modules you would have the full Message class where you also have a setter.

If you mean by "administrator" that it depends on the current user's role if the property can be set (in a GUI for instance), then there is no simple solution that can be solved by a simple property setter. You will need to make it public. Then you will have to code up some fancy rights management which you will use to evaluate if that part of the GUI will be enabled and which you will use again to validate before you save the value to your data store.

herzmeister der welten