c# - How to implement Save in an in-memory repository -


i have following code

foreach (var obj in mylist) {   // bl   repository.add(obj)    // bl   repository.update(obj) } 

i have 2 classes implement irepository. 1 class uses mssql , 1 in-memory. in-memory implementation uses list store data. if use mssql class data stored in db. if use in-memory need insert data repository's list db after foreach loop. i'm not sure best way oop wise.

i can add save method irepository means mssql class implement method doesn't need. option add method in in-memory class , this:

irepository rep = repository inmemoryrepository if (rep != null) {     rep.save() } 

what think?

you should have methods common repository implementations (mssql, in-memory) in base interface.

savetodatabase in-memory specific method , should part of inmemory repository implementation class. not make part of base interface since may not have meaning other concrete implementations.

the other option implement persist method of in-memory repository such saves in list saves in db. (though mixing of concerns)

public interface irepository {  void add(mytype object); }  public class sqlrepository : irepository {  public void add(mytype object)  {   // saves db  } }   public class inmemoryrepository : irepository {  list<mytype> store = new list<mytype>();   public void add(mytype object)  {   // saves list store.   // may also, additionally store db.   irepository dbrep = dependencyinjection.givemedatabaserepository<irepository>();    // null check if tomorrow, there no sql repository, code doesn't break.   if (dbrep != null )   {    dbrep.add(object);   }  }   // explicit option  public void addtodatabase(mytype object)  {    irepository dbrep = dependencyinjection.givemedatabaserepository<irepository>();    dbrep.add(object);  } } 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -