Skip to content

Question...How do I do this? #194

Closed
Closed

Description

@scovel

I'm trying to port some code from Unity. (SolrNet container integration)

Here's the syntax..

container.RegisterType<IReadOnlyMappingManager, MemorizingMappingManager>(new InjectionConstructor(new ResolvedParameter(typeof (AttributesMappingManager))));

Another:

 var ISolrQueryExecuter = typeof (ISolrQueryExecuter<>).MakeGenericType(core.DocumentType);
            var SolrQueryExecuter = typeof (SolrQueryExecuter<>).MakeGenericType(core.DocumentType);
            string coreConnectionId = GetCoreConnectionId(core.Id);
            var registrationId = isNamed ? core.Id : null;
            container.RegisterType(
                ISolrQueryExecuter, SolrQueryExecuter, registrationId,
                new InjectionConstructor(
                    new ResolvedParameter(typeof (ISolrAbstractResponseParser<>).MakeGenericType(core.DocumentType)),
                    new ResolvedParameter(typeof (ISolrConnection), coreConnectionId),
                    new ResolvedParameter(typeof (ISolrQuerySerializer)),
                    new ResolvedParameter(typeof (ISolrFacetQuerySerializer)),
                    new ResolvedParameter(typeof (ISolrMoreLikeThisHandlerQueryResultsParser<>).MakeGenericType(core.DocumentType))));

The point seems to be to create a named (keyed?) registrations.

You get this instances like this:

var core1 = container.Resolve<ISolrOperations<Entity>>("core1");

How would you do this in Grace?

Activity

  1. ipjohnson commented on Sep 17, 2018

    @ipjohnson
    Owner

    Hi @scovel,

    I’ll find some examples tonight showing how to do this. Essentially it’s just adding a couple WithCtorParam configurations to the Export call.

  2. ipjohnson commented on Sep 18, 2018

    @ipjohnson
    Owner

    I realized there was no method to register what you wanted to do so I added a new method and will do a nuget pre-release for it.

    Case 1:

    container.Configure(c => 
           c.Export<MemorizingMappingManager>().As<IReadOnlyMappingManager>().
             WithCtorParam<IReadOnlyMappingManager>().Use(typeof(AttributesMappingManager)));
             

    Case 2:

    if(registrationId == null)
    {
      container.Configure(c => 
            c.Export(SolrQueryExecuter).As(ISolrQueryExecuter).
              WithCtorParam(typeof(ISolrConnection)).LocateWithKey(coreConnectionId));
    }
    else 
    {
      container.Configure(c => 
            c.Export(SolrQueryExecuter).AsKeyed(ISolrQueryExecuter, registrationId).
              WithCtorParam(typeof(ISolrConnection)).LocateWithKey(coreConnectionId));
    }
  3. scovel commented on Sep 19, 2018

    @scovel
    Author

    That is working!!!

    I owe you so many beers....

    So, follow-up question. I need to set an injection property at the same time. Unity was using InjectionProperty. I looked at ImportMembers but couldn't figure out how to set a specific value.

    .ImportMembers(MembersThat.AreNamed("HttpWebRequestFactory"))

    Looking at the property in the library there is no specific injection attributes on it, it's just a public property.

            /// <summary>
            /// HTTP request factory
            /// </summary>
            public IHttpWebRequestFactory HttpWebRequestFactory { get; set; }
    

    Here's the Unity code:

    container.RegisterType<ISolrConnection, SolrConnection>("solr" + typeof(SolrConnection), 
         new InjectionMember[] { new InjectionConstructor("http://localhost:8983/solr"),  
         new InjectionProperty("HttpWebRequestFactory", new BasicAuthHttpWebRequestFactory("user", "pass")) });
    

    Now, why they didn't just add an overloaded constructor, I have no idea...

  4. scovel commented on Sep 19, 2018

    @scovel
    Author

    That is working!!!

    I owe you so many beers....

    So, follow-up question. I need to set an injection property at the same time. Unity was using InjectionProperty. I looked at ImportMembers but couldn't figure out how to set a specific value.

    .ImportMembers(MembersThat.AreNamed("HttpWebRequestFactory"))

    Looking at the property in the library there is no specific injection attributes on it, it's just a public property.

            /// <summary>
            /// HTTP request factory
            /// </summary>
            public IHttpWebRequestFactory HttpWebRequestFactory { get; set; }
    

    Here's the Unity code:

    container.RegisterType<ISolrConnection, SolrConnection>("solr" + typeof(SolrConnection), 
         new InjectionMember[] { new InjectionConstructor("http://localhost:8983/solr"),  
         new InjectionProperty("HttpWebRequestFactory", new BasicAuthHttpWebRequestFactory("user", "pass")) });
    

    Now, why they didn't just add an overloaded constructor, I have no idea...

  5. ipjohnson commented on Sep 19, 2018

    @ipjohnson
    Owner

    @scovel, I think is probably another case of the container can do it but the registration method is missing for non generic.

    I can add it but I may not be able to get to it till this weekend because of family obligations.

  6. scovel commented on Sep 20, 2018

    @scovel
    Author

    Sounds good. I finished up the rest of the port and didn't find anything else I need. Let me know when you want me to test.

    Have a good weekend.

  7. ipjohnson commented on Sep 22, 2018

    @ipjohnson
    Owner

    Ok I've pushed out a beta to nuget with the new ImportProperty method.

  8. scovel commented on Sep 24, 2018

    @scovel
    Author

    The beta is working great! Thanks for your efforts again.

  9. ipjohnson commented on Sep 24, 2018

    @ipjohnson
    Owner

    Very cool, in 2-3 weeks I’ll plan to do an official release.

  10. scovel commented on Oct 11, 2018

    @scovel
    Author

    How's it going with the official release? We have a release coming up in a week or so and I'd like to change my Grace package from Beta before that happens.

    Thanks,

    Sean

  11. ipjohnson commented on Oct 11, 2018

    @ipjohnson
    Owner

    I’ll do an official release this weekend.

  12. ipjohnson commented on Oct 13, 2018

    @ipjohnson
    Owner

    marking as closed in release 6.4.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions