<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>Comments on: Entity Framework POCO (EF4): Generic Repository and Unit of Work Prototype</title>
	<atom:link href="http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/feed/" rel="self" type="application/rss+xml" />
	<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=entity-framework-ef4-generic-repository-and-unit-of-work-prototype</link>
	<description></description>
	<lastBuildDate>Tue, 08 May 2012 09:13:00 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: Mustafa Düman</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64389</link>
		<dc:creator>Mustafa Düman</dc:creator>
		<pubDate>Tue, 01 Nov 2011 19:35:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64389</guid>
		<description>Hello, 

1- 

- i&#039;m looking for &quot;generic repository&quot; and &quot;generic unit of work&quot; interface declerations and concrete implementation of these with EF.

Since i want generic interface, the interfaces must not include anything specific to EF.

Is IObjectSet specific to EF? If so, i think this interface is not generic enough. 

2- I think the unit of work interface having just a &quot;commit&quot; function is not generic at all. It&#039;s true just for your implementation logic...

Just a Commit?  Let&#039;s forget about repository(since it&#039;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..

The best UOW interface i&#039;ve met so for is below. 


If you can implement these with EF then it would be generic..


Interfaces
------------------------------------------------------------------------
public interface IAggregateRoot    
{}


public interface IUnitOfWorkRepository    
{        
     void PersistCreationOf(IAggregateRoot entity);        
     void PersistUpdateOf(IAggregateRoot entity);        
     void PersistDeletionOf(IAggregateRoot entity);    
}

public interface IUnitOfWork
{
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);
     void Commit();
}



------------------------------------------------------------------------Concrete UOW implementation ------------------------------------------------------------------------

public class UnitOfWork : IUnitOfWork     
{
private Dictionary addedEntities;
private Dictionary changedEntities;
private Dictionary deletedEntities;

public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }

------------------------------------------------------------------------



These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: 

