/// <summary>
        /// Creates the data model for entire system.
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <remarks>
        /// Having multiple DbContexts and still create database for user on app launch is a challenge,
        /// <see cref="https://stackoverflow.com/a/11198345/32240"/>. I get around this issue using
        /// reflection here to load everything dynamically.
        /// </remarks>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var logger = _loggerFactory.CreateLogger <FanDbContext>();

            // find entities and model builders from app assemblies
            var typeFinder        = new TypeFinder(_loggerFactory);
            var entityTypes       = typeFinder.Find <Entity>();
            var modelBuilderTypes = typeFinder.Find <IEntityModelBuilder>();

            // add entity types to the model
            foreach (var type in entityTypes)
            {
                modelBuilder.Entity(type);
                logger.LogInformation($"Entity: '{type.Name}' added to model");
            }

            // call base
            base.OnModelCreating(modelBuilder);

            // add mappings and relations
            foreach (var builderType in modelBuilderTypes)
            {
                if (builderType != null && builderType != typeof(IEntityModelBuilder))
                {
                    logger.LogInformation($"ModelBuilder '{builderType.Name}' added to model");
                    var builder = (IEntityModelBuilder)Activator.CreateInstance(builderType);
                    builder.CreateModel(modelBuilder);
                }
            }
        }
Example #2
0
        /// <summary>
        /// Creates the data model for entire system.
        /// </summary>
        /// <param name="modelBuilder"></param>
        /// <remarks>
        /// Having multiple DbContexts and still create database for user on app launch is a challenge,
        /// <see cref="https://stackoverflow.com/a/11198345/32240"/>. I get around this issue using
        /// reflection here to load everything dynamically.
        /// </remarks>
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            // logger, can't inject through constructor due to AddDbContextPool restriction
            var serviceProvider = new ServiceCollection().AddLogging().BuildServiceProvider();
            var loggerFactory   = serviceProvider.GetService <ILoggerFactory>();
            var logger          = loggerFactory.CreateLogger <FanDbContext>();

            // find entities and model builders from app assemblies
            var typeFinder        = new TypeFinder();
            var entityTypes       = typeFinder.Find <Entity>();
            var modelBuilderTypes = typeFinder.Find <IEntityModelBuilder>();

            // add entity types to the model
            foreach (var type in entityTypes)
            {
                modelBuilder.Entity(type);
                logger.LogInformation($"Entity: {type.Name} added to model");
            }

            // call base
            base.OnModelCreating(modelBuilder);

            // add mappings and relations
            foreach (var builderType in modelBuilderTypes)
            {
                if (builderType != null && builderType != typeof(IEntityModelBuilder))
                {
                    logger.LogInformation($"ModelBuilder {builderType.Name} added to model");
                    var builder = (IEntityModelBuilder)Activator.CreateInstance(builderType);
                    builder.CreateModel(modelBuilder);
                }
            }
        }
Example #3
0
        private (MethodInfo MethodInfo, Func <object, object[], object> Func) FindMethod(ReflectionClass clazz,
                                                                                         ChaosInvocation request)
        {
            foreach (var method in clazz.AllMethods)
            {
                if (!IsMatchMethod(method.MethodInfo, request))
                {
                    continue;
                }

                return(method.MethodInfo, method.Func);
            }

            foreach (var method in clazz.AllGenericMethods)
            {
                if (!IsMatchMethod(method.MethodInfo, request))
                {
                    continue;
                }

                var genericTypes = request.GenericTypes
                                   .Select(x => _typeFinder.Find(x.ParameterType))
                                   .ToArray();

                return(method.MethodInfo, method.GetFunc(genericTypes));
            }

            throw new EntryPointNotFoundException(request.MethodName);
        }
    public void Simple()
    {
        var target            = TypeFinder.Find <MultiLayerNestedClass>();
        var methodDefinitions = target.FindMethodDefinitions("Method", null);

        Assert.AreEqual(1, methodDefinitions.Count);
    }
Example #5
0
    public void Simple()
    {
        var target           = TypeFinder.Find <GenericMethod>();
        var methodDefinition = target.FindMethodDefinitions("Method", null);

        Assert.NotNull(methodDefinition);
    }
        public object ToData(ChaosInvocationResp resp)
        {
            if (resp.Exception != null)
            {
                throw new RemotingException(resp.Exception);
            }

            if (string.IsNullOrEmpty(resp.DataTypeFullName))
            {
                return(null);
            }

            if (resp.Data == null)
            {
                return(null);
            }

            if (resp.Data.Length <= 0)
            {
                return(null);
            }

            var dataType = _typeFinder.Find(resp.DataTypeFullName);

            return(_serializer.Deserialize(dataType, resp.Data));
        }
Example #7
0
        public AttributeRemoteEventHandler(TypeFinder typeFinder, IIocResolver iocResolver, IEventBus eventBus)
        {
            Logger = NullLogger.Instance;

            _typeFinder  = typeFinder;
            _iocResolver = iocResolver;
            _eventBus    = eventBus;

            _typeMapping = new Dictionary <string, List <Tuple <Type, RemoteEventHandlerAttribute> > >();

            _typeFinder.Find(type => Attribute.IsDefined(type, typeof(RemoteEventHandlerAttribute), false) && typeof(IRemoteEventHandler).IsAssignableFrom(type))
            .ToList().ForEach(type =>
            {
                var attribute = Attribute.GetCustomAttribute(type, typeof(RemoteEventHandlerAttribute)) as RemoteEventHandlerAttribute;
                var key       = attribute.ForType;
                var item      = new Tuple <Type, RemoteEventHandlerAttribute>(type, attribute);
                if (_typeMapping.ContainsKey(key))
                {
                    var list = _typeMapping[key];
                    list.Add(item);
                }
                else
                {
                    _typeMapping.Add(key, new List <Tuple <Type, RemoteEventHandlerAttribute> >(new[] { item }));
                }
            });
        }
        public IRemoteEventBusConfiguration AutoSubscribe()
        {
            var topics = new List <string>();
            var types  = _typeFinder.Find(type =>
                                          type.GetInterfaces().Any(e => e.Name.Contains("IRemoteEventHandler")))
                         .ToList();

            foreach (var type in types)
            {
                var iTypes = type.GetInterfaces().Where(e => e.Name.Contains("IRemoteEventHandler")).ToList();

                foreach (var iType in iTypes)
                {
                    if (iType != null)
                    {
                        var gType = iType.GetGenericArguments().FirstOrDefault();
                        if (gType != null)
                        {
                            topics.Add(gType.FullName);
                        }
                    }
                }
            }

            Logger.Info($"auto subscribe topics {string.Join(",", topics)}");

            var remoteEventBus = _configuration.IocManager.Resolve <IRemoteEventBus>();

            remoteEventBus.Subscribe(topics);

            return(this);
        }
Example #9
0
        public object GetService(string interfaceTypename)
        {
            var interfaceType = _typeFinder.Find(interfaceTypename);
            var services      = _serviceProvider.GetServices(interfaceType);

            return(services.FirstOrDefault());
        }
Example #10
0
        protected override void OnExecute(XenMessageContext ctx)
        {
            var req = ctx.Get <AddSupportedTypeRequest>();

            if (req == null)
            {
                return;
            }

            var res = XenMessage.Create <AddSupportedTypeResponse>();

            try
            {
                if (TypeRegistrar.Instance.IsRegistered(req.Type))
                {
                    res.AlreadyRegistered = true;
                    res.DisplayMessage    = $"The type '{req.Type.FullName} is already registered.";
                }
                else
                {
                    if (!string.IsNullOrWhiteSpace(req.Type?.FullName))
                    {
                        var match = TypeFinder.Find(req.Type.FullName);

                        if (match == null)
                        {
                            res.DisplayMessage = $"The type '{req.Type.FullName}' was not found.";
                        }
                        else
                        {
                            if (match.GetTypeInfo().IsValueType)
                            {
                                res.Added = TypeRegistrar.Instance.AddType(req.Type);
                                res.Suggest <GetVisualTreeRequest>();

                                if (res.Added)
                                {
                                    res.DisplayMessage = $"The type '{req.Type.FullName}' can now be used.";
                                }
                            }
                            else
                            {
                                res.DisplayMessage = "You can only add value types in the beta.";
                            }
                        }
                    }
                    else
                    {
                        res.DisplayMessage = "You must enter a type name.";
                    }
                }
            }
            catch (Exception ex)
            {
                res.UnhandledExceptionOccurred = true;
                res.ExceptionMessage           = ex.ToString();
            }

            ctx.Response = res;
        }
    public void Simple()
    {
        var target = TypeFinder.Find <MultiLayerNestedClass>();
        var method = target.FindMethodDefinitions("Method", null);

        Assert.NotNull(method);
    }
