예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.AddDbContext <AppDbContext>(options =>
            {
                options.UseInMemoryDatabase("blog-in-memory");
            });

            services.AddScoped <IArticleRepository, ArticleRepository>();
            services.AddScoped <IArticleService, ArticleService>();

            services.AddScoped <IUserRepository, UserRepository>();
            services.AddScoped <IUserService, UserService>();

            services.AddScoped <IUnitOfWork, UnitOfWork>();

            DeltaConfig.Init(cfg => {
                cfg.AddEntity <Article>();
            });

            DeltaConfig.Init(cfg => {
                cfg.AddEntity <PatchArticleResource>();
            });

            services.AddAutoMapper();
        }
예제 #2
0
        public void MappingFunction()
        {
            DeltaConfig.Init(cfg =>
            {
                cfg

                /* When the target property type is int and the input is string,
                *  then the assigned value will be the length of the input string*/
                .AddMapping((propType, newValue) =>
                {
                    var result = new MapResult <object>();

                    if (propType != typeof(int))
                    {
                        return(result.SkipMap());
                    }
                    if (newValue.GetType() != typeof(string))
                    {
                        return(result.SkipMap());
                    }

                    result.Value = newValue.ToString().Length;

                    return(result);
                })

                /* When the target property is double and the input is string,
                 * then the assigned value will be the length of the string + 0.5*/
                .AddMapping((propType, newValue) =>
                {
                    var result = new MapResult <object>();

                    if (propType != typeof(double))
                    {
                        return(result.SkipMap());
                    }

                    if (newValue.GetType() != typeof(string))
                    {
                        return(result.SkipMap());
                    }

                    result.Value = newValue.ToString().Length + 0.5;

                    return(result);
                })
                .AddEntity <Person>();
            });

            // First mapping function will be executed here, Age type is int
            CreateDelta <Person, int>(x => x.Age, "abc").Patch(John);
            Assert.AreEqual("abc".Length, John.Age);

            // Second mapping function will be executed here, Height type is double
            CreateDelta <Person, double>(x => x.Height, "abcdef").Patch(John);
            Assert.AreEqual("abcdef".Length + 0.5, John.Height);
        }
예제 #3
0
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            DeltaConfig.Init(cfg => {
                cfg.AddEntity <Person>()
                .Property(x => x.Id).Exclude();
            });
        }
예제 #4
0
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                          .SetBasePath(env.ContentRootPath)
                          .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                          .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                          .AddEnvironmentVariables();

            Configuration = builder.Build();

            DeltaConfig.Init(x => x.ExcludeProperties <Person>(y => y.Id));
        }
예제 #5
0
        public void IgnoreLetterCase()
        {
            DeltaConfig.Init(cfg =>
            {
                cfg
                .IgnoreLetterCase()
                .AddEntity <Person>();
            });

            CreateDelta <Person>("AgE", 23).Patch(John);
            Assert.AreEqual(23, John.Age);
        }
예제 #6
0
        public void ExcludedByAttribute()
        {
            DeltaConfig.Init(cfg =>
            {
                cfg
                .AddEntity <Person>();
            });

            var initialAge = John.AgeExcludeByAttibute;

            CreateDelta <Person, int>(x => x.AgeExcludeByAttibute, 23).Patch(John);

            Assert.AreEqual(initialAge, John.AgeExcludeByAttibute);
        }
예제 #7
0
        public void IgnoreNullValue()
        {
            DeltaConfig.Init(cfg =>
            {
                cfg
                .AddEntity <Person>()
                .Property(x => x.Name).IgnoreNull();
            });

            var initialName = John.Name;

            CreateDelta <Person, string>(x => x.Name, null).Patch(John);

            Assert.AreEqual(initialName, John.Name);
        }
예제 #8
0
        public void Exclude()
        {
            DeltaConfig.Init(cfg =>
            {
                cfg
                .AddEntity <Person>()
                .Property(x => x.Age).Exclude();
            });

            var initialAge = John.Age;

            CreateDelta <Person, int>(x => x.Age, 23).Patch(John);

            Assert.AreEqual(initialAge, John.Age);
        }
예제 #9
0
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;

            // Setup simple patch
            DeltaConfig.Init(config =>
            {
                config.AddEntity <Games>();
                config.AddEntity <Jams>();
                config.AddEntity <Ratings>();
                config.AddEntity <Themes>();
                config.AddEntity <Users>();

                config.IgnoreLetterCase();
            });
        }
예제 #10
0
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);

            DeltaConfig.Init(x => x.ExcludeProperties <Person>(y => y.Id));
        }
예제 #11
0
 public static void ClassInit(TestContext context)
 {
     DeltaConfig.Init(cfg => cfg.AddEntity <PersonExtended>());
 }
예제 #12
0
        public void MappingFunction()
        {
            DeltaConfig.Init(cfg =>
            {
                cfg

                /* When the target property type is string and the input is string, then the assigned value will be the reversed input string */
                .AddMapping((propType, newValue) =>
                {
                    var result = new MapResult <object>();

                    if (propType == typeof(string) && newValue.GetType() == typeof(string))
                    {
                        result.Value = string.Join("", newValue.ToString().ToCharArray().Reverse());
                    }
                    else
                    {
                        result.Skip = true;
                    }

                    return(result);
                })
                .AddEntity <Person>()
                .Property(x => x.Name)
                /* If the input value is string, then the assigned value will be the same string. Overriding global mapping function.*/
                .AddMapping((propType, newValue) =>
                {
                    if (newValue.GetType() != typeof(string))
                    {
                        return(new MapResult <string>().SkipMap());
                    }
                    return(new MapResult <string>()
                    {
                        Value = newValue.ToString()
                    });
                })
                /* If the input value is int, then the assigned value will be "number:{number}" */
                .AddMapping((propType, newValue) =>
                {
                    var result = new MapResult <string>();

                    if (newValue.GetType() != typeof(int))
                    {
                        return(result.SkipMap());
                    }

                    result.Value = "number:" + newValue.ToString();

                    return(result);
                })

                /* If the input value is DateTime, then the assigned value will be "datetime:{datetime}".
                 * This behavior could be accomplished using only the previous mapping function. They are separeted
                 * functions to test mapping functions order execution*/
                .AddMapping((propType, newValue) =>
                {
                    var result = new MapResult <string>();

                    if (newValue.GetType() != typeof(DateTime))
                    {
                        return(result.SkipMap());
                    }

                    result.Value = "datetime:" + ((DateTime)newValue).ToString("s");

                    return(result);
                });
            });

            // Global mapping function executed here
            CreateDelta <Person, string>(x => x.Surname, "Rossi").Patch(John);
            Assert.AreEqual("issoR", John.Surname);

            // First property mapping function executed here
            CreateDelta <Person, string>(x => x.Name, "Mario").Patch(John);
            Assert.AreEqual("Mario", John.Name);

            // Second property mapping function executed here
            CreateDelta <Person, string>(x => x.Name, 15).Patch(John);
            Assert.AreEqual("number:15", John.Name);

            // Third property mapping function executed here
            CreateDelta <Person, string>(x => x.Name, John.BirthDate).Patch(John);
            Assert.AreEqual("datetime:1990-02-01T20:15:10", John.Name);
        }