.net - C# unit of work repository pattern -
i'm trying use repository pattern unit of work. use c# , entity framework , purpose use reporitory connect different data sources single controller, can't implement it.
this scenario:
i have 2 different data source:
sqlserver entity framework read , write data
web service retrieve data.
i use unit of work access 2 data sources interchangeably.
this repository:
class genericrepository<t> : irepository<t> t : class { private dbentities entities = null; iobjectset<t> _objectset; public genericrepository(dbentities _entities) { entities = _entities; _objectset = entities.createobjectset<t>(); } public ienumerable<t> getall(string uri, string ragneuri) { ienumerable<t> t = new list<t>(); return t.asenumerable(); } public ienumerable<t> getall(func<t, bool> predicate = null) { if (predicate != null) { return _objectset.where(predicate); } return _objectset.asenumerable(); } public t get(func<t, bool> predicate) { return _objectset.first(predicate); } public void add(t entity) { _objectset.addobject(entity); } public void attach(t entity) { _objectset.attach(entity); } public void delete(t entity) { _objectset.deleteobject(entity); } }
and it's unit of work
public class genericunitofwork : idisposable { private dbentities entities = null; private dataactions da = null; public genericunitofwork(dbentities pe) { entities = pe; } public dictionary<type, object> repositories = new dictionary<type, object>(); public irepositoryipse<t> repository<t>() t : class { if (repositories.keys.contains(typeof(t)) == true) { return repositories[typeof(t)] irepository<t>; } irepository<t> repo = new genericrepository<t>(entities); repositories.add(typeof(t), repo); return repo; } public void savechanges() { entities.savechanges(); } private bool disposed = false; protected virtual void dispose(bool disposing) { if (!this.disposed) { if (disposing) { entities.dispose(); } } this.disposed = true; } public void dispose() { dispose(true); gc.suppressfinalize(this); } }
now succeed connect @ data base, don't connect web service.
to access web service create second repository single method getdata(), don't know how integrate unit of work , use different repository interchangeably. there different way resolve problem?
thank's
Comments
Post a Comment