patterncsharpModerate
Entity framework with repository and Unit Of Work pattern and POCO architecture
Viewed 0 times
architecturewithrepositoryunitworkandpocoframeworkpatternentity
Problem
This is my architecture to EF4 using repository pattern and unit of work pattern with POCO. I believe I made some mistakes.
I have a solution with 5 projects:
In addition, I am using
Now I will describe what each project contains - with source:
Repository interface:
Unit of work interface:
Unit of work factory interface:
Unit of work:
```
public static class UnitOfWork
{
private const string HTTPCONTEXTKEY = "MyApp.Common.HttpContext.Key";
private static IUnitOfWorkFactory _unitOfWorkFactory;
private static readonly Hashtable _threads = new Hashtable();
public static void Commit()
{
IUnitOfWork unitOfWork = GetUnitOfWork();
if (unitOfWork != null)
{
unitOfWork.Commit();
}
}
public static IUnitOfWork Current
{
get
{
IUnitOfWork unitOfWork = GetUnitOfWork();
if (unitOfWork == null)
{
_unitOfWorkFacto
I have a solution with 5 projects:
MyApp.Common
MyApp.Data.EF4
MyApp.Domain.Company
MyApp.Domain.Transmission
MyApp.Web
In addition, I am using
StructureMap in this application in order to inject the data layer.Now I will describe what each project contains - with source:
MyApp.Common - contains common classes and interfacesRepository interface:
public interface IRepository where T : class
{
IQueryable GetQuery(IEnumerable>> includes);
IPaged GetQuery(IQueryable query,
Func, IOrderedQueryable> orderBy, int pageNumber, int pageSize);
IPaged GetQuery(IEnumerable query,
Func, IOrderedEnumerable> orderBy, int pageNumber, int pageSize);
IEnumerable GetObjectStateManagerChanges();
void Insert(T entity);
void MarkModified(T entity);
void Delete(T entity);
void Attach(T entity);
void Detach(T entity);
T GetOriginalEntity(Func predicate);
}Unit of work interface:
public interface IUnitOfWork : IDisposable
{
int Commit();
}Unit of work factory interface:
public interface IUnitOfWorkFactory
{
IUnitOfWork Create();
}Unit of work:
```
public static class UnitOfWork
{
private const string HTTPCONTEXTKEY = "MyApp.Common.HttpContext.Key";
private static IUnitOfWorkFactory _unitOfWorkFactory;
private static readonly Hashtable _threads = new Hashtable();
public static void Commit()
{
IUnitOfWork unitOfWork = GetUnitOfWork();
if (unitOfWork != null)
{
unitOfWork.Commit();
}
}
public static IUnitOfWork Current
{
get
{
IUnitOfWork unitOfWork = GetUnitOfWork();
if (unitOfWork == null)
{
_unitOfWorkFacto
Solution
Without understanding too much of your scenario, here are a few pointers:
I'm not that familiar with Entity Framework, but a common interface for UoW usually have the following behaviors:
Where is the error and logging handling? Below is an example of a simple wrapper you can use.
And change the name of the Common-project to something less common..;)
I'm not that familiar with Entity Framework, but a common interface for UoW usually have the following behaviors:
public interface UnitOfWork : IDisposable {
bool IsInTransaction { get; }
bool IsDirty { get; } // Same as your MarkModified()
void BeginTransaction();
void Commit();
void Rollback();
}Where is the error and logging handling? Below is an example of a simple wrapper you can use.
// Example call
ExceptionHandler.Try("Could not load Vechicle", () => _vehicleRepository.Insert(vehicle));
// Example class
public class ExceptionHandler
public void Try(string errorMessage, Action func)
{
try
{
func();
}
catch (Exception e)
{
Handle(e, errorMessage); // handle and log exception
}
}And change the name of the Common-project to something less common..;)
Code Snippets
public interface UnitOfWork : IDisposable {
bool IsInTransaction { get; }
bool IsDirty { get; } // Same as your MarkModified()
void BeginTransaction();
void Commit();
void Rollback();
}// Example call
ExceptionHandler.Try("Could not load Vechicle", () => _vehicleRepository.Insert(vehicle));
// Example class
public class ExceptionHandler
public void Try(string errorMessage, Action func)
{
try
{
func();
}
catch (Exception e)
{
Handle(e, errorMessage); // handle and log exception
}
}Context
StackExchange Code Review Q#5556, answer score: 13
Revisions (0)
No revisions yet.