Exemplo n.º 1
0
        /// <summary>
        /// Creates a controller object
        /// </summary>
        /// <param name="controllerType"></param>
        /// <param name="tinyDependencyInjection"></param>
        /// <returns></returns>
        public static object Create(Type controllerType, TinyDependencyInjection tinyDependencyInjection)
        {
            try
            {
                var constructor = controllerType.GetConstructors().FirstOrDefault();
                var parameters  = constructor.GetParameters();
                if (parameters.Any())
                {
                    var objs = new List <object>();
                    objs.AddRange(parameters.Select(x => tinyDependencyInjection.GetService(x.ParameterType)));
                    var newControllerObj = Activator.CreateInstance(controllerType, objs.ToArray());
                    return(newControllerObj);
                }
                else
                {
                    return(Activator.CreateInstance(controllerType));
                }
            }
            catch (Exception e)
            {
                throw new Exception($"CreateControllerInstance Create Error: {e.Message}");
            }

            return(null);
        }
Exemplo n.º 2
0
 public HandlerDispatcher(Dictionary <string, Type> controllers, TinyDependencyInjection tinyDependencyInjection, List <ControllerInfo> controllerInfos, List <RouteInfo> routes)
 {
     _controllers             = controllers;
     _tinyDependencyInjection = tinyDependencyInjection;
     _controllerInfos         = controllerInfos;
     _routes = routes;
 }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            var di = new TinyDependencyInjection();

            di.AddDependency(Dependency.Create().For <ITestWriter>().Use <TestWriter>());
            di.Init();

            var testWriter         = di.GetService <TestWriter>();
            var testWriterFromType = di.GetService(typeof(TestWriter)) as TestWriter;

            testWriter.PrintHelloWorld();
            testWriterFromType.PrintHelloWorld();
        }
Exemplo n.º 4
0
        private static async Task Go()
        {
            Console.WriteLine("Starting");
            Console.WriteLine("Dependency Injection - Register All");
            // Dependency Injection
            var di = new TinyDependencyInjection();

            di.AddDependency(Dependency.Create().For <ICommandHandler <ICommand> >().Use <Declaration_CommandHandler>());
            di.AddDependency(Dependency.Create().For <IRepository <Declaration> >().Use <DeclarationEventsRepository <Declaration> >().SetBehaviour(DIBehaviour.Singleton));
            di.AddDependency(Dependency.Create().For <IRepository <Event> >().Use <DeclarationRepository <Event> >().SetBehaviour(DIBehaviour.Singleton));
            di.AddDependency(Dependency.Create().For <IRepository <Event> >().Use <DeclarationRepository <Event> >().SetBehaviour(DIBehaviour.Singleton));
            di.AddDependency(Dependency.Create().For <IQueryHandler <Declaration_GetQuery, Declaration> >().Use <Declaration_QueryHandler>().SetBehaviour(DIBehaviour.Singleton));
            di.Init(); // Init All Objects

            // CommandBus - need for send Command.
            var CommandBus = new CommandBus(di);
            // QueryBus - need for send Queries and Receive Data
            var QueryBus = new QueryBus(di);

            Console.WriteLine("Create two Entities");
            // Create two commands which Create Declaration objects in Repos and Write to EventRepo
            var CreateCommand1 = new Declaration_CreateCommand()
            {
                Id = "1", Title = "CreateCommand1", Description = "This is First Command1"
            };
            await CommandBus.SendAsync(CreateCommand1);

            var CreateCommand2 = new Declaration_CreateCommand()
            {
                Id = "2", Title = "CreateCommand2", Description = "This is First Command2"
            };
            await CommandBus.SendAsync(CreateCommand2);

            Console.WriteLine("Get Data From First Entity");
            // Create query and get data about first Declaration object from repo
            var Get1Query = new Declaration_GetQuery()
            {
                Id = "1"
            };
            var res1 = await QueryBus.GetAsync <Declaration_GetQuery, Declaration>(Get1Query);

            var Get2Query = new Declaration_GetQuery()
            {
                Id = "2"
            };
            var res2 = await QueryBus.GetAsync <Declaration_GetQuery, Declaration>(Get2Query);

            Console.WriteLine(string.Format("Id = {0}; Title = {1}; Description = {2}", res1.Id, res1.Title, res1.Description));
            Console.WriteLine(string.Format("Id = {0}; Title = {1}; Description = {2}", res2.Id, res2.Title, res2.Description));
        }
Exemplo n.º 5
0
        public HttpServer(string host, Dictionary <string, Type> controllers, TinyDependencyInjection tinyDependencyInjection, List <ControllerInfo> controllerInfos, List <RouteInfo> routes)
        {
            _controllers             = controllers;
            _tinyDependencyInjection = tinyDependencyInjection;
            _controllerInfos         = controllerInfos;
            _routes       = routes;
            _httpListener = new HttpListener();
            _httpListener.Prefixes.Add(host);
            var stringRoutes = RouteScriber.GetControllerRoutes(_controllerInfos);

            foreach (var route in stringRoutes)
            {
                var prefixRoute = route.EndsWith("/") ? route.TrimEnd(new [] { '/', '\\' }) : route;
                var prefix      = $"{host}{prefixRoute}/";
                _httpListener.Prefixes.Add(prefix);
            }
            _handlerDispatcher = new HandlerDispatcher(_controllers, _tinyDependencyInjection, _controllerInfos, _routes);
        }
Exemplo n.º 6
0
 public CommandBus(TinyDependencyInjection tinyDI)
 {
     this.tinyDI = tinyDI;
 }
Exemplo n.º 7
0
 public QueryBus(TinyDependencyInjection tinyDI)
 {
     this.tinyDI = tinyDI;
 }
Exemplo n.º 8
0
 private WeberServer(string baseAddress, WeberServerSettings weberServerSettings)
 {
     _baseAddress             = baseAddress;
     _weberServerSettings     = weberServerSettings;
     _tinyDependencyInjection = new TinyDependencyInjection();
 }