C# – How should I design the object model so that the DAL can populate read-only fields

asp.netcnet

In order to separate concerns, on my current project, I've decided to completely separate my DAL and BLL/Business objects in separate assemblies. I would like to keep my business objects as simple structures without any logic to keep things extremely simple. I would like if I could keep my Business Logic separate from my DAL also. So my application will tell my DAL to load my objects, my DAL will run off to the database and get the data, populate the object with the data and then pass it back to my BLL.

Question – how can I have my DAL in a separate assembly and push data into the read only fields?

  • If I set the getter as protected then inherited objects can access it which isn't really what I want as I'd be returning the inherited object types, not the original object types.
  • If I set the getter as internal, then my DAL must reside in the same assembly as my BLL which I don't want.
  • If I set the getter as public, then anyone can read/write to it when it should be read only.

Edit: I note that I can have a return type of ObjectBase but actually be returning an object or collection of objects that are derived form ObjectBase so to the outside world (outside my DAL) the properties would be read-only, but my derived types (only accessible inside my DAL) the properties are actually read/write.

Best Answer

You can set the read only property via a constructor.

Related Topic