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 264987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:43:53+00:00 2026-05-11T22:43:53+00:00

So I’m building an App using the Entity Framework on top of SQL Compact

  • 0

So I’m building an App using the Entity Framework on top of SQL Compact Edition. I didn’t like the idea of using the Entites as my business objects so I’ve been building a layer (I’ve called it ObjectModel layer, prob not the best terminology) in-between that will take my plain objects, use them to populate and save the entities. And also vice versa – take entites, populate the POCOs for use as business object


Let’s say I have a POCO object called Customer,

public class Customer:ICustomer
    {


        #region ICustomer Members

        public System.Guid id {get;set;}


        public string Forename {get;set;}


        public string Surname { get; set; }


        #endregion
    }

And I want to populate this using my ObjectModel layer. I currently do this…

OM_Customer<ICustomer,Entity.Customer> customerLayer = new OM_Agent<ICustomer,Entity.Customer>();

ICustomer businessObject = customerLayer.GetById(new Guid("4a75d5a5-6f5a-464f-b4b3-e7807806f1a9"));

The OM_Customer class inherits from ObjectModelBase, which is below..

public abstract class ObjectModelBase<BllClass, EntityClass>
{
    public DataStoreEntities1 db;

    public EntityClass _dataObject;

    public string setName;


    #region POCO-ORM Mapping Functions
    public EntityClass MapBLLToEntity(BllClass bllObject)
    {
        Mapper.CreateMap<BllClass, EntityClass>();
        return Mapper.Map<BllClass, EntityClass>(bllObject);
    }

    public BllClass MapEntityToBLL(EntityClass entityObject)
    {
        Mapper.CreateMap<EntityClass, BllClass>();
        return Mapper.Map<EntityClass, BllClass>(entityObject);
    }
    #endregion

    public void Save(BllClass toAdd)
    {
        _dataObject = MapBLLToEntity(toAdd);

        using (db = new DataStoreEntities1())
        {
            db.AddObject(setName, _dataObject);
            db.SaveChanges();
        }
    }

    public BllClass GetById(Guid id)
    {
        using (db = new DataStoreEntities1())
        {
            EntityClass result = (EntityClass)((object)(from c in  db.CustomerSet  where c.id == id select c).First());
            return MapEntityToBLL(result);
        }

    }

}

The problem here lies with the GetById method. The Save method can be used by subclasses fine as all Entity Framework Object Contexts have the AddObject() method.

However, I wish to have a generic getById method, as all my entities will have a property called id (in the db as well, being the primary key).The problem is that the linq here…

ModelClass result = (ModelClass)((object)(from c in  db.CustomerSet  where c.id == id select c).First());

is specific to db.CustomerSet. I wish to have this abstract class provide the generic CRUD/Paging/’Collection Retrieval’ for all my entities, but without the repetitive keystrokes, however, this simple GetById method has driven me to ask for help.

Current ideas :

  • have my get by Id use Entity SQL where i’d have more control over building the query string.
  • scrap the entity framework and linq as it may be too time consuming to abstract fully.

Would appreciate any adivce as to how the SO community would go about solving the above.

  • 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-05-11T22:43:53+00:00Added an answer on May 11, 2026 at 10:43 pm

    First, I would start by questioning why you feel that using EF entities as you business entities would necessarily be a bad thing? I’ve done so on a number of projects and I have yet to run into any serious issues that I can’t overcome.

    Having said that, if you really feel you have a strong reason to separate the two, here’s one option.

    You can use a delegate to inject the specific entity set object to query and another for the selector for the key – it could allows derivatives to only specify their storage semantics. So your code would look like:

    public abstract class ObjectModelBase<BllClass, EntityClass>
    {
        // ... same basic code here...
    
        // supplied by your derivatives...
        protected abstract Func<IQueryable<EntityClass>> GetEntitySet { get; };
        protected abstract Func<Guid,Func<EntityClass,bool>> KeySelector { get; }
    
        public BllClass GetById(Guid id)
        {
            using (db = new DataStoreEntities1())
            {
                EntityClass result = (EntityClass)((object)
                     GetEntitySet()
                         .Where( KeySelector( id ) )
                         .First();
                return MapEntityToBLL(result);
            }
        }
    }
    
    public class OM_Customer : ObjectModelBase<ICustomer,Entity.Customer>
    {
        protected abstract Func<IQueryable<Entity.Customer>> GetEntitySet
        { 
            get { return db.CustomerSet; }
        }
    
        protected abstract Func<Guid,Func<Entity.Customer,bool>> KeySelector
        {
            get { return (g => (e => e.Id == g)); }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 122k
  • Answers 122k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I wrote a "philosophy" of leading software development teams in… May 12, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer Options: PHP Inline Diff (uses PEAR text-diff) PEAR Text_Diff Diff… May 12, 2026 at 12:49 am
  • Editorial Team
    Editorial Team added an answer There are many ways to do this, but given your… May 12, 2026 at 12:49 am

Related Questions

So I'm getting a new job working with databases (Microsoft SQL Server to be
So I have a Sybase stored proc that takes 1 parameter that's a comma
So I'm embarking on an ASP.NET MVC project and while the experience has been
So I've got a JPanel implementing MouseListener and MouseMotionListener : import javax.swing.*; import java.awt.*;
So I wrote some perl that would parse results returned from the Amazon Web

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.