Example #12
0
        public void Find()
        {
            TypeFinder Finder = new TypeFinder();
            List<System.Type> Types = Finder.Find<TypeToFindBase>();

            Assert.AreEqual(18, Types.Count());
        }
        public async Task Initialize()
        {
            if (!string.IsNullOrWhiteSpace(_assemblyPath) && _assembly == null)
            {
                if (!File.Exists(_assemblyPath))
                {
                    throw new ArgumentException($"Assembly in path {_assemblyPath} does not exist.");
                }

                _pluginAssemblyLoadContext = new PluginAssemblyLoadContext(_assemblyPath, _options.PluginLoadContextOptions);
                _assembly = _pluginAssemblyLoadContext.Load();
            }

            _plugins = new List <TypePluginCatalog>();

            var finder = new TypeFinder();

            foreach (var typeFinderCriteria in _options.TypeFinderCriterias)
            {
                var pluginTypes = finder.Find(typeFinderCriteria.Value, _assembly, _pluginAssemblyLoadContext);

                foreach (var type in pluginTypes)
                {
                    var typePluginCatalog = new TypePluginCatalog(type, new TypePluginCatalogOptions()
                    {
                        PluginNameOptions = _options.PluginNameOptions
                    });
                    await typePluginCatalog.Initialize();

                    _plugins.Add(typePluginCatalog);
                }
            }

            IsInitialized = true;
        }
Example #14
0
    public void Simple()
    {
        var target            = TypeFinder.Find <SimpleMethod>();
        var methodDefinitions = target.FindMethodDefinitions("Method", null);

        Assert.AreEqual(1, methodDefinitions.Count);
    }
Example #15
0
    public void Simple()
    {
        var target            = TypeFinder.Find <SimpleMethod>();
        var methodDefinitions = target.FindMethodDefinitions("Method", null);

        Assert.Single(methodDefinitions);
    }
Example #16
0
        protected override List <Type> FindServices()
        {
            var types = TypeFinder
                        .Find(t => typeof(ISpearService).IsAssignableFrom(t) && t.IsClass && !t.IsAbstract)
                        .ToList();

            return(types);
        }
Example #17
0
    public void BadNamespace()
    {
        var target = TypeFinder.Find <SimpleMethod>();

        Assert.Throws <WeavingException>(() => target.FindMethodDefinitions("MethodWithParam", new List <string> {
            "System2.Int32"
        }));
    }
Example #18
0
        private IDictionary DeserializeDictionary(SerializableDictionary serializableDictionary)
        {
            var keyType   = _typeFinder.Find(serializableDictionary.KeyTypeFullName);
            var valueType = _typeFinder.Find(serializableDictionary.ValueTypeFullName);

            var dict         = typeof(Dictionary <,>).MakeGenericType(keyType, valueType);
            var dictInstance = (IDictionary)Activator.CreateInstance(dict);

            foreach (var item in serializableDictionary.Items)
            {
                var key   = Deserialize(keyType, item.Key);
                var value = Deserialize(valueType, item.Value);
                dictInstance[key] = value;
            }

            return(dictInstance);
        }
Example #19
0
    public void FullWithNamespace()
    {
        var target            = TypeFinder.Find <NestedClass>();
        var methodDefinitions = target.FindMethodDefinitions("Method", new List <string> {
            "MyNamespace.Root/Nested"
        });

        Assert.AreEqual(1, methodDefinitions.Count);
    }
Example #20
0
    public void SimpleParam()
    {
        var target            = TypeFinder.Find <NestedClass>();
        var methodDefinitions = target.FindMethodDefinitions("Method", new List <string> {
            "Nested"
        });

        Assert.AreEqual(1, methodDefinitions.Count);
    }
