I want to implement generic repository pattern in an efficient way. How to implement? Below is my code
namespace FXRates_EF.Base
{
public interface IRepository<T> : IDisposable where T : class
{
List<T> GetAll();
T GetById(int id);
List<T> SearchFor(Expression<Func<T, bool>> predicate);
void Insert(T entity);
void Delete(T entity);
void Update(T entity);
}
}
namespace FXRates_EF.Base
{
public class DataRepository<T> : IRepository<T> where T : class
{
public List<T> GetAll()
{
List<T> List = new List<T>();
try
{
using(xrateEntities _entity = new xrateEntities())
{
List = _entity.Set<T>().ToList();
}
}
catch(Exception ex)
{
}
return List;
}
public T GetById(int id)
{
throw new NotImplementedException();
}
public List<T> SearchFor(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
throw new NotImplementedException();
}
public void Insert(T entity)
{
throw new NotImplementedException();
}
public void Delete(T entity)
{
throw new NotImplementedException();
}
public void Update(T entity)
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
}
Below is my business layer
namespace FXRates_Business.RepositoryManager
{
public class CurrencyDefRepository : IRepository<CurrencyDef>
{
public List<CurrencyDef> GetAll()
{
List<CurrencyDef> lst = new List<CurrencyDef>();
// How to fetch data from CurrencyDef table using generic repository
return lst;
}
public CurrencyDef GetById(int id)
{
CurrencyDef objCurDef = new CurrencyDef();
return objCurDef;
}
public List<CurrencyDef> SearchFor(System.Linq.Expressions.Expression<Func<CurrencyDef, bool>> predicate)
{
List<CurrencyDef> lst = new List<CurrencyDef>();
return lst;
}
public void Insert(CurrencyDef entity)
{
}
public void Delete(CurrencyDef entity)
{
}
public void Update(CurrencyDef entity)
{
}
public void Dispose()
{
}
}
A while ago I created a small project that I use to explain dependency injection to developers in which I make use of the Unit of Work and a Generic Repository. You can download the project via:
https://1drv.ms/u/s!AmiC8xvp5PorgQkVZCpFXCdCQtlw
Maybe it helps you to get on your way.
Regards,
Yorrick
Live life loosely coupled and separated of concerns
Member
14 Points
133 Posts
How to implement generic repository in asp.net?
Nov 20, 2016 11:38 AM|Athar Ali Khan|LINK
Hi
I want to implement generic repository pattern in an efficient way. How to implement? Below is my code
Below is my business layer
Thanks
All-Star
194434 Points
28074 Posts
Moderator
Re: How to implement generic repository in asp.net?
Nov 21, 2016 07:48 AM|Mikesdotnetting|LINK
These articles show how:
Participant
1433 Points
359 Posts
Re: How to implement generic repository in asp.net?
Nov 21, 2016 12:27 PM|Yorrick vd Voort|LINK
Hi,
A while ago I created a small project that I use to explain dependency injection to developers in which I make use of the Unit of Work and a Generic Repository. You can download the project via: https://1drv.ms/u/s!AmiC8xvp5PorgQkVZCpFXCdCQtlw
Maybe it helps you to get on your way.
Regards,
Yorrick
Contributor
4790 Points
1211 Posts
Re: How to implement generic repository in asp.net?
Nov 25, 2016 02:26 PM|ketan_al|LINK
Hi,
Please refer following for generic repository example and demonstration
http://shashangka.com/2015/11/27/mvc5-crud-using-generic-repository-pattern/
http://cpratt.co/truly-generic-repository/
http://www.ketscode.com
MCP, MCTS,MCPD (Microsoft Azure Developer)
Please mark as answer if it helps