http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html)</description>
		<content:encoded><![CDATA[<p>Hello, </p>
<p>1- </p>
<p>- i&#8217;m looking for &#8220;generic repository&#8221; and &#8220;generic unit of work&#8221; interface declerations and concrete implementation of these with EF.</p>
<p>Since i want generic interface, the interfaces must not include anything specific to EF.</p>
<p>Is IObjectSet specific to EF? If so, i think this interface is not generic enough. </p>
<p>2- I think the unit of work interface having just a &#8220;commit&#8221; function is not generic at all. It&#8217;s true just for your implementation logic&#8230;</p>
<p>Just a Commit?  Let&#8217;s forget about repository(since it&#8217;s not mentioned in the interface). Is any concrete class implementing a simple commit function be UOW?  What will it commit???? Not generic or meaningful at all..</p>
<p>The best UOW interface i&#8217;ve met so for is below. </p>
<p>If you can implement these with EF then it would be generic..</p>
<p>Interfaces<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
public interface IAggregateRoot   <br />
{}</p>
<p>public interface IUnitOfWorkRepository   <br />
{       <br />
     void PersistCreationOf(IAggregateRoot entity);       <br />
     void PersistUpdateOf(IAggregateRoot entity);       <br />
     void PersistDeletionOf(IAggregateRoot entity);   <br />
}</p>
<p>public interface IUnitOfWork<br />
{<br />
     void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository);<br />
     void Commit();<br />
}</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;Concrete UOW implementation &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>public class UnitOfWork : IUnitOfWork    <br />
{<br />
private Dictionary addedEntities;<br />
private Dictionary changedEntities;<br />
private Dictionary deletedEntities;</p>
<p>public UnitOfWork()        {            addedEntities = new Dictionary();            changedEntities = new Dictionary();            deletedEntities = new Dictionary();        }        public void RegisterAmended(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!changedEntities.ContainsKey(entity))            {                changedEntities.Add(entity, unitofWorkRepository);            }        }        public void RegisterNew(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!addedEntities.ContainsKey(entity))            {                addedEntities.Add(entity, unitofWorkRepository);            };        }        public void RegisterRemoved(IAggregateRoot entity, IUnitOfWorkRepository unitofWorkRepository)        {            if (!deletedEntities.ContainsKey(entity))            {                deletedEntities.Add(entity, unitofWorkRepository);            }        }                public void Commit()        {            using (TransactionScope scope = new TransactionScope())            {                foreach (IAggregateRoot entity in this.addedEntities.Keys)                {                    this.addedEntities[entity].PersistCreationOf(entity);                }                foreach (IAggregateRoot entity in this.changedEntities.Keys)                {                    this.changedEntities[entity].PersistUpdateOf(entity);                }                foreach (IAggregateRoot entity in this.deletedEntities.Keys)                {                    this.deletedEntities[entity].PersistDeletionOf(entity);                }                scope.Complete();             }        }    }</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>These interfaces are from a book. Samples are freely available. The interfaces are in chapter 7 samples: </p>
<p><a href="http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html" rel="nofollow">http://www.wrox.com/WileyCDA/WroxTitle/Professional-ASP-NET-Design-Patterns.productCd-0470292784,descCd-DOWNLOAD.html</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Warren LaFrance</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64231</link>
		<dc:creator>Warren LaFrance</dc:creator>
		<pubDate>Mon, 29 Aug 2011 17:41:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64231</guid>
		<description>Did you ever get an answer to your question?</description>
		<content:encoded><![CDATA[<p>Did you ever get an answer to your question?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Efekaptan</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64176</link>
		<dc:creator>Efekaptan</dc:creator>
		<pubDate>Thu, 11 Aug 2011 00:38:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64176</guid>
		<description>Hi, you can also add an (frequently used) order by function 

public IEnumerable GetAllOrderBy(Func keySelector) {
            return _objectSet.OrderBy(keySelector).ToList();
 }</description>
		<content:encoded><![CDATA[<p>Hi, you can also add an (frequently used) order by function </p>
<p>public IEnumerable GetAllOrderBy(Func keySelector) {<br />
            return _objectSet.OrderBy(keySelector).ToList();<br />
 }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gavin Luo</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-64123</link>
		<dc:creator>Gavin Luo</dc:creator>
		<pubDate>Wed, 27 Jul 2011 01:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-64123</guid>
		<description>??????</description>
		<content:encoded><![CDATA[<p>??????</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Big D</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63887</link>
		<dc:creator>Big D</dc:creator>
		<pubDate>Fri, 10 Jun 2011 06:50:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63887</guid>
		<description>Hi

You mentioned that you are not keeping a static reference to unit of work...what if the user is work across multiple screens and getting and updating data within those screens?

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>You mentioned that you are not keeping a static reference to unit of work&#8230;what if the user is work across multiple screens and getting and updating data within those screens?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: zambak zambak</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63875</link>
		<dc:creator>zambak zambak</dc:creator>
		<pubDate>Tue, 07 Jun 2011 00:08:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63875</guid>
		<description>Hi

Since Entity Framework 4.1 there is allready IObjectContextAdapter...Could we have used 

    public class UnitOfWork : IUnitOfWork, IDisposable
    {
        private readonly ObjectContext _objectContext;
 
        public UnitOfWork(IObjectContextAdapter objectContextAdapter)
        {
            _objectContext = objectContextAdapter.ObjectContext;
        }        //ommited for brevity    }

and

    public class Repository : IRepository where T : class
    {
        IObjectSet _objectSet;
 
        public Repository(IObjectContextAdapter objectContextAdapter)
        {
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();
        }

        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)
 
            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)
var newTeam = new Team { Name = &quot;Da Bears&quot; };Thoughts?ThanksZ....

</description>
		<content:encoded><![CDATA[<p>Hi</p>
<p>Since Entity Framework 4.1 there is allready IObjectContextAdapter&#8230;Could we have used </p>
<p>    public class UnitOfWork : IUnitOfWork, IDisposable<br />
    {<br />
        private readonly ObjectContext _objectContext;</p>
<p>        public UnitOfWork(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectContext = objectContextAdapter.ObjectContext;<br />
        }        //ommited for brevity    }</p>
<p>and</p>
<p>    public class Repository : IRepository where T : class<br />
    {<br />
        IObjectSet _objectSet;</p>
<p>        public Repository(IObjectContextAdapter objectContextAdapter)<br />
        {<br />
            _objectSet = objectContextAdapter.ObjectContext.CreateObjectSet();<br />
        }</p>
<p>        //ommited for brevity    }then usage becomes             var db = new MyEfCodeFirstContext();  // create context (could be auto wired I guess)</p>
<p>            var uow = new UnitOfWork(db);    // unit of workvar teamRepository = new Repository(db);  // no adapter needed (EF provides one)<br />
var newTeam = new Team { Name = &#8220;Da Bears&#8221; };Thoughts?ThanksZ&#8230;.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bass</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63861</link>
		<dc:creator>Bass</dc:creator>
		<pubDate>Wed, 01 Jun 2011 14:37:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63861</guid>
		<description>Hi , I have a really silly question, 
since I am newbie to, I wonder about this line : 
&quot;var context = builder.Create(connection);&quot; 
Is that just to instantiate the context ? 
what is the type/class of &quot;builder&quot; ? 
Is it a EntityConnectionStringBuilder Class as described in MSDN. 
What I have seen in almost all tutorials till now is : 
&quot;var context = new MyModel()&quot; 
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. 

Or may be they are different approaches and I am totally going in wrong direction.  

Thanks for the explanation

</description>
		<content:encoded><![CDATA[<p>Hi , I have a really silly question, <br />
since I am newbie to, I wonder about this line : <br />
&#8220;var context = builder.Create(connection);&#8221; <br />
Is that just to instantiate the context ? <br />
what is the type/class of &#8220;builder&#8221; ? <br />
Is it a EntityConnectionStringBuilder Class as described in MSDN. <br />
What I have seen in almost all tutorials till now is : <br />
&#8220;var context = new MyModel()&#8221; <br />
May be this is equivalent and the difference is that in the 2nd case   the model is often referenced from another ClassLib project. </p>
<p>Or may be they are different approaches and I am totally going in wrong direction.  </p>
<p>Thanks for the explanation</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cibrax</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-3/#comment-63802</link>
		<dc:creator>Cibrax</dc:creator>
		<pubDate>Wed, 27 Apr 2011 14:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63802</guid>
		<description>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments. </description>
		<content:encoded><![CDATA[<p>A common problem with methods that receive an Expression tree like Find, Single or First in your example is that they can not easily be mocked in an unit test with most of common Mocking frameworks out there. I can tell this from experience with previous developments.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jarod Ferguson</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63752</link>
		<dc:creator>Jarod Ferguson</dc:creator>
		<pubDate>Sat, 26 Mar 2011 20:59:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63752</guid>
		<description>@Yeah CreateObjectSet doesn&#039;t load data</description>
		<content:encoded><![CDATA[<p>@Yeah CreateObjectSet doesn&#8217;t load data</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Yeah</title>
		<link>http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/comment-page-2/#comment-63744</link>
		<dc:creator>Yeah</dc:creator>
		<pubDate>Tue, 22 Mar 2011 18:42:00 +0000</pubDate>
		<guid isPermaLink="false">http://elegantcode.com/2009/12/15/entity-framework-ef4-generic-repository-and-unit-of-work-prototype/#comment-63744</guid>
		<description>why are you calling CreateObjectSet on every context so you can use it? this is a terrible design. So if I want to work with an POCO mapped to a table with 5 million rows, you are going to load all those rows into memory every time?</description>
		<content:encoded><![CDATA[<p>why are you calling CreateObjectSet on every context so you can use it? this is a terrible design. So if I want to work with an POCO mapped to a table with 5 million rows, you are going to load all those rows into memory every time?</p>
]]></content:encoded>
	</item>
</channel>
</rss>

