0

Hell o,

I have a problem to register a repository with unity container. I have a following configuration and classes:

/* Generic repository: */

public class Repository<E> : IRepository<E> where E : EntityObject
{
    private readonly ObjectContext _ctx;

    public ObjectContext Context
    {
        get { return _ctx; }
    }

    public Repository(ObjectContext context)
    {
       _ctx = context;            
    }
}   

/* Concrete repository: */

public class SourceRepository : Repository<Source>
{
    public SourceRepository(EntityContext context) : base(context) { }    
}

/* EF generated context: */

public partial class EntityContext : ObjectContext
{
    public EntityContext() : base("name=EntityContext", "EntityContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    public EntityContext(string connectionString) : base(connectionString, "EntityContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }

    public EntityContext(EntityConnection connection) : base(connection, "EntityContext")
    {
        this.ContextOptions.LazyLoadingEnabled = true;
        OnContextCreated();
    }
}   

/* Registration of EntityContext type with constructor without parameters */

<register type="ObjectContext" mapTo="EntityContext" name="EntityContext">
    <constructor/>
</register>

/* Registration for generic repository (I'm not sure if this registration is needed) */

<register type="IRepository[]" mapTo="Repository[]" name="Repository">
    <lifetime type="transient" />
    <constructor>
        <param name="context">
            <dependency name="EntityContext"/>
        </param>
    </constructor>
</register>

/* Registration for concrete repository. I want to inject constructor dependency of EntityContext type */

<register type="IRepository[Source]" mapTo="SourceRepository" name="SourceRepository">
    <lifetime type="transient" />
    <constructor>
        <param name="context">
            <dependency name="EntityContext"/>
        </param>
    </constructor>
</register>

When I'm trying to load configuration I'm getting error:

Resolution of the dependency failed, type = "BL.DataAccess.Repository.SourceRepository", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type ObjectContext has multiple constructors of length 1. Unable to disambiguate.

I understand what this exception means, but I don't know where I have an error in my configuration.

Can you help please?

thanks.

5
  • Specifying EntityContext as the parameter type in the Constructor configuration might do the trick. Commented Feb 16, 2012 at 23:13
  • if you mean this: <constructor> <param name="context" type="EntityContext"> <dependency name="EntityContext"/> </param> </constructor> so it didn't help. The same error. Commented Feb 16, 2012 at 23:24
  • Looks SourceRepository has only one constractor. Can you try Type t = typeof(SourceRepository); ConstructorInfo[] constructorInfos = t.GetConstructors(); to check how many contractors it has. Commented Feb 16, 2012 at 23:30
  • yes, it has the only one constructor Commented Feb 16, 2012 at 23:33
  • Does the SourceRepository constructor take an EntityContext, like your code here? Or does it actually take an ObjectContext? Commented Feb 17, 2012 at 0:47

1 Answer 1

2

By the looks of the exception, Resolve is being called for an unnamed registration for SourceRepository.

Either ensure your configuration sets up all classes which depend on SourceRepository to use the correct named registration (through <param><dependency name="SourceRepository" /></param>).

Or remove the name in the source repository registration, so you'd end up with:

<register type="IRepository[Source]" mapTo="SourceRepository">
    <lifetime type="transient" />
    <constructor>
        <param name="context">
            <dependency name="EntityContext"/>
        </param>
    </constructor>
</register>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.