this question has answer here:
- c# class constructor assigning 'this' 2 answers
a kind of weird question, i'm hoping there's out there shorten process or else i'll have many many times each project.
we've made shift toward mvc in our office , we're building "entity classes" of our objects. in order enforce levels of reusability, i've separated core functions of entity database presents particular issue constructors. way i've written dal piece stand-alone static class (to hide outside world) returns objects need ... have copy fields class via constructor , i'm hoping better way.
public class myobj{ public int id { get; set; } public string name { get; set; } public bool isactive { get; set; } public datetime createdate { get; set; } public myobj(int prmid){ list<myobj> raw = dal.get(prmid); if(raw.count != 1) throw new exception("invalid number of objects returned"); id = raw[0].id; name = raw[0].name; isactive = raw[0].isactive; createdate = raw[0].createdate; } static class dal { public static list<myobj> get(int? prmid){ // database stuff call stored procedure , return 0 or more myobjs } } }
where i'm doing copying raw[0], there way of saying "this = raw[0]" ?
i'm doing same structure in number of entities, shortcut appreciated.
you have made architectural mistakes:
the first 1 domain model entity must not know how must saved / loaded. you've broken single responsibility principle.you must have repository or other pattern on them handles persistance logic.
the second mistake speaking you've introduced concrete dependency domain model entity ( dal class seen concrete type in domain model entity). bad habbit , violated inversion of control principle , general spoken "code on abstraction , not on concrete implementations" rule.
Comments
Post a Comment