Using Repository in ASP.NET 4.5.1 Webform

Nov 20, 2014 at 1:40 AM
Hi,

I downloaded the source and sample project and I added a new webform project with nuget package Unity.Webforms.

This is my code for UnityConfig:
using System.Web;
using Microsoft.Practices.Unity;
using Notrhwind.Webforms;
using Unity.WebForms;
using Northwind.Entities.Models;
using Northwind.Service;
using Repository.Pattern.DataContext;
using Repository.Pattern.Ef6;
using Repository.Pattern.Ef6.Factories;
using Repository.Pattern.Repositories;
using Repository.Pattern.UnitOfWork;


[assembly: WebActivator.PostApplicationStartMethod( typeof(UnityWebFormsStart), "PostStart" )]
namespace Notrhwind.Webforms
{
    internal static class UnityWebFormsStart
    {
        internal static void PostStart()
        {
            IUnityContainer container = new UnityContainer();
            HttpContext.Current.Application.SetContainer( container );

            RegisterDependencies( container );
        }

        private static void RegisterDependencies( IUnityContainer container )
        {
            container
             .RegisterType<IDataContextAsync, NorthwindContext>(new HierarchicalLifetimeManager())
             .RegisterType<IRepositoryProvider, RepositoryProvider>(
                 new HierarchicalLifetimeManager(),
                 new InjectionConstructor(new object[] { new RepositoryFactories() })
                 )
             .RegisterType<IUnitOfWorkAsync, UnitOfWork>(new HierarchicalLifetimeManager())
             .RegisterType<IRepositoryAsync<Customer>, Repository<Customer>>()
             .RegisterType<IRepositoryAsync<Product>, Repository<Product>>()
             .RegisterType<IProductService, ProductService>()
             .RegisterType<ICustomerService, CustomerService>()
             .RegisterType<INorthwindStoredProcedures, NorthwindContext>(new HierarchicalLifetimeManager())
             .RegisterType<IStoredProcedureService, StoredProcedureService>();
      }
    }
}
This is my Default.aspx.cs code:
using System;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Threading.Tasks;
using System.Web.UI;
using Microsoft.Practices.Unity;
using Northwind.Entities.Models;
using Northwind.Service;
using Repository.Pattern.Infrastructure;
using Repository.Pattern.UnitOfWork;

namespace Notrhwind.Webforms
{
   public partial class _Default : Page
   {
      [Dependency]
      private ICustomerService CustomerService { get; set; }

      [Dependency]
      private IUnitOfWorkAsync UnitOfWork { get; set; }

      protected void Page_Load(object sender, EventArgs e)
      {
         var customers = GetCustomers();
      }

      private IQueryable<Customer> GetCustomers()
      {
         return CustomerService.Queryable();
      }

      public async Task<bool> Post(Customer customer)
      {

         customer.ObjectState = ObjectState.Added;
         CustomerService.Insert(customer);

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

         return true;
      }

      private bool CustomerExists(string key)
      {
         return CustomerService.Query(e => e.CustomerID == key).Select().Any();
      }
   }
}
I have some errors:
  • The property CustomerService on type Northwind.Webforms.Default is not settable
  • The property UnitOfWork on type Northwind.Webforms.Default is not settable
Can anyone help?

Please advise, on a fix.
Thanks
Nov 20, 2014 at 6:34 PM
Sorry, was very sleepy when I published this

Fixed with this:

Change
[Dependency]
private ICustomerService CustomerService { get; set; }

[Dependency]
private IUnitOfWorkAsync UnitOfWork { get; set; }
for this
[Dependency]
public ICustomerService CustomerService { get; set; }

[Dependency]
public IUnitOfWorkAsync UnitOfWork { get; set; }
Thanks
Marked as answer by ericardezp on 11/20/2014 at 10:37 AM