Instead of declaring your UserID property as Public, make it Private. Alternatively don't implement the "Set" part of the property, which will make it read only.
- R.B. Davidson On Feb 12, 6:35 pm, Adam <[email protected]> wrote: > Hi, > > I may have designed something fundamentally flawed.... > > I have three c# projects - a UI, the Business Logic (BL) and a Data > Access Layer (DAL) > > Some pseudo(ish) code to illustrate > > DAL is: > > class DAL > { > public interface IDALUser > { > string UserId { get; set; } > } > > public void SaveUser(IDALUser myUser) > { > /// Save the user using the properties > /// If UserId is an empty string then generate ID and INSERT > /// otherwise UPDATE > } > > public List<IDALUser> SearchForUser(IDALUser searchUser) > { > /// User properties to generate SQL SELECT statement > /// foreach row in results > /// Clone() the supplied object, set its properties from the > DataTable, add it to the return list > } > > } > > my Business logic (references DAL project) > > class User : IDALUser > { > public string UserId > { > /// get and set > } > > public void Save() > { > DAL.SaveUser(this as IDALUser); > } > > public List<User> User GetById(string userId) > { > List<User> returnList = new List<User>(); > searchUser = new User(); > searchUser.UserId = 1; > > foreach(IDALUser current in DAL.SearchForUser(this as IDALUser) > { > returnList.add(current as User); > } > > return returnList; > > } > > OK, this works. However.... > When some idiot (me) is implementing the UI using the BL, they are > able to set the UserId on the BL object. I would rather this property > were internal as it should only be set by the DAL (it tells me whether > to INSERT or UPDATE). Can I achieve this? Or is this approach just bad > design? > > If you got this far, thanks for your time. > Any comments/suggestions/abuse gratefully received > > Adam
