public void GetsNames()
 {
     Assert.AreEqual("MockLogger", WithName.TypeName(typeof(MockLogger)));
     Assert.AreEqual("List`1", WithName.TypeName(typeof(List <>)));
     Assert.IsNull(WithName.Default(typeof(MockLogger)));
     Assert.IsNull(WithName.Default(typeof(List <>)));
 }
Пример #2
0
        /// <summary>Registers the type mappings with the Unity container.</summary>
        /// <param name="container">The unity container to configure.</param>
        /// <remarks>There is no need to register concrete types such as controllers or API controllers (unless you want to
        /// change the defaults), as Unity allows resolving a concrete type even if it was not previously registered.</remarks>
        public static void RegisterTypes(IUnityContainer container)
        {
            // NOTE: To load from web.config uncomment the line below. Make sure to add a Microsoft.Practices.Unity.Configuration to the using statements.
            // container.LoadConfiguration();

            // TODO: Register your types here
            // container.RegisterType<IProductRepository, ProductRepository>();

            // Add Extension
            container.AddNewExtension <ResolverUnityExtension>();

            container.AddNewExtension <DataUnityExtension>();

            // TODO: Register your types here
            container.RegisterTypes(AllClasses.FromLoadedAssemblies().Where(c =>
                                                                            (c.Namespace.StartsWith("CrossCutting") ||
                                                                             c.Namespace.StartsWith("SMS.Repository") ||
                                                                             c.Namespace.StartsWith("SMS.Services")
                                                                            ) && !c.Namespace.StartsWith("StoreManagementSystem.Controllers") && !c.Namespace.StartsWith("SMS.Repository.Context")),
                                    WithMappings.FromAllInterfacesInSameAssembly,
                                    type =>
            {
                if (type.Name.StartsWith("Mock"))
                {
                    return("Mock");
                }
                return(WithName.Default(type));
            },
                                    WithLifetime.ContainerControlled
                                    );
        }
Пример #3
0
        public static void All(IUnityContainer c)
        {
            if (c == null)
            {
                return;
            }
            if (Container == null)
            {
                Container = c;
            }

            List <Assembly> list = AppDomain.CurrentDomain.GetAssemblies().ToList();

            foreach (Assembly assembly in list)
            {
                IEnumerable <Type> types = from type in GetLoadableTypes(assembly)
                                           where Attribute.IsDefined(type, typeof(ImplementAttribute))
                                           select type;

                foreach (Type to in types)
                {
                    ImplementAttribute custom = to.GetCustomAttributes <ImplementAttribute>().SingleOrDefault();
                    if (custom != null)
                    {
                        Container.RegisterType(custom.FromType, to, WithName.Default(to), WithLifetime.ContainerControlled(to));
                    }
                }
            }
        }
Пример #4
0
 public override Func <Type, string> GetName()
 {
     return(type => unity.Registrations.Select(x => x.RegisteredType).Any(r => r.GetInterfaces().Contains(r))
         ? WithName.TypeName(type)
         : WithName.Default(type));
 }
Пример #5
0
 public static string AttributeNameOrDefault(Type type)
 {
     return(UnityNamedInstance.AttributeName(type) ?? WithName.Default(type));
 }
Пример #6
0
 public override Func <Type, String> GetName()
 {
     return(t => WithName.Default(t));
 }
Пример #7
0
        private static string SelectNameToRegister(Type type)
        {
            var attributes = type.GetCustomAttributes(false);

            return(WithName.Default(type));
        }
Пример #8
0
        public MainWindow()
        {
            InitializeComponent();

            firstUnity.RegisterInstance(typeof(IProductFactory), new ShoeProductFactory());


            var section = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");

            section.Configure(secondUnity);

            thirdUnity.RegisterTypes(
                AllClasses.FromLoadedAssemblies()
                .Where(x => x.IsPublic &&
                       x.GetInterfaces().Any() &&
                       !x.IsAbstract &&
                       x.IsClass),
                WithMappings.FromAllInterfacesInSameAssembly,
                type => (thirdUnity.Registrations
                         .Select(x => x.RegisteredType)
                         .Any(r => type.GetInterfaces().Contains(r)))?WithName.TypeName(type):WithName.Default(type),
                WithLifetime.ContainerControlled);

            forthUnity.RegisterInstance(typeof(IProductFactory), new JacketProductFactory(), WithLifetime.ExternallyControlled(typeof(IProductFactory)));
        }