// 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(); }
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); }
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); DeltaConfig.Init(cfg => { cfg.AddEntity <Person>() .Property(x => x.Id).Exclude(); }); }
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)); }
public void IgnoreLetterCase() { DeltaConfig.Init(cfg => { cfg .IgnoreLetterCase() .AddEntity <Person>(); }); CreateDelta <Person>("AgE", 23).Patch(John); Assert.AreEqual(23, John.Age); }
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); }
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); }
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); }
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(); }); }
protected void Application_Start() { GlobalConfiguration.Configure(WebApiConfig.Register); DeltaConfig.Init(x => x.ExcludeProperties <Person>(y => y.Id)); }
public static void ClassInit(TestContext context) { DeltaConfig.Init(cfg => cfg.AddEntity <PersonExtended>()); }
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); }