Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Repository MVC4 EF5.0
Hi,
I am implementing Repository Pattern in one of my project based on ASP.NET MVC4 and N-Tier Architecture. I am little confused as how to implement Custom Membership with Repository Pattern. Here are how I have implemented my classes so far. For posting purpose all < are replaced with [ and > are replaced with ]
 
Generic Repository
 
public interface IRepository[T] where T : class
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
IQueryable[T] GetAll();
T FindBy(Expression[Func[T, bool]] expression);
IQueryable[T] FilterBy(Expression[Func[T, bool]] expression);
}
 
Repository Base Class
 
public abstract class Repository[T] : IRepository[T] where T : class
{
private STNDataContext _stnDataContext;
private readonly IDbSet[T] _dbSet;
protected Repository(IDatabaseFactory databaseFactory)
{
DatabaseFactory = databaseFactory;
_dbSet = StnDataContext.Set[T]();
}
protected IDatabaseFactory DatabaseFactory { get; private set; }
public STNDataContext StnDataContext
{
get { return _stnDataContext ?? (_stnDataContext = new STNDataContext()); }
}
public void Add(T entity)
{
_dbSet.Add(entity);
_stnDataContext.Commit();
}
public void Delete(T entity)
{
_dbSet.Remove(entity);
}
public void Update(T entity)
{
_dbSet.Attach(entity);
_stnDataContext.Entry(entity).State = EntityState.Modified;
_stnDataContext.Commit();
}
public IQueryable[T] GetAll()
{
return _dbSet.ToList().AsQueryable();
}
public T FindBy(Expression[Func[T, bool]] expression)
{
return FilterBy(expression).SingleOrDefault();
}
public IQueryable[T] FilterBy(Expression[Func[T, bool]] expression)
{
return GetAll().Where(expression).AsQueryable();
}
 
User Repository Interface
 
public interface IUserRepository : IRepository[User]
{
}
 
User Repository
 
public class UserRepository : Repository[User], IUserRepository
{
public UserRepository(IDatabaseFactory databaseFactory)
: base(databaseFactory)
{
}
}
 
Heres my Business Logic Layer
Generic Service Interface


 
public interface IService[T]
{
void Add(T entity);
void Delete(T entity);
void Update(T entity);
IEnumerable[T] GetAll();
T FindBy(Expression[Func[T, bool]] expression);
IEnumerable[T] FilterBy(Expression[Func[T, bool]] expression);
}
 
Service Base
 
public class Service[T] : IService[T] where T : class
{
public void Add(T entity)
{
throw new NotImplementedException();
}
public void Delete(T entity)
{
throw new NotImplementedException();
}
public void Update(T entity)
{
throw new NotImplementedException();
}
public IEnumerable[T] GetAll()
{
throw new NotImplementedException();
}
public T FindBy(Expression[Func[T, bool]] expression)
{
throw new NotImplementedException();
}
public IEnumerable[T] FilterBy(Expression[Func[T, bool]] expression)
{
throw new NotImplementedException();
}
}
 
User Service Interface
 
public interface IUserService : IService[User]
{
}
 
User Service Implementation
 
public class UserService : Service[User], IUserService
{
private readonly IUserRepository _userRepository;
private readonly IRoleRepository _roleRepository;
public UserService(IUserRepository userRepository, IRoleRepository roleRepository)
{
_userRepository = userRepository;
_roleRepository = roleRepository;
}
public IList[User] GetAllUsers()
{
return _userRepository.GetAll().ToList();
}
public User GetUser(int id)
{
return _userRepository.FindBy(x =] x.UserId == id);
}
public User GetUser(string userName)
{
return _userRepository.FindBy(x =] x.Username.Equals(userName));
}
public void CreatUser(User user)
{
_userRepository.Add(user);
}
public IList[User] GetUsersForRole(string roleName)
{
return _userRepository.FilterBy(x =] x.Role.RoleName.Equals(roleName)).ToList[User]();
}
public IList[User] GetUsersForRole(int roleId)
{
return _userRepository.FilterBy(x =] x.Role.RoleId == roleId).ToList[User]();
}
public IList[User] GetUsersForRole(Role role)
{
return GetUsersForRole(role.RoleId);
}
}
 
I am not very sure on Am I going the right way? If yes how do i implement my UserService class.
If no, what changes do i need to implement.
Any help on this is highly appreciable. Thanks in advance
Posted 28-Oct-12 21:56pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100