Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I've been working on our application (CQRS and DDD) for awhile now. The application architecture is well layered and thought through.

However we are having difficulties in decide where to put the below code, in such a way that it makes sense from an architectural point of view.

What the code does: It's a generic method which executes a query against the dataset, specified by the generic parameter which inherits a particular interface. Note: we are not using repositories, since the query is, as the name indicates, for our read layer.

The interface, used by the generic method:

public interface IOrganisationalDbEntry
{
    string OrganisationLevelId { get; set; }
    OrganisationLevelDbEntry OrganisationLevel { get; set; }
}

The generic method, which lives inside our Entity Framework DbContext:

public IList<TDbEntry> GetByOrganisationLevel<TDbEntry>(Guid? organisationLevelId,
    Func<TDbEntry, bool> filter = null)
    where TDbEntry : class, IOrganisationalDbEntry
{
    var organisationLevel = organisationLevelId.HasValue ? organisationLevelId.Value.ToString() : null;

    var query = filter == null ? Set<TDbEntry>() : Set<TDbEntry>().Where(filter);

    IList<TDbEntry> result = query.Where(x => x.OrganisationLevelId == organisationLevel).ToList();

    // If there are no results for the current organisation level
    // => recursive check for entries for parent organisation level
    if (!result.Any() && !string.IsNullOrWhiteSpace(organisationLevel))
    {
        // Get parentOrganisationLevel
        var parentOrganisationLevelId = Set<OrganisationLevelDbEntry>()
            .Where(x => x.Id == organisationLevel)
            .Select(x => x.ParentOrganisationLevelId)
            .SingleOrDefault();

        return
            GetByOrganisationLevel<TDbEntry>(
                string.IsNullOrWhiteSpace(parentOrganisationLevelId)
                    ? (Guid?)null
                    : new Guid(parentOrganisationLevelId), filter);
    }

    return result;
}

As you may be thinking, we have several EF Entities inheriting the IOrganisationalDbEntry. What comes to mind first, is a base Repository. But We do not want to use repositories in our query side, since we want this layer to be as flat as possible.

The next thing that comes to mind is a specific query and queryhandler for this query. Tho this is not acceptable since this method accepts a Func parameter to be able to add extra filter criteria while calling this method, to make it more flexible.

At this moment, this method is living as method on our IDbContext and DbContext implementation. However we are not satisfied with this. We are wondering whether there is a specific pattern or approach for our case. Imagine we have more of these methods, I do not want to keep expanding our context. Since this is not what the DbContext should be used for.

share|improve this question
    
I'm also not sure this a great fit here. It's bordering on a design question. I would recommend Programmers, but I'm not sure it would be well received there. It seems too narrow for their scope. We can certainly review the code you've posted though. –  RubberDuck Aug 19 '14 at 12:22
2  
It's narrow, but on the other side it's a pretty heavy question imo. Ok, I can just create a static helper and problem fixed. But we all know we dont want that to happen. Reviewing the code is always welcome, even tho we're happy with it's current state (of the code it self, not where it should be placed) –  Frederik P. Aug 19 '14 at 12:44
    
I'm refraining from casting a close vote since you're okay with having the code reviewed. Suggestions on where the code should be placed may or may not come, but we'll never know if the question gets closed. –  RubberDuck Aug 19 '14 at 12:47
    
Ofcourse I'm ok with code review! But if this question get's closed, I hope i get any feedback on where to put this question, if even programmers isn't a fit. I'm pretty sure I'm not gonna get the desired answer on Stackoverlow itself. :( –  Frederik P. Aug 19 '14 at 12:56
    
Moved my answer to a comment, wasn't important enough for an answer. Use IEnumerable<> instead of IList<> since you probably won't add/remove items of the result of the query –  TopinFrassi Aug 19 '14 at 14:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.