c# - How to structure two classes that "intersect" and output certain values (Mediator design pattern)? -
i have 2 classes: user
, site
. user
, site
have many-to-many relationship. site
object s has property indicating whether or not user
u should validated before u added s. validate user
, application retrieves validation rules site
, checks see user
's properties' values match validation rules' values. if match/pass, user
"valid" added site
.
how structure relationship? first thought create intermediate class (i.e. mediator design pattern?) has field of type ienumerable<user>
, site
can encapsulate site
validation setting retrieval. also, i'm thinking of adding "isvalid" property user
class. need? want make sure things not tightly coupled.
thoughts?
here's similar code wrote up:
public class user { public int _userid; public string _fname; public string _lname; public user(string connectionstring, int id) { using (var dc = new datacontext(connectionstring)) { var user = dc.users.singleordefault(u => u.id == id); _userid = user.id; _fname = user.fname; _lname = user.lname; } } public bool isvaliduser(int siteid) { bool isvalid = true; // logic here won't change var conditions = site.getconditions(_userid); // e.g. checks _fname, _lname return site.usermeetsconditions(_id, conditions); } } public class site { public int _siteid; public list<setting> _sitesettings; public site(string connectionstring, int id) { using (var dc = new datacontext(connectionstring)) { var site = dc.sites.singleordefault(u => u.id == id); _siteid = site.id; } } public void setsitesettings(string connectionstring) { using (var dc = new datacontext(connectionstring)) { _sitesettings = dc.sitesettings.select(s => s).tolist(); } } public bool siterequiresvaliditycheck() { return _sitesettings.any(s => s.settingid = 100 && s.value == true); } } public validator { public list<user> users; public bool _requiresvaliditycheck; public bool usersarevalid(int siteid) { bool usersarevalid = true; if (_requiresvaliditycheck) { foreach (var user in users) { if (!user.isvalid) { usersarevalid = false; break; } } } return usersarevalid; } } static void main() { string cs = myconnectionstring; var user1 = new user(cs, 1); var user2 = new user(cs, 2); list<user> users = new list<user>() { user1, user2 }; var site = new site(cs, 10); site.setsitesettings(cs); var validator = new validator(); validator.users = users; validator._requiresvaliditycheck = site.siterequiresvaliditycheck(); bool usersarevalid = validator.usersarevalid(site._siteid); }
if poco class or model use dataannotations on class attributes. user
[required]
etc attributes each attribute. data annotations has support cross attribute checking can implement too. take @ them here: https://msdn.microsoft.com/en-us/library/dd901590%28vs.95%29.aspx
Comments
Post a Comment