Inheritance: Newtonsoft.Json.Serialization.DefaultContractResolver
Ejemplo n.º 1
0
        public void CreateObjectWithParameters()
        {
            int count = 0;

            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType <TaskRepository>().As <ITaskRepository>();
            builder.RegisterType <TaskController>();
            builder.Register(c =>
            {
                count++;
                return(new LogManager(new DateTime(2000, 12, 12)));
            }).As <ILogger>();

            IContainer container = builder.Build();

            AutofacContractResolver contractResolver = new AutofacContractResolver(container);

            TaskController controller = JsonConvert.DeserializeObject <TaskController>(@"{
                'Logger': {
                    'Level':'Debug'
                }
            }", new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            });

            Assert.IsNotNull(controller);
            Assert.IsNotNull(controller.Logger);

            Assert.AreEqual(1, count);

            Assert.AreEqual(new DateTime(2000, 12, 12), controller.Logger.DateTime);
            Assert.AreEqual("Debug", controller.Logger.Level);
        }
Ejemplo n.º 2
0
        public void CreateObjectWithSettableParameter()
        {
            int count = 0;

            ContainerBuilder builder = new ContainerBuilder();

            builder.Register(c =>
            {
                count++;
                return(new TaskRepository());
            }).As <ITaskRepository>();
            builder.RegisterType <HasSettableProperty>();
            builder.Register(c =>
            {
                count++;
                return(new LogManager(new DateTime(2000, 12, 12)));
            }).As <ILogger>();

            IContainer container = builder.Build();

            AutofacContractResolver contractResolver = new AutofacContractResolver(container);

            HasSettableProperty o = JsonConvert.DeserializeObject <HasSettableProperty>(@"{
                'Logger': {
                    'Level': 'Debug'
                },
                'Repository': {
                    'ConnectionString': 'server=.'
                },
                'People': [
                    {
                        'Name': 'Name1!'
                    },
                    {
                        'Name': 'Name2!'
                    }
                ],
                'Person': {
                    'Name': 'Name3!'
                }
            }", new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            });

            Assert.IsNotNull(o);
            Assert.IsNotNull(o.Logger);
            Assert.IsNotNull(o.Repository);

            Assert.AreEqual(2, count);

            Assert.AreEqual(new DateTime(2000, 12, 12), o.Logger.DateTime);
            Assert.AreEqual("Debug", o.Logger.Level);
            Assert.AreEqual("server=.", o.Repository.ConnectionString);
            Assert.AreEqual(2, o.People.Count);
            Assert.AreEqual("Name1!", o.People[0].Name);
            Assert.AreEqual("Name2!", o.People[1].Name);
            Assert.AreEqual("Name3!", o.Person.Name);
        }
        public void ResolveContractFromAutofac()
        {
            ContainerBuilder builder = new ContainerBuilder();

            builder.RegisterType <Company>().As <ICompany>();
            IContainer container = builder.Build();

            AutofacContractResolver resolver = new AutofacContractResolver(container);

            User user = JsonConvert.DeserializeObject <User>("{'company':{'company_name':'Company name!'}}", new JsonSerializerSettings
            {
                ContractResolver = resolver
            });

            Assert.AreEqual("Company name!", user.Company.CompanyName);
        }
        public void CreateObjectWithSettableParameter()
        {
            int count = 0;

            ContainerBuilder builder = new ContainerBuilder();
            builder.Register(c =>
            {
                count++;
                return new TaskRepository();
            }).As<ITaskRepository>();
            builder.RegisterType<HasSettableProperty>();
            builder.Register(c =>
            {
                count++;
                return new LogManager(new DateTime(2000, 12, 12));
            }).As<ILogger>();

            IContainer container = builder.Build();

            AutofacContractResolver contractResolver = new AutofacContractResolver(container);

            HasSettableProperty o = JsonConvert.DeserializeObject<HasSettableProperty>(@"{
                'Logger': {
                    'Level': 'Debug'
                },
                'Repository': {
                    'ConnectionString': 'server=.'
                },
                'People': [
                    {
                        'Name': 'Name1!'
                    },
                    {
                        'Name': 'Name2!'
                    }
                ],
                'Person': {
                    'Name': 'Name3!'
                }
            }", new JsonSerializerSettings
              {
                  ContractResolver = contractResolver
              });

            Assert.IsNotNull(o);
            Assert.IsNotNull(o.Logger);
            Assert.IsNotNull(o.Repository);

            Assert.AreEqual(2, count);

            Assert.AreEqual(new DateTime(2000, 12, 12), o.Logger.DateTime);
            Assert.AreEqual("Debug", o.Logger.Level);
            Assert.AreEqual("server=.", o.Repository.ConnectionString);
            Assert.AreEqual(2, o.People.Count);
            Assert.AreEqual("Name1!", o.People[0].Name);
            Assert.AreEqual("Name2!", o.People[1].Name);
            Assert.AreEqual("Name3!", o.Person.Name);
        }
        public void CreateObjectWithParameters()
        {
            int count = 0;

            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<TaskRepository>().As<ITaskRepository>();
            builder.RegisterType<TaskController>();
            builder.Register(c =>
            {
                count++;
                return new LogManager(new DateTime(2000, 12, 12));
            }).As<ILogger>();

            IContainer container = builder.Build();

            AutofacContractResolver contractResolver = new AutofacContractResolver(container);

            TaskController controller = JsonConvert.DeserializeObject<TaskController>(@"{
                'Logger': {
                    'Level':'Debug'
                }
            }", new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            });

            Assert.IsNotNull(controller);
            Assert.IsNotNull(controller.Logger);

            Assert.AreEqual(1, count);

            Assert.AreEqual(new DateTime(2000, 12, 12), controller.Logger.DateTime);
            Assert.AreEqual("Debug", controller.Logger.Level);
        }
        public void Example()
        {
            #region Usage
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<TaskRepository>().As<ITaskRepository>();
            builder.RegisterType<TaskController>();
            builder.Register(c => new LogService(new DateTime(2000, 12, 12))).As<ILogger>();

            IContainer container = builder.Build();

            AutofacContractResolver contractResolver = new AutofacContractResolver(container);

            string json = @"{
              'Logger': {
                'Level':'Debug'
              }
            }";

            // ITaskRespository and ILogger constructor parameters are injected by Autofac 
            TaskController controller = JsonConvert.DeserializeObject<TaskController>(json, new JsonSerializerSettings
            {
                ContractResolver = contractResolver
            });

            Console.WriteLine(controller.Repository.GetType().Name);
            // TaskRepository
            #endregion

            Assert.IsNotNull(controller);
            Assert.IsNotNull(controller.Logger);

            Assert.AreEqual(new DateTime(2000, 12, 12), controller.Logger.DateTime);
            Assert.AreEqual("Debug", controller.Logger.Level);
        }
        public void ResolveContractFromAutofac()
        {
            ContainerBuilder builder = new ContainerBuilder();
            builder.RegisterType<Company>().As<ICompany>();
            IContainer container = builder.Build();

            AutofacContractResolver resolver = new AutofacContractResolver(container);

            User user = JsonConvert.DeserializeObject<User>("{'company':{'company_name':'Company name!'}}", new JsonSerializerSettings
            {
                ContractResolver = resolver
            });

            Assert.AreEqual("Company name!", user.Company.CompanyName);
        }