About IRegistrationSource

24 views
Skip to first unread message

Serhat Morkoç

unread,
Nov 24, 2014, 12:17:01 PM11/24/14
to aut...@googlegroups.com
hello to everyone.
I do not have much experience in AutoFac structure. I had to use this structure in any project. I am trying structure no problem. but here the working principle is exactly? I want to learn and improve myself.


public void Register(ContainerBuilder builder)
        {
           builder.RegisterType<SettingService>().As<ISettingService>().InstancePerHttpRequest();
           builder.RegisterSource(new SettingsSource());
       }

    public class SettingsSource : IRegistrationSource
    {
        static readonly MethodInfo BuildMethod = typeof(SettingsSource).GetMethod("BuildRegistration",BindingFlags.Static | BindingFlags.NonPublic);

        public IEnumerable<IComponentRegistration> RegistrationsFor(Service service,Func<Service, IEnumerable<IComponentRegistration>> registrations)
        {
            var ts = service as TypedService;
            if (ts != null && typeof(ISettings).IsAssignableFrom(ts.ServiceType))
            {
                var buildMethod = BuildMethod.MakeGenericMethod(ts.ServiceType);
                yield return (IComponentRegistration)buildMethod.Invoke(null, null);
            }
        }

        static IComponentRegistration BuildRegistration<TSettings>() where TSettings : ISettings, new()
        {
            return RegistrationBuilder
                .ForDelegate((c, p) =>
                {
                    
                    return c.Resolve<ISettingService>().LoadSetting<TSettings>();
                })
                .InstancePerHttpRequest()
                .CreateRegistration();
        }

        
    }

    

Travis Illig

unread,
Nov 24, 2014, 12:37:11 PM11/24/14
to aut...@googlegroups.com
It may be good to tell us what you're trying to accomplish (what is the intent of your code? what do you want it to do?) so we can better advise you on whether you're implementing things in a recommended pattern. Registration sources are a cool and very powerful extensibility point but I have to admit... I've never really found many cases where I have to actually use them in my code.

-T

Serhat Morkoç

unread,
Nov 24, 2014, 1:10:00 PM11/24/14
to aut...@googlegroups.com
I will explain the subject better tomorrow sample codes. Thank you for your comment
Reply all
Reply to author
Forward
0 new messages