Bridging ObjectDataSource to Entity Framework with a Generic BusinessObject
-
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
-
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
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