Example #21
0
    public void BadNamespace()
    {
        var target            = TypeFinder.Find <SimpleMethod>();
        var methodDefinitions = target.FindMethodDefinitions("MethodWithParam", new List <string> {
            "System2.Int32"
        });

        Assert.Empty(methodDefinitions);
    }
Example #22
0
    public void FullWithNamespace()
    {
        var target = TypeFinder.Find <NestedClass>();
        var method = target.FindMethodDefinitions("Method", new List <string> {
            "MyNamespace.Root/Nested"
        });

        Assert.NotNull(method);
    }
Example #23
0
    public void Full()
    {
        var target            = TypeFinder.Find <NestedClass>();
        var methodDefinitions = target.FindMethodDefinitions("Method", new List <string> {
            "Root/Nested"
        });

        Assert.Single(methodDefinitions);
    }
Example #24
0
    public void SimpleParam()
    {
        var target            = TypeFinder.Find <SimpleMethod>();
        var methodDefinitions = target.FindMethodDefinitions("MethodWithParam", new List <string> {
            "System.Int32"
        });

        Assert.Single(methodDefinitions);
    }
Example #25
0
    public void SimpleParam()
    {
        var target = TypeFinder.Find <NestedClass>();
        var method = target.FindMethodDefinitions("Method", new List <string> {
            "Nested"
        });

        Assert.NotNull(method);
    }
    public void Full()
    {
        var target = TypeFinder.Find <MultiLayerNestedClass>();
        var method = target.FindMethodDefinitions("Method", new List <string> {
            "MultiLayerNestedClass/Nested/Nested2"
        });

        Assert.NotNull(method);
    }
Example #27
0
    public void SimpleWithParam()
    {
        var target            = TypeFinder.Find <MultiLayerNestedClass>();
        var methodDefinitions = target.FindMethodDefinitions("Method", new List <string> {
            "Nested2"
        });

        Assert.Single(methodDefinitions);
    }
    public void Full()
    {
        var target            = TypeFinder.Find <MultiLayerNestedClass>();
        var methodDefinitions = target.FindMethodDefinitions("Method", new List <string> {
            "MultiLayerNestedClass/Nested/Nested2"
        });

        Assert.AreEqual(1, methodDefinitions.Count);
    }
Example #29
0
        public void Test_QueryFinders()
        {
            var types = typeFinder.Find(type =>
                                        type.IsDefined(typeof(AutoMapAttribute)) ||
                                        type.IsDefined(typeof(AutoMapFromAttribute)) ||
                                        type.IsDefined(typeof(AutoMapToAttribute))
                                        );

            types.FirstOrDefault(t => t == typeof(ENodebView)).ShouldNotBeNull();
        }
 public static IEnumerable <object> DeserializeToArguments(this List <ChaosParameter> requestParameters,
                                                           IChaosSerializer serializer,
                                                           TypeFinder typeFinder)
 {
     foreach (var requestParameter in requestParameters)
     {
         var parameterType = typeFinder.Find(requestParameter.ParameterType);
         var value         = serializer.Deserialize(parameterType, requestParameter.Value);
         yield return(value);
     }
 }
Example #31
0
        public void StartService()
        {
            var types =
                TypeFinder.Find(
                    t => t.IsInterface && t != typeof(IWcfService) && typeof(IWcfService).IsAssignableFrom(t));

            foreach (var type in types)
            {
                var resolve = IocManager.Resolve(type);
                OpenService(type, resolve.GetType());
            }
        }
Example #32
0
        public static void Bootstrap()
        {
            using (new FunctionLogger(Log))
            {
                TypeFinder Finder = new TypeFinder(Assembly.GetCallingAssembly());

                foreach (Type Bootstrapper in Finder.Find<IBootstrapper>())
                {
                    Log.InfoFormat("Bootstrapping: {0}", Bootstrapper.FullName);
                    IBootstrapper Instance = (IBootstrapper)Activator.CreateInstance(Bootstrapper);
                    Instance.Initialise();
                }
            }
        }