HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Identity repository pattern with unit of work

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
identitywithrepositoryworkpatternunit

Problem

I am creating simple web api/ SPA application using EntityFramework, IUnitOfWork, Repository pattern, Unity DI along with Asp.net Identity.

Unity configuration

public static void RegisterTypes(IUnityContainer container)
        {
            // TODO: Register your types here
            container.RegisterType(new PerResolveLifetimeManager());
            container.RegisterType(new PerResolveLifetimeManager());
            container.RegisterType, UserRepository>(new HierarchicalLifetimeManager());
            container.RegisterType(new HierarchicalLifetimeManager());
            container.RegisterType, RoleRepository>(new HierarchicalLifetimeManager());
            container.RegisterType(new HierarchicalLifetimeManager());
            container.RegisterType(new HierarchicalLifetimeManager());
            container.RegisterType(new HierarchicalLifetimeManager());
        }


UnitOfWork

public class UnitOfWork : IUnitOfWork
    {
        protected DataContext _context = null;

        public UnitOfWork(DataContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("Context argument cannot be null in UnitOfWork.");
            }

        this._context = context;
    }

    public void SaveChanges()
    {
        this._context.SaveChanges();
    }

    public async Task SaveChangesAsync()
    {
        await this._context.SaveChangesAsync();
    }

    public void Dispose()
    {
        if (this._context != null)
        {
            this._context.Dispose();
        }
    }
}


GenericRepository

```
public abstract class GenericRepository : IGenericRepository, IDisposable
where T : class
{
protected DataContext _context;
protected readonly IDbSet _dbset;

public GenericRepository(DataContext context)
{
if (context == null)
{
throw new ApplicationException("DbContext cannot be null.");
}

Solution

Looks pretty good, I will try this approach on my next project.

Just one question:
Why do you use UnitOfWork AND Services?

You should as well be able to use only UnitOfWork and use all the Getters and Setters from there, or not?

Is this structure helpful when mocking?

When looking at the graphic here:

from: http://www.asp.net/mvc/overview/older-versions/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

It seems a little overhead, does it not?

Context

StackExchange Code Review Q#85741, answer score: 3

Revisions (0)

No revisions yet.