how to use default contructor

Jun 26, 2012 at 2:17 PM

my config file like this:

<alias alias="IGenericRepository" type="ettaingroup.data.ef.Interfaces.IGenericRepository`1,ettaingroup.data.ef"/>
    <alias alias="Repository" type="ettaingroup.data.ef.Repositories.Repository`1,ettaingroup.data.ef"/>

 <alias alias="IPianoAreaService" type="PianoBusiness.BaseInfo.IPianoAreaService,PianoBusiness"/>
    <alias alias="PianoAreaService" type="PianoBusiness.BaseInfo.Impl.PianoAreaService,PianoBusiness"/>

<register type ="IGenericRepository" mapTo="Repository" name="pianoAreaDal"/>

<register type="IPianoAreaService" mapTo="PianoAreaService" name="pianoAreaService">
        <constructor>
          <param name="pianoAreaDal">
            <dependency name="pianoAreaDal"></dependency>
          </param>
        </constructor>
      </register>

and my service contructor like this:

public class PianoAreaService : IPianoAreaService
{

private Repository<PianoArea> _pianoAreaDal;

        public PianoAreaService(Repository<PianoArea> pianoAreaDal)
        {
            _pianoAreaDal = pianoAreaDal;
        }

}

public class Repository<T> : IGenericRepository<T> where T : class

{

private DbContext ctx;

/// <summary>
        /// Default constructor creating a new DbContext and setting Autosave to true
        /// </summary>
        public Repository()
        {
            ctx = new DbContext(ConfigurationManager.AppSettings["EntityFrameworkGenericRepository_ConnectionString"]);
            ctx.Configuration.ProxyCreationEnabled = false;
            ctx.Configuration.ValidateOnSaveEnabled = true;
            Autosave = true;
            AutoDisposeContext = true;
        }

/// <summary>
        /// Constructor specifying the DbContext to use for operations, and setting Autosave to false.
        /// </summary>
        /// <param name="context">The DbContext to use for operations</param>
        public Repository(DbContext context)
        {
            ctx = context;
            ctx.Configuration.ProxyCreationEnabled = false;
            Autosave = true;
            AutoDisposeContext = false;
        }

}

when i run test throw expection :

Resolution of the dependency failed, type = "PianoBusiness.BaseInfo.IPianoAreaService", name = "pianoAreaService".
Exception occurred while: while resolving.
Exception is: InvalidOperationException - The type DbConnection cannot be constructed. You must configure the container to supply this value.
-----------------------------------------------
At the time of the exception, the container was:

  Resolving PianoBusiness.BaseInfo.Impl.PianoAreaService,pianoAreaService (mapped from PianoBusiness.BaseInfo.IPianoAreaService, pianoAreaService)
  Resolving parameter "pianoAreaDal" of constructor PianoBusiness.BaseInfo.Impl.PianoAreaService(ettaingroup.data.ef.Repositories.Repository`1[[PianoData.PianoArea, PianoData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] pianoAreaDal)
    Resolving ettaingroup.data.ef.Repositories.Repository`1[PianoData.PianoArea],pianoAreaDal
    Resolving parameter "context" of constructor ettaingroup.data.ef.Repositories.Repository`1[[PianoData.PianoArea, PianoData, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]](System.Data.Entity.DbContext context, System.Boolean validateEntitesOnSave)
      Resolving System.Data.Entity.DbContext,(none)
      Resolving parameter "existingConnection" of constructor System.Data.Entity.DbContext(System.Data.Common.DbConnection existingConnection, System.Data.Entity.Infrastructure.DbCompiledModel model, System.Boolean contextOwnsConnection)
        Resolving System.Data.Common.DbConnection,(none)

how to do ?

I want to use Repository contructor with no param!

thanks!

Jun 26, 2012 at 2:21 PM

Just use an empty <constructor/> element in the configuration.

Jun 27, 2012 at 1:33 AM

I have resolved it

thanks!