asp.net mvc - repository pattern in asp. net mvc3 -
i have started teaching myself c# , asp.net. trying build simple blog application. confused repository pattern usage. have seen few tutorials , implementation varies.
for blog application, have 2 database tables (models) - blogs , comments. currently, have idbcontext looks this:
public interface idbcontext { iqueryable<blog> findallblogs(); iqueryable<blog> findblogsinmonth(int month); blog getblog(int id); void add(blog blog); void update(blog blog); void delete(blog blog); void add(comment comment); //void remove(comment comment); }
and have repository looks this:
public class blogrepository : idbcontext { private blogdb db = new blogdb(); public iqueryable<blog> findallblogs() { return db.blogs.orderbydescending(b => b.publishdate); } public blog getblog(int id) { return db.blogs.single(b => b.blogid == id); } ... }
the other implementation of repository pattern this:
public interface idbcontext { iqueryable<blog> blogs { get; } iqueryable<comments> comments { get; } int savechanges(); t attach<t>(t entity) t : class; t add<t>(t entity) t : class; t delete<t>(t entity) t : class; }
which calls repository , there separate class queries.
what best method it?
the easiest method use entity framework directly, particularly new dbcontext functionality showed in entity framework 4.1 , later.
the context contain dbset properties - each implementation of repository pattern accomplishes of goals described above. idbset can used if unit testing support required.
i've seen lot of demos asp.net mvc repository patterns online end wrapping entity framework custom repository. it's waste of time - it's code wraps other code , doesn't serve direct purpose aside adding needless complexity.
Comments
Post a Comment