Quantcast
Channel: URF - Unit of Work & (extensible/generic) Repositories Framework
Viewing all articles
Browse latest Browse all 1539

Updated Wiki: ASP.NET MVC 5 Controller Example with Async

$
0
0
Basic sample of using the framework in an ASP.NET MVC 5 Controller. In real world this would most like be injected with an IService which would facilitate business logic and injected with necessary Repositories.

Use your choice of a DI & IoC framework, although DI & IoC are not required, it is highly recommended.

Northwind.Web.App_Start.UnityConfig.cs

Using Unity for DI & IoC

Northwind.Web.App_Start.UnityConfig.cs

publicstaticvoid RegisterTypes(IUnityContainer container)
{
    container
        .RegisterType<IDataContextAsync, NorthwindContext>(new PerRequestLifetimeManager())
        .RegisterType<IUnitOfWorkAsync, UnitOfWork>(new PerRequestLifetimeManager())
        .RegisterType<IRepositoryAsync<Customer>, Repository<Customer>>();
}

Northwind.Api.CustomerController.cs

publicclass CustomerController : ODataController
{
    privatereadonly IRepositoryAsync<Customer> _customerRepository;
    privatereadonly IUnitOfWorkAsync _unitOfWorkAsync;

    public CustomerController(
        IUnitOfWorkAsync unitOfWorkAsync,
        IRepositoryAsync<Customer> customerRepository)
    {
        _unitOfWorkAsync = unitOfWorkAsync;
        _customerRepository = customerRepository;
    }

    // GET odata/Customerpublic IQueryable<Customer> GetCustomer()
    {
        return _customerService.ODataQueryable();
    }

    // GET odata/Customer(5)
    [Queryable]
    public SingleResult<Customer> GetCustomer(string key)
    {
        return SingleResult.Create(
            _customerRepository
                .Query(customer => customer.CustomerID == key)
                .Select()
                .AsQueryable());
    }

    // PUT odata/Customer(5)public async Task<IHttpActionResult> Put(string key, Customer customer)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (key != customer.CustomerID)
        {
            return BadRequest();
        }

        customer.ObjectState = ObjectState.Modified;
        _customerRepository.Update(customer);

        try
        {
            await _unitOfWorkAsync.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!CustomerExists(key))
            {
                return NotFound();
            }
            throw;
        }

        return Updated(customer);
    }

    // POST odata/Customerpublic async Task<IHttpActionResult> Post(Customer customer)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        _customerRepository.Insert(customer);

        try
        {
            await _unitOfWorkAsync.SaveChangesAsync();
        }
        catch (DbUpdateException)
        {
            if (CustomerExists(customer.CustomerID))
            {
                return Conflict();
            }
            throw;
        }

        return Created(customer);
    }

    // PATCH odata/Customer(5)
    [AcceptVerbs("PATCH", "MERGE")]
    public async Task<IHttpActionResult> Patch(string key, Delta<Customer> patch)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        var customer = await _customerRepository.FindAsync(key);

        if (customer == null)
        {
            return NotFound();
        }

        patch.Patch(customer);

        _customerRepository.Update(customer);

        try
        {
            customer.ObjectState = ObjectState.Modified;
            await _unitOfWorkAsync.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!CustomerExists(key))
            {
                return NotFound();
            }
            throw;
        }

        return Updated(customer);
    }

    // DELETE odata/Customer(5)public async Task<IHttpActionResult> Delete(string key)
    {
        var customer = await _customerRepository.FindAsync(key);
        if (customer == null)
        {
            return NotFound();
        }

        _customerRepository.Delete(customer);
        await _unitOfWorkAsync.SaveChangesAsync();

        return StatusCode(HttpStatusCode.NoContent);
    }

    // GET odata/Customer(5)/CustomerDemographics
    [Queryable]
    public IQueryable<CustomerDemographic> GetCustomerDemographics(string key)
    {
        return _customerRepository.Query(m => m.CustomerID == key)
            .Select()
            .AsQueryable()
            .SelectMany(m => m.CustomerDemographics);
    }

    // GET odata/Customer(5)/Orders
    [Queryable]
    public IQueryable<Order> GetOrders(string key)
    {
        return _customerRepository
            .Query(m => m.CustomerID == key)
            .Select()
            .AsQueryable()
            .SelectMany(m => m.Orders);
    }

    protectedoverridevoid Dispose(bool disposing)
    {
        if (disposing)
        {
            _unitOfWorkAsync.Dispose();
        }
        base.Dispose(disposing);
    }

    privatebool CustomerExists(string key)
    {
        return _customerRepository.Query(e => e.CustomerID == key).Select().Any();
    }
}

Viewing all articles
Browse latest Browse all 1539

Trending Articles