linq - Why my DataContext doesn't see the changes to an object(Unit Of Work)? -


i'm trying set project use unitofwork , repository pattern.

now can't use ioc , ef4, i'm trying linq , datacontext bit of dependency :(. don't hide i'm bit confused integration of these concepts. noticed debugging code datacontext doesn't see updates made object, every time adds new entity database.

i have read lot about, can't find problem, maybe it's simple step. before proceed, here's have:

for example have object called foo...i have foo controller in constructor creates new instance of foorepository. in foorepository add reference unitofwork wraps datacontext...is right? here's code

public class listacontroller : controller {      ilistarepository _listarepository;             public listacontroller()         : this(new listarepository()) {     }      public listacontroller(ilistarepository repository)     {         _listarepository = repository;     }      [httppost]     public actionresult edit(int id, lista lista)     {         try         {             this._listarepository.save(lista);             return redirecttoaction("index");          }         catch         {             return view();         }     } }     public class listarepository : linqrepository<lista>, ilistarepository     {      protected iunitofwork uow     {         { return base.getcurrentunitofwork<iunitofwork>(); }     }      public listarepository()         : base()     {     }      public override void add(lista lista)     {         this.uow.context.listas.insertonsubmit(lista);         this.uow.commit();     }      public override void save(lista lista)     {         add(lista);     }    }     public static class unitofwork    {     private const string httpcontextkey = "domain.httpcontext.key";      private static iunitofworkfactory _unitofworkfactory;     private static readonly hashtable _threads = new hashtable();      public static void commit()     {         iunitofwork unitofwork = getunitofwork();         if (unitofwork != null)         {             unitofwork.commit();         }     }      public static iunitofwork current     {                 {             iunitofwork unitofwork = getunitofwork();             if (unitofwork == null)             {                 //qui inserisco dipendenza in quanto non uso un ioc                 //_unitofworkfactory = objectfactory.getinstance<iunitofworkfactory>();                 _unitofworkfactory = new linqunitofworkfactory();                 unitofwork = _unitofworkfactory.create();                 saveunitofwork(unitofwork);             }             return unitofwork;         }     }      private static iunitofwork getunitofwork()     {         if (httpcontext.current != null)         {             if (httpcontext.current.items.contains(httpcontextkey))             {                 return (iunitofwork)httpcontext.current.items[httpcontextkey];             }             return null;         }         else         {             thread thread = thread.currentthread;             if (string.isnullorempty(thread.name))             {                 thread.name = guid.newguid().tostring();                 return null;             }             else             {                 lock (_threads.syncroot)                 {                     return (iunitofwork)_threads[thread.currentthread.name];                 }             }         }     }      private static void saveunitofwork(iunitofwork unitofwork)     {         if (httpcontext.current != null)         {             httpcontext.current.items[httpcontextkey] = unitofwork;         }         else         {             lock (_threads.syncroot)             {                 _threads[thread.currentthread.name] = unitofwork;             }         }     }   }  public abstract class linqrepository<t> : irepository<t> t : class {     protected manageremaildatacontext _context = new manageremaildatacontext();      protected manageremaildatacontext context     {                 {             if (_context == null)             {                 _context = getcurrentunitofwork<linqunitofwork>().context;             }             return _context;         }     }      public tunitofwork getcurrentunitofwork<tunitofwork>() tunitofwork : iunitofwork     {         return (tunitofwork)unitofwork.current;     }       public abstract iqueryable<t> getall();     public abstract void add(t entity);     public abstract void save(t entity);     public abstract t get(int id); }  protected void application_start() {     arearegistration.registerallareas();     registerglobalfilters(globalfilters.filters);     registerroutes(routetable.routes);     linqunitofworkfactory.setdatacontext(() => new manageremaildatacontext()); }  public class linqunitofworkfactory : manageremail.models.iunitofworkfactory {     private static func<manageremaildatacontext> _objectcontextdelegate;     private static readonly object _lockobject = new object();      public static void setdatacontext(func<manageremaildatacontext> objectdatacontextdelegate)     {         _objectcontextdelegate = objectdatacontextdelegate;     }      public iunitofwork create()     {         manageremaildatacontext context;         lock (_lockobject)         {             context = _objectcontextdelegate();         }         return new linqunitofwork(context);     } }`enter code here` 

any or suggest appreciated!

sorry if posted code, it's week i'm going crazy thing.

the problem related linq. in controller call save(t entity) method , in repository have

public override void save(lista lista) {    lista original = cloneentity<lista>(this.get(lista.id));    this._uow = new linqunitofworkfactory().create(); //i must renew datacontext    this._uow.context.listas.attach(lista, original);    this._uow.context.refresh(refreshmode.keepchanges, lista);    this._uow.commit(); }  internal static t cloneentity<t>(t originalentity) {    type entitytype = typeof(t);    datacontractserializer ser = new datacontractserializer(entitytype);    using (memorystream ms = new memorystream())    {        ser.writeobject(ms, originalentity);        ms.position = 0;        return (t)ser.readobject(ms);    } } 

hope helps someone.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

windows - Python Service Installation - "Could not find PythonClass entry" -

Determine if a XmlNode is empty or null in C#? -