I am reading the following tutorial about creating Generic Repository Link in asp.net mvc & EF, to perform CRUD operations, as follow:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using ContosoUniversity.Models;
using System.Linq.Expressions;
namespace ContosoUniversity.DAL
{
public class GenericRepository<TEntity> where TEntity : class
{
internal SchoolContext context;
internal DbSet<TEntity> dbSet;
public GenericRepository(SchoolContext context)
{
this.context = context;
this.dbSet = context.Set<TEntity>();
}
public virtual IEnumerable<TEntity> Get(
Expression<Func<TEntity, bool>> filter = null,
Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
string includeProperties = "")
{
IQueryable<TEntity> query = dbSet;
if (filter != null)
{
query = query.Where(filter);
}
foreach (var includeProperty in includeProperties.Split
(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
{
query = query.Include(includeProperty);
}
if (orderBy != null)
{
return orderBy(query).ToList();
}
else
{
return query.ToList();
}
}
public virtual TEntity GetByID(object id)
{
return dbSet.Find(id);
}
public virtual void Insert(TEntity entity)
{
dbSet.Add(entity);
}
}
}
but in real web project Add/Edit/Delete/Find operations might not be very simple and standard, as different entities might require different operations inside these methods.
Currently inside my Asset Management asp.net mvc web application, for example I have a repository method to add a new Server entity , where I do the following main operations:-
- Create a general Technology object and automatically generates a Tag number.
- Create a new audit record.
- Create a new Server.
- etc
so i find it hard to create Generic repository for almost all my current repository methods, because each entity have different requirements when performing CRUD operations.
so my questions is basically , when developing web applications that have different and complex requirements is having a Generic Repository a tasks that can be achieved ? , from my point of view having a Generic Repository will work only if the requirements are simple and standard.
OR i should style my Repository and action method to work with Generic repository , and failing to do so might be a problem ?
Can anyone advice on this please ?
Thanks