Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Bridging ObjectDataSource to Entity Framework with a Generic BusinessObject

Bridging ObjectDataSource to Entity Framework with a Generic BusinessObject

Scheduled Pinned Locked Moved The Weird and The Wonderful
csharpdatabaselinqbusinessannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    Woppelmann P
    wrote on last edited by
    #1

    I am/we are moving from our 6 years old home grown entity framework towards .NET 4.0 Entity Framework and LINQ to Entities - I like it! At a first glance it looked like I had to manually code LINQ queries into every Business-Object, even for standard operations (CRUD). Let me show you a tiny generic BusinessObject, that fits perfectly into ObjectDataSource:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.ComponentModel;
    using System.Data.Objects.DataClasses;
    using System.Data.Objects;
    using System.Data;
    using System.Reflection;

    namespace DataObjectDemo {

    [DataObject]
    public abstract class BasicWebBO
    where TEntity : EntityObject,new()
    where TContext : ObjectContext, new() {

    private TContext _context;
    protected TContext Context {
    get {if (_context == null) {
    _context = new TContext();
    }
    return _context;
    }
    }

    protected virtual string EntitySetName {
    get {
    return typeof(TEntity).Name;
    }
    }

    [DataObjectMethod(DataObjectMethodType.Insert)]
    public virtual void Insert(TEntity entity) {
    Context.AddObject(typeof(TEntity).Name,entity);
    Context.SaveChanges();
    }

    [DataObjectMethod(DataObjectMethodType.Update)]
    public virtual void Update(TEntity entity) {
    Context.AttachTo(EntitySetName, entity);
    Context.ObjectStateManager.
    ChangeObjectState(
    entity, System.Data.EntityState.Modified);
    Context.ApplyCurrentValues(
    entity.EntityKey.EntitySetName, entity);
    Context.SaveChanges();
    }

    [DataObjectMethod(DataObjectMethodType.Delete)]
    public virtual void Delete(TEntity entity) {
    Context.AttachTo(EntitySetName, entity);
    Context.DeleteObject(entity);
    Context.SaveChanges();
    }

    [DataObjectMethod(DataObjectMethodType.Select)]
    public virtual TEntity Select(TKey key) {
    //HACK: assume a scalar key and the key-property on index 0
    TEntity template = new TEntity();
    PropertyInfo pi = typeof(TEntity).GetProperties()[0];
    pi.SetValue(template, key, null);
    //
    EntityKey entityKey =
    Context.CreateEntityKey(EntitySetName, template);
    object entity;
    if (Context.TryGetObjectByKey(entityKey, out entity)) {
    return entity as TEntity;
    }
    return null;
    }

    Now you can easily derive a BusinessObject, eg. PersonBO:

    public class PersonBO : BasicWebBO

    _ 1 Reply Last reply
    0
    • W Woppelmann P

      I am/we are moving from our 6 years old home grown entity framework towards .NET 4.0 Entity Framework and LINQ to Entities - I like it! At a first glance it looked like I had to manually code LINQ queries into every Business-Object, even for standard operations (CRUD). Let me show you a tiny generic BusinessObject, that fits perfectly into ObjectDataSource:

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Web;
      using System.ComponentModel;
      using System.Data.Objects.DataClasses;
      using System.Data.Objects;
      using System.Data;
      using System.Reflection;

      namespace DataObjectDemo {

      [DataObject]
      public abstract class BasicWebBO
      where TEntity : EntityObject,new()
      where TContext : ObjectContext, new() {

      private TContext _context;
      protected TContext Context {
      get {if (_context == null) {
      _context = new TContext();
      }
      return _context;
      }
      }

      protected virtual string EntitySetName {
      get {
      return typeof(TEntity).Name;
      }
      }

      [DataObjectMethod(DataObjectMethodType.Insert)]
      public virtual void Insert(TEntity entity) {
      Context.AddObject(typeof(TEntity).Name,entity);
      Context.SaveChanges();
      }

      [DataObjectMethod(DataObjectMethodType.Update)]
      public virtual void Update(TEntity entity) {
      Context.AttachTo(EntitySetName, entity);
      Context.ObjectStateManager.
      ChangeObjectState(
      entity, System.Data.EntityState.Modified);
      Context.ApplyCurrentValues(
      entity.EntityKey.EntitySetName, entity);
      Context.SaveChanges();
      }

      [DataObjectMethod(DataObjectMethodType.Delete)]
      public virtual void Delete(TEntity entity) {
      Context.AttachTo(EntitySetName, entity);
      Context.DeleteObject(entity);
      Context.SaveChanges();
      }

      [DataObjectMethod(DataObjectMethodType.Select)]
      public virtual TEntity Select(TKey key) {
      //HACK: assume a scalar key and the key-property on index 0
      TEntity template = new TEntity();
      PropertyInfo pi = typeof(TEntity).GetProperties()[0];
      pi.SetValue(template, key, null);
      //
      EntityKey entityKey =
      Context.CreateEntityKey(EntitySetName, template);
      object entity;
      if (Context.TryGetObjectByKey(entityKey, out entity)) {
      return entity as TEntity;
      }
      return null;
      }

      Now you can easily derive a BusinessObject, eg. PersonBO:

      public class PersonBO : BasicWebBO

      _ Offline
      _ Offline
      _Amy
      wrote on last edited by
      #2

      Hi,

      Woppelmann P wrote:

      Now you can easily derive a BusinessObject

      This idea of deriving business object is fantastic. You should use this idea as a new article. The user's will come to know about this. This is not the right place of this kind of ideas. You can refer Here[^] for articles and tips.

      Warm Regards. --Amit

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups