How to use Unity Block solve the type that contain parameter?

Mar 17, 2013 at 7:42 PM
Edited Mar 17, 2013 at 8:07 PM

Take a look at the code.

public class UnitedExportDbContext : DbContext, IUnitOfWork
    //public string connectionString="data source=.\\SQLExpress;Integrated Security=SSPI;Initial Catalog=UnitedExportDb";
    public UnitedExportDbContext(string connectionString)
        : base(connectionString)

With Prism I can do like the line below

container.RegisterType<IUnitOfWork, ReservationContext>(new ContainerControlledLifetimeManager(), new InjectionConstructor("FasterTableContext"));

Because of we register the types like following way.
<type type="UnitedExport.Data.IRepositoryInitializer, UnitedExport.Data" mapTo="UnitedExport.Data.Sql.RepositoryInitializer, UnitedExport.Data.Sql">
      <lifetime type="perRequest" />
    <type type="UnitedExport.Data.IUnitOfWork, UnitedExport.Data" mapTo="UnitedExport.Data.Sql.UnitedExportDbContext, UnitedExport.Data.Sql">
      <lifetime type="perRequest" />
I create the database like following code.

public virtual void Execute(ProvisioningRequestObj obj)
        var repositoryInitializer = ServiceLocator.Current.GetInstance<IRepositoryInitializer>();
I got this error messages

{"The type String cannot be constructed. You must configure the container to supply this value."}

What I need is that the dynamic not lives within config file
Mar 18, 2013 at 9:09 PM

Based on my understanding, you can also declare in the configuration file if you want to pass an specific value to a constructor parameter. For example, I believe you can do this by using the <constructor> , <param> and <value> XML elements. You can find more information about this in the following sections of the Unity documentation:
On the other hand, besides registering the types in the container using a configuration file, you should also be able to register them in the same way that you do in Prism. For example, I tried commenting the registration line of the IVehicleRepository interface from the configuration file and registering the type manually in the Global.asax.cs 's InitializeDependencyInjectionContainer method:
private static void InitializeDependencyInjectionContainer()
    container = new UnityContainerFactory().CreateConfiguredContainer();            
    var serviceLocator = new UnityServiceLocator(container);
    ServiceLocator.SetLocatorProvider(() => serviceLocator);
    DependencyResolver.SetResolver(new UnityDependencyResolver(container));

    // We register the type manually.
    container.RegisterType<IVehicleRepository, VehicleRepository>(new UnityHttpContextPerRequestLifetimeManager());
The result was that both the type registered manually and the types registered by the configuration file were correctly registered in the container.

Either of the two approaches should be useful to register the corresponding types as you need, but the first approach seems to be the cleaner one.

I hope this helps,

Damian Cherubini
Mar 19, 2013 at 9:08 AM
Hi DCherubini,

Thanks, I was solve by inject the service rather than register it.

public UnitedExportDbContext(IConnectionString connectionString)
        : base(connectionString.GetConnectionString())

Because of I need dynamic not static. Is that correct way?