Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

Difference between UnitOfWork and Repository Pattern

Repository can work without Unit Of Work, so it can also have Save method.

public interface IRepository<T>
{
     T Get(int id);
     void Add(T entity);
     void Update(T entity);
     void Remove(T entity);
     void Save();
}
Unit Of Work is used when you have multiple repositories (may have different data context). It keeps track of all changes in a transaction until you call Commit method to persist all changes to database(file in this case).

So, when you call Add/Update/Remove in the Repository, it only changes the status of the entity, mark it as Added, Removed or Dirty... When you call Commit, Unit Of Work will loop through repositories and perform actual persistence:

If repositories share the same data context, the Unit Of Work can work directly with the data context for higher performance(open and write file in this case).

If repositories have different data context(different databases or files), the Unit Of Work will call each repository's Save method in a same TransactionScope.
Comment

PREVIOUS NEXT
Code Example
Csharp :: unity slider decimal 0.01 
Csharp :: Rotate Object with keyboard 
Csharp :: how to input data several times in c# 
Csharp :: return every digit on a string c# 
Csharp :: jtoken value is not exact double 
Csharp :: how to add the ssl certificate in vb.net application 
Csharp :: reflection assemblies gettypes 
Csharp :: Propertychanged is not firing up when text is change 
Csharp :: how to run a console app in another app c# 
Csharp :: linq conditionnally add where clause 
Csharp :: C# remain space 
Csharp :: auto refresh gridview c# 
Csharp :: replace update claims c# 
Csharp :: c# class reference 
Csharp :: conveyor function in f# 
Csharp :: nullable IList 
Csharp :: .net SaveChanges vs update difference 
Csharp :: C# sprint key 
Csharp :: Open Windows Explorer to a certain directory from within a WPF app 
Csharp :: c# check if object can be cast to type 
Csharp :: c# .net calculate md5 
Csharp :: unity mass unit 
Csharp :: changing color of material of renderer with multiple materias 
Csharp :: game creator change local variable 
Csharp :: unity remove component 
Csharp :: how to not overwrite a text file in c# 
Csharp :: c# web scraping get images from specific url 
Csharp :: make sprite invisible unity 
Csharp :: Task w = Task.Delay(600);w.Wait();new Program().Start(); 
Csharp :: C# webclient immitate browser 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =