Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:12:44+00:00 2026-06-06T18:12:44+00:00

I have a nice clean domain layer in my app that was developed in

  • 0

I have a nice clean domain layer in my app that was developed in a DDD fashion. The database was not considered at all when developing the domain. Property names make sense, aren’t in ALL CAPS, and are relevant to my application.

Today, I am implementing a repository to pull from an existing EF DbContext. The DbContext was developed to (basically) match a poorly-designed Oracle database.

Ideally, I would like to implement a repository like this:

public interface IRepository {
    IQueryable<T> Find<T>(Expression<Func<T, bool>> query) where T : IMyDomainEntity;
}

T is my domain entity. But, inside my Find method in my repository, I have to…

  1. Somehow convert the expression to work with the DbContext

    I am not sure how to do this yet.

  2. Query the DbContext

    Once the expression is ‘mapped’, this is simple

  3. Somehow map to my domain object

    I’m sure I can use AutoMapper or implement my own mapper.

  4. Return an IQueryable having not made a trip to the database yet.

    Not sure this is possible after all the meddling done in #’s 1 – 3

So, how has this problem been solved in the past? Are there any reusable patterns here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:12:47+00:00Added an answer on June 6, 2026 at 6:12 pm

    Well, you’re on the right track already, just implement what your say you want 🙂

    1.You’re passing an expression into your find method so, just use that expression in your Where clause

    2.You just need to get the correct DbSet from your DbContext to query against, DbContext has a method to get the DbContext of a given type, use that and you can query like

    public IQueryable<T> Find<T>(Expression<Func<T, bool>> query) where T : IMyDomainEntity
    {
       var dbSet = context.Set<T>();
       return dbSet.Where(query);
    }
    

    3.If your domain objects are not the ones mapped by EF to the database, you’ll need to customize your mapping against what’s in your DB in your DbContext class (no need for automapper for that), so you would have something like this in your DbContext class

    public class MyContext : DbContext 
    {
       ...
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<User>()
                    .Map(a => a.ToTable("DB_USERS"))
                    .Property(a => a.Email).HasColumnName("MAIL");
    
                base.OnModelCreating(modelBuilder);
            }
    }
    

    To map from the table DB_USERS in the DB to the class User, having different names for the fields, etc. here’s an article on that

    http://www.codeproject.com/Articles/165720/Using-the-Code-First-Model-Configuration-Classes

    You could also map the properties to the correct table columns using attributes if you don’t want/can’t change your DbContext class

    http://msdn.microsoft.com/en-us/data/gg193958

    Or you can have a different set of entities that are mapped to your DB and use automapper to translate them into your domain objects, but you lose no. 4 bellos since you’ll need to materialize the query to automap it to your domain model.

    4.No need to do anything special, EF takes care of the that

    UPDATE: Solution without having access to the DbContext (not fully generic version but works)

    The idea is to create the mapping part of the repository for each domain class, so all gets binded correctly. Continueing with the User domain model and DBUser table model:

    public class User : IDomainModelEntity
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Email { get; set; }
    }
    
    public class DBUser
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int USER_ID { get; set; }
    
        [Required]
        [MaxLength(150)]
        public string USER_NAME { get; set; }
    
        [Required]
        [MaxLength(260)]
        public string USER_MAIL { get; set; }
    }
    

    Then you would have an abstract Repository and an a concrete repository per domain class that implements the basic GetAll query mapped:

    public abstract class Repository<T>  where T : IDomainModelEntity
    {
        protected readonly DbContext _context;
    
        public Repository(DbContext context)
        {
            _context = context;
        }
    
        public abstract IQueryable<T> GetAll();
    
        public IQueryable<T> Find(Expression<Func<T, bool>> predicate)
        {
            return GetAll().Where(predicate);
        }
    
    }
    
    public class UserRepository : Repository<User>
    {
        public UserRepository(DbContext context)
            : base(context)
        {
        }
    
        public override IQueryable<User> GetAll()
        {
            return _context.Set<DBUser>()
                .Select(u => new User
                    {
                        Id = u.USER_ID,
                        Name = u.USER_NAME,
                        Email = u.USER_MAIL
                    });
        }
    }
    

    now to use it you will just call the find or get all on the repository…

            using (var context = new CompanyDbContext())
            {
                var repo = new UserRepository(context);
                var list = repo.Find(a=>a.Id >= 2).ToList();
    
                list.ForEach(a => Console.WriteLine("Id: {0}, Name {1}, email {2}", a.Id, a.Name, a.Email));
            }
    

    It is not fully generic since you will need to pass a repository for each domain class you need to use, but it may be an acceptable compromise

    Hope this helps

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a nice clean standards-compliant codebase that I can compile with GCC's -ansi
I would like to have a nice clean LINQ code that can get an
I have a nice page that does everything I need. However one of the
I have this nice little MSBuild-based daily build setup that I use on my
I have a nice clean UI within a table view which has a few
In order to have nice clean urls with some kind of site archetecture I
I have two simple programs set up that share data through a unix domain
I want to replace some headers by images, so I would have nice font.For
have a nice day. I got problem when trying to create an image from
Have a nice day everyone, I have something to ask your hel, to better

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.