locked
BLL - DAL : eliminate repositories? RRS feed

  • Question

  • User1196679051 posted

    I have read Entity Framework uses repositories so we don't need to create our repositories. But how can I eliminate repositories if I don't want to duplicate code and I want to have readable code?

    Now I have the DAL project with repositories, for example:

    namespace Blog.DAL.Repository
    {
        public class CommentsRepository : ICommentsRepository
        {
            private DatabaseContext _context;
    
            public CommentsRepository(DatabaseContext context)
            {
                _context = context;
            }
    
            public IEnumerable<Comment> GetComments(bool isSpam)
            {
                return _context.Comments.Where(x => x.IsSpam == isSpam).OrderByDescending(x => x.CreatedDate).ToList(); // it can be much more complex query
            }
        }
    }

    I have also the BLL project with services, for example:

    namespace Blog.BLL.Services
    {
        public class CommentsService : ICommentsService
        {
            private ICommentsRepository _commentsRepository;
            
            public CommentsService(ICommentsRepository commentsRepository)
            {
                _commentsRepository = commentsRepository;
            }
            
            public IEnumerable<Comment> DoSomethingWithComments()
            {
                IEnumerable<Comment> comments = _commentsRepository.GetComments(true);
                
                // ......
    
                return comments;
            }        
        }
    }

    namespace Blog.BLL.Services
    {
        public class NotesService : INotesService
        {
            private INotesRepository _notesRepository;
            private ICommentsRepository _commentsRepository;
    
            public NotesService(INotesRepository notesRepository, ICommentsRepository commentsRepository)
            {
                _notesRepository = notesRepository;
                _commentsRepository = commentsRepository;
            }
            
            public IEnumerable<Note> DoSomethingWithNotes()
            {
                IEnumerable<Note> notes = _notesRepository.GetNotes();
                IEnumerable<Comment> comments = _commentsRepository.GetComments(true);
    
                // ......
    
                return notes;
            }        
        }
    }

    Without repositories I don't need to have the DAL project (STRANGE - only the BLL project?) and I have code duplication:

    namespace Blog.BLL.Services
    {
        public class CommentsService : ICommentsService
        {
            private DatabaseContext _context;
    
            public CommentsService(DatabaseContext context)
            {
                _context = context;
            }
            
            public IEnumerable<Comment> DoSomethingWithComments()
            {
                IEnumerable<Comment> comments = _context.Comments.Where(x => x.IsSpam == true).OrderByDescending(x => x.CreatedDate).ToList(); // code duplication
                
                // ......
    
                return comments;
            }        
        }
    }

    namespace Blog.BLL.Services
    {
        public class NotesService : INotesService
        {
            private DatabaseContext _context;
    
            public NotesService(DatabaseContext context)
            {
                _context = context;
            }
    
            public IEnumerable<Note> DoSomethingWithNotes()
            {
                IEnumerable<Note> notes = _context.Notes.OrderBy(x => x.Title).ToList();
                IEnumerable<Comment> comments = _context.Comments.Where(x => x.IsSpam == true).OrderByDescending(x => x.CreatedDate).ToList(); // code duplication
    
                // ......
    
                return notes;
            }
        }
    }

    So maybe repositories are good solution?

    Tuesday, June 9, 2015 5:03 AM

All replies

  • User-1611549905 posted

    There are a couple of alternatives to the Repository wrapper that you can use to eliminate code duplication.

    For simpler queries, such as GetComments(bool isSpam) here, you can define an extension method on DbSet<Comment>.

    For more complex queries (more than about three parameters or so), use a query object. This could have properties such as IsSpam, StartDate, EndDate and so on, and a method, BuildQuery() which takes a DbSet<Comment> and returns an IQueryable<Comment>.

    The key point to remember is that Entity Framework itself is your data access layer. Anything you layer on top of Entity Framework, even if it is the Repository-as-a-query-container, is first and foremost business logic, not data access logic.

    Thursday, June 11, 2015 12:15 PM