Am I using the uow and repository correctly in my controller classes?

Mar 17, 2014 at 6:27 PM
I have a bootstrap class that is called on initialization, where the DI plumbing is done (using Autofac):
    public static void Bootstrap()
    {
        //
        // setup autofac dependency resolver
        //
        var builder = new ContainerBuilder();

        builder.RegisterControllers(Assembly.GetExecutingAssembly());
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());                        // using extension to register all controllers in an assembly at once
        builder.RegisterType<DBV001Context>().As<IDataContext>().InstancePerHttpRequest();
        //builder.RegisterType<Repository<ContentType>>().As<IRepository<ContentType>>().InstancePerHttpRequest();
        builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerHttpRequest();

In my controller, I do the setup like this:
public class ContentTypeController : ODataBaseController, IContentTypeController
{
    // add repository reference
    private readonly IRepository<ContentType> _repository;
    private readonly IUnitOfWork _uow;

    public ContentTypeController(IUnitOfWork unitOfWork) //, IRepository<ContentType> repository)
    {
        _uow = unitOfWork;
        //_repository = repository;
    }

Now, when I want to do lookups, I'm doing something like this:
        var result = SingleResult.Create(_uow.Repository<ContentType>().Query().Get()
            .Where(c => c.ContentTypeId == key)
            .Select(x => new ContentTypeDTO()
                  {
                      //projection goes here
                      ContentTypeId = x.ContentTypeId,
                      Description = x.Description,
                  }));
And saving:
            // perform mapping from DTO to model object - do not use AutoMapper as it is not thread-safe
            var entity = new ContentType
            {
                ContentTypeId = key,
                UserId = 102, // mock
                Description = contentTypeDTO.Description
            };

            _uow.Repository<ContentType>().Update(entity);
            _uow.Save();
            return Updated(contentTypeDTO);

As you can see from the constructor, I have had some thoughts about the uow/repository., I am using the injected UoW value, but I'm not sure if I'm using the UoW/repository correctly. Somehow I think I'm mixing up the two concepts.

Can you clarify this area for me, and how to properly use the UoW / repository patterns?
Mar 18, 2014 at 9:22 PM
From what I can see here, it looks fine.
Marked as answer by lelong37 on 3/31/2014 at 10:25 PM