0

I have written asp.net core application and used below code for dependency injection, which is working fine as expected.

services.AddScoped<IDeviceRepository<Device>>(factory =>
{
    return new DeviceRepository<Device>(
        new AzureTableSettings(
            storageAccount: Configuration.GetConnectionString("Table_StorageAccount"),
            storageKey: Configuration.GetConnectionString("Table_StorageKey"),
            tableName: Configuration.GetConnectionString("Table_TableName")));
});

Due to some reason I am moving back to ASP.NET MVC application, and I have to use 3rd party library for dependency injection. So I have used Unity framework.

container.RegisterType<IDeviceRepository<Device>>(factory =>
{
    return new DeviceRepository<Device>(
        new AzureTableSettings(
            storageAccount: Configuration.GetConnectionString("Table_StorageAccount"),
            storageKey: Configuration.GetConnectionString("Table_StorageKey"),
            tableName: Configuration.GetConnectionString("Table_TableName")));
});

But, I am getting error as follow

Severity Code Description Project File Suppression State Line Error CS1660 Cannot convert lambda expression to type 'InjectionMember[]' because it is not a delegate type \Presentation\Web\App_Start\UnityConfig.cs Active 60

Here is my DeviceRepository Class

public class DeviceRepository<T>: IDeviceRepository<T>
where T : TableEntity, new()
{
    ...
}

My AzureTableSettings class

public class AzureTableSettings
{
    public AzureTableSettings(string storageAccount,
                                   string storageKey,
                                   string tableName)
    {
        if (string.IsNullOrEmpty(storageAccount))
            throw new ArgumentNullException("StorageAccount");

        if (string.IsNullOrEmpty(storageKey))
            throw new ArgumentNullException("StorageKey");

        if (string.IsNullOrEmpty(tableName))
            throw new ArgumentNullException("TableName");

        this.StorageAccount = storageAccount;
        this.StorageKey = storageKey;
        this.TableName = tableName;
    }

    public string StorageAccount { get; }
    public string StorageKey { get; }
    public string TableName { get; }
}

How can use Dependency Injection in ASP.NET MVC application for this type of class?

Issue Resolved and here is the Solution

First register the type as follow

     container.RegisterType<IDeviceRepository<Device>>(
        new InjectionFactory(c => CreateDeviceRepository()));

Here is the CreateDeviceRepository method

       public static IDeviceRepository<Device> CreateDeviceRepository()
       {
             return new DeviceRepository<Device>(
                    new AzureTableSettings(
               storageAccount:Configuration.GetConnectionString("Table_StorageAccount"),
        storageKey: Configuration.GetConnectionString("Table_StorageKey"),
        tableName: Configuration.GetConnectionString("Table_TableName")));
                });
      }
kudlatiger
  • 3,028
  • 8
  • 48
  • 98

1 Answers1

-1

For my understanding, the error tells you to inherit from InjectionMember:

public class DeviceRepository<T>: InjectionMember, IDeviceRepository<T>
where T : TableEntity, new()
{
    ...
}
kara
  • 3,205
  • 4
  • 20
  • 34
  • InjectionMember is part of Unity and adding it's reference in repository make it tightly coupled. I wish to make changes only in web application, not in repositories or domain classes. I suspect it is syntax issue. – kudlatiger Apr 26 '18 at 13:14
  • Have a look to the docs. You need to implement `InjectionMember` for `UnityContainer.RegisterType`: https://msdn.microsoft.com/en-us/library/microsoft.practices.unity.unitycontainer.registertype.aspx – kara Apr 27 '18 at 06:58