Calling from WebForms

May 20, 2014 at 9:12 PM
Edited May 20, 2014 at 9:14 PM
Currently, the implementation of the IRepository works like : (excerpt from service layer)
public interface IAccountService : IService<Account>
{
    // local functions
    List<Account> GetAccountsByClient(Guid pClientId);

}

public class AccountService : Service<Account>, IAccountService
{
    private readonly IRepositoryAsync<Account> _repository;

    public AccountService(IRepositoryAsync<Account> repository)  : base(repository)
    {
        _repository = repository;
    }

    public List<Account> GetAccountsByClient(Guid pClientId)
    {
        List<Account> oAccounts = new List<Account>();

        try
        {
            oAccounts = _repository.Queryable().Where(a => a.ClientId == pClientId).ToList();
        }
        catch
        { 
        }
        return oAccounts;
    }

    public override void Update(Account pAccount)
    { 
        // do security check
        _repository.Update(pAccount);
    }

 }
But, when binding to the WebForms Grid, I get the message "the repository does not contain a constructor that take 0 arguments".

Please advise, on a fix.
thanks
May 20, 2014 at 9:37 PM
Sounds like you need you configure you DI/IoC framework to inject the right constructor for the repository you are requesting to be injected with.
Marked as answer by lelong37 on 6/3/2014 at 11:16 AM
May 20, 2014 at 9:41 PM
that's true. Can you point to the a sample pls.
May 20, 2014 at 9:43 PM
Here's an example using Unity as the DI/IoC framework in ASP.NET MVC.

https://genericunitofworkandrepositories.codeplex.com/SourceControl/latest#main/Sample/Northwind.Web/App_Start/UnityConfig.cs

Setup should be fairly similar for Web Forms.
May 20, 2014 at 9:52 PM
Edited May 28, 2014 at 3:55 PM
still continuing to have the issue.
Here's what I have.
  1. in the global.asax
    protected void Application_Start(object sender, EventArgs e)
    {
        IUnityContainer container = Application.GetContainer();
        Bootstrapper.Configure(container);
    }
    
  2. public class Bootstrapper
    {
    public static void Configure(IUnityContainer container)
    {
        container
            .RegisterType<IDataContextAsync, FESWebContext>()
            .RegisterType<IUnitOfWorkAsync, UnitOfWork>()
            .RegisterType<IRepositoryAsync<FES.Models.Account>, Repository<FES.Models.Account>>()
            .RegisterType<IAccountService, AccountService>();
    }
    }
  3. public Repository(IDataContextAsync context, IUnitOfWorkAsync unitOfWork)
    {
        _context = context;
        _unitOfWork = unitOfWork;
    
        var dbContext = context as DbContext;
    
        if (dbContext != null)
        {
            _dbSet = dbContext.Set<TEntity>();
        }
    } 
    
  4. Where should I initialize the AccountService BLL object that initializes the AccountRepository object ? Page_load maybe ?
If I do this in the Page_Load, it works : ASPxGridView1.DataSource = new AccountService(new Repository<Account>(new FESWebContext(), null)).GetAccountsByClient(new Guid("abc"));


I know the whole point of doing DI is to avoid what I am doing above....but how can I.

But, when I bind the Service Layer to a Grid, I get - "the repository does not contain a constructor that take 0 arguments".

Please guide me. TY
Jun 3, 2014 at 7:16 PM
If you are still having this issue, please paste in the entire stack trace from the exception.
Jun 4, 2014 at 9:34 PM
Updating the registration like below fixed the issue :
private static void RegisterDependencies(IUnityContainer container)
    {
        container
            .RegisterType<IDataContextAsync, FESWebContext>(new HierarchicalLifetimeManager())
            .RegisterType<IUnitOfWorkAsync, UnitOfWork>(new HierarchicalLifetimeManager())

            .RegisterType<IPaymentService, PaymentService>()
            .RegisterType<IRepositoryAsync<Payment>, Repository<Payment>>()
Marked as answer by shaq2676 on 6/4/2014 at 1:35 PM