Currently, I am building a typical 3-tier web app with ASP.NET MVC. I have set it up with dependency injection (Autofac) like below:
public class UserController : BaseController
{
private readonly IUserService _userService;
public UserController(IUserService userService)
{
this._userService = userService;
}
}
public class IUserService
{
void InsertUser(User user);
void UpdateUser(User user);
void DeleteUser(User user);
}
public class UserService : IUserService
{
private readonly IRepository<User> _userRepository;
public UserService(IRepository<User> userRepository)
{
this._userRepository = userRepository;
}
public void InsertUser(User user)
{
_userRepository.Insert(user);
}
public void UpdateUser(User user)
{
_userRepository.Update(user);
}
public void DeleteUser(User user)
{
_userRepository.Delete(user);
}
}
Repository is a typical generic repository using EF.
public interface IRepository<T> where T : BaseEntity
{
void Insert(T entity);
void Update(T entity);
void Delete(T entity);
}
The problem is my app has a lot of entities, and for each entity I have to replicate the above code for CRUD operations in service layer. For example: With entity "Role", I have "InsertRole", "UpdateRole", "DeleteRole"... and many more for other entities. So I try to refactor to remove duplicate code by extracting CRUD operations to a STATIC CLASS "CommonService" with STATIC METHODs like below:
public static class CommonService
{
public static void Insert<T>(T entity) where T : BaseEntity
{
var repository = EngineContext.Current.Resolve<IRepository<T>>();
repository.Insert(entity);
}
public static void Update<T>(T entity) where T : BaseEntity
{
var repository = EngineContext.Current.Resolve<IRepository<T>>();
repository.Update(entity);
}
public static void Delete<T>(T entity) where T : BaseEntity
{
var repository = EngineContext.Current.Resolve<IRepository<T>>();
repository.Delete(entity);
}
}
With this class, I will remove duplicate code in service for CRUD operations. In Controller, I just call CommonService.Insert(user);... It's really good with me now. I still have another service methods as normal and no duplication for CRUD. But I'm wondering if there's any downside with this approach except unit testing (I won't unit test for CRUD). Is there any problem with memory management and concurrency handling in web environment (ASP.NET MVC)? I haven't implemented concurrency mechanism for data handling with EF yet (simultaneous update an entity...)
Thanks in advance! MillDol.