Answered by:
Repository

Question
-
Hello Everyone,
I find the Repository pattern hard to grasp, maybe someone could explain it to me. I have seen some tutorials. And they say that the repository is a kind of placeholder for data. It would be implemented between the data acces and bussiness layer. So in code is that I see they dont use any LINQ within the repository interface. Is that the idea of repository to make it more simple to get full datatable data?
Could someone tell me what normall the idea would be and what's the difference is with this repository pattern?
Greetings,
SpacelamaEdit: ...Or is the main difference that you dont need to write context.Save with every dataacces code?
public class Repository<T> : IRepository<T> where T : class { public DataContext Context { get; set; } public virtual IQueryable<T> GetAll() { return Context.GetTable<T>(); } public virtual void InsertOnSubmit(T entity) { GetTable().InsertOnSubmit(entity); } public virtual void DeleteOnSubmit(T entity) { GetTable().DeleteOnSubmit(entity); } public virtual void SubmitChanges() { Context.SubmitChanges(); } public virtual ITable GetTable() { return Context.GetTable<T>(); } }
- Edited by SpaceLama Sunday, October 21, 2012 12:51 PM
Sunday, October 21, 2012 12:48 PM
Answers
-
Hi SpaceLama,
Welcome to the MSDN Forum.
According to this documentation: http://msdn.microsoft.com/en-us/library/ff649690.aspx
You should implement the save method and you have done: The repository mediates between the data source layer and the business layers of the application. It queries the data source for the data, maps the data from the data source to a business entity, and persists changes in the business entity to the data source.
And I think your submitchanges is the save method. It makes the same things.
And the advantage of "dont need to write context.Save with every dataacces code" is that your business logic is more focus on the business, rather than it should call which data access layer.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Mike FengModerator Tuesday, October 30, 2012 10:57 AM
Monday, October 22, 2012 11:03 AMModerator
All replies
-
Hi SpaceLama,
Welcome to the MSDN Forum.
According to this documentation: http://msdn.microsoft.com/en-us/library/ff649690.aspx
You should implement the save method and you have done: The repository mediates between the data source layer and the business layers of the application. It queries the data source for the data, maps the data from the data source to a business entity, and persists changes in the business entity to the data source.
And I think your submitchanges is the save method. It makes the same things.
And the advantage of "dont need to write context.Save with every dataacces code" is that your business logic is more focus on the business, rather than it should call which data access layer.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked as answer by Mike FengModerator Tuesday, October 30, 2012 10:57 AM
Monday, October 22, 2012 11:03 AMModerator -
Hello SpaceLame
Here is a good blog post of what is a Repository pattern and how to implement the same in C#.
Thursday, October 25, 2012 9:38 AM