views:

77

answers:

2

If I want to run something like this

BLL.Person person = (BLL.Person)repository.Single(item => item.Id == Id);

Down in my single method I'd do something like this:

public Resource Single(Expression<Func<BLL.Resource, bool>> where)
{
     Resource resource = AsQueryable().FirstOrDefault(where);
     return resource;
}

protected IQueryable<BLL.Resource> AsQueryable()
{
     // I need to use the where clause on an object called DAL.Resource
     throw new NotImplementedException();
}

The DAL.Resource object is identical to the BLL.Resource, however the BLL copy is ignorant of persistence.. I can map things using automapper no problem to return a collection of what I want, however I need the where clause to run agaisnt DAL not BLL...

This must be possible somehow! Any ideas would be appreciated.

A: 

This may be way off base, but this is how I do it. In my BLL objects, I pass a DAL object in in the constructor. In my services, I get the DAL object, and create a new BLL object, and pass the DAL object in. The BLL object then maps out the DAL object.

using PersonDto = DAL.IPerson;

namespace BLL
{
    public class Person : IPerson
    {

        private readonly PersonDto _person;

        public Person(PersonDto person)
        {
            _person = person;
        }

        public string Name
        {
            get { return _person.Name; }
        }
    }
}

Then do this with LINQ:

BLL.Person person = new Person(repository.Single(item => item.Id == Id));

Probably a little confusing, let me know if I am way off base, or you need more explanation.

Martin
Hi, I can't implement an interface for these two with the current plan! This is mapping two objects linq where clauses together, is that what yours does?
Shahin
My solution is using a where clause on the DAL, then mapping the DAL object to a Business Object. Are you saying you want to do `BLL.Person.Where(x => x.Id == id)` and have the where look in the DAL object and not the BLL object?
Martin
Yes that is what I'm trying to do... I was going to answer with yes but it made me enter more characters!
Shahin
A: 

Your signature for the where-clause (Expression<Func<BLL.Resource, bool>> where) is a very good step along the way to the answer. Adding Expression<...> tells the C# compiler to create an expression tree rather than a delegate for the where parameter. The solution is to traverse the expression tree and replace all references to your BLL.Person with references to DAL.Person. Since they are identical, as you say, then you should be able to compile and run the modified expression tree against the DAL without any problems.

Enigmativity
Hi, thanks for your response I was beginning to lose all hope in this. I'll look into expression trees and get back to you, if you have any references or an example I'd be grateful.
Shahin
@`Shahin` - Try looking at the class `System.Linq.Expressions.ExpressionVisitor` in `System.Core`. It is an abstract class for rewriting expressions. Using Reflector.NET I could find three derived implementations to hopefully give you an idea how to use it. You'll need to use Reflector to dump the source because it is internal. Good luck!
Enigmativity