//public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) protected override object CreateModel(ControllerContext controllerContext, ModelBindingContext bindingContext, System.Type modelType) { var id = bindingContext.ValueProvider.GetValue(bindingContext.ModelName + "." + Modl <M> .Definitions.IdProperty.PropertyName); if (id == null) { id = bindingContext.ValueProvider.GetValue(Modl <M> .Definitions.IdProperty.PropertyName); } if (id != null) { return(Modl <M> .Get(id.AttemptedValue)); } return(Modl <M> .New()); //if (bindingContext.ModelType.GetInterface("IModl") != null && value != null) //{ // if (MvcHelper.IsNumeric(value.AttemptedValue)) // { // var method = bindingContext.ModelType.GetMethods(BindingFlags.Static | BindingFlags.FlattenHierarchy | BindingFlags.Public).Single(x => x.Name == "Get" && x.GetParameters()[0].ParameterType == typeof(int)); // return method.Invoke(null, new object[] { Convert.ToInt32(value.AttemptedValue), true }); // } //} //return base.BindModel(controllerContext, bindingContext); }
public void Save() { var testClass = new CustomIdStringClass(); Assert.Throws <InvalidIdException>(() => testClass.Save()); var id = Guid.NewGuid().ToString(); testClass.CustomId = id; testClass.Save(); Assert.False(testClass.IsNew()); Assert.False(testClass.IsModified()); Assert.True(id == testClass.Id()); Assert.Equal(id, testClass.CustomId); var loadedTestClass = Modl <CustomIdStringClass> .Get(id); Assert.Equal(id, loadedTestClass.CustomId); Assert.True(id == loadedTestClass.Id()); Assert.Equal(id, loadedTestClass.CustomId); Assert.False(loadedTestClass.IsNew()); Assert.False(loadedTestClass.IsModified()); Assert.Throws <InvalidIdException>(() => loadedTestClass.Id(Guid.NewGuid().ToString())); Assert.Throws <InvalidIdException>(() => { loadedTestClass.CustomId = Guid.NewGuid().ToString(); loadedTestClass.Save(); }); }
public void Save() { var testClass = new CustomIdIntClass(); Assert.Throws <InvalidIdException>(() => testClass.Save()); var id = 2472; testClass.CustomId = id; testClass.Save(); Assert.False(testClass.IsNew()); Assert.False(testClass.IsModified()); Assert.True(id == testClass.Id()); Assert.Equal(id, testClass.CustomId); var loadedTestClass = Modl <CustomIdIntClass> .Get(id); Assert.Equal(id, loadedTestClass.CustomId); Assert.True(id == loadedTestClass.Id()); Assert.Equal(id, loadedTestClass.CustomId); Assert.False(loadedTestClass.IsNew()); Assert.False(loadedTestClass.IsModified()); Assert.Throws <InvalidIdException>(() => loadedTestClass.Id(4544)); Assert.Throws <InvalidIdException>(() => { loadedTestClass.CustomId = 4544; loadedTestClass.Save(); }); }
public void CRUDExplicitId() { Manufacturer m1 = Modl <Manufacturer> .New(Guid.NewGuid()); Assert.False(m1.IsModified()); m1.Name = "BMW"; Assert.True(m1.IsModified()); m1.Save(); Assert.False(m1.IsNew()); Assert.False(m1.IsModified()); Manufacturer m2 = Modl <Manufacturer> .Get(m1.ManufacturerID); AssertEqual(m1, m2); m2.Name = "Mercedes"; Assert.Equal("Mercedes", m2.Name); m2.Save(); Manufacturer m3 = Modl <Manufacturer> .Get(m1.ManufacturerID); Assert.Equal("Mercedes", m3.Name); m3.Delete(); Assert.True(m3.IsDeleted()); Assert.Throws <NotFoundException>(() => Modl <Manufacturer> .Get(m1.ManufacturerID)); }
//[TestCleanup] //public void Cleanup() //{ // foreach (var obj in Modl<CustomIdIntClass>.GetAll()) // obj.Delete(); //} private void Cleanup(int id) { if (Modl <CustomIdIntClass> .Exists(id)) { Modl <CustomIdIntClass> .Get(id).Delete(); } }
public void AddMultipleRelations() { var class1 = new Class1().Modl(); class1.MultipleRelation.Add(new Class2()); class1.MultipleRelation.Add(new Class2()); class1.Save(); foreach (var class2 in class1.MultipleRelation) { class2.Save(); } Assert.Equal(2, class1.MultipleRelation.Count()); Assert.Equal(class1.Id(), class1.MultipleRelation.First().SingleRelation.Id); Assert.Equal(class1.Id(), class1.MultipleRelation.First().SingleRelation.Val.Id()); var loadedClass1 = Modl <Class1> .Get(class1.Id()); Assert.Equal(2, loadedClass1.MultipleRelation.Count()); Assert.Equal(loadedClass1.Id(), loadedClass1.MultipleRelation.First().SingleRelation.Id); Assert.Equal(loadedClass1.Id(), loadedClass1.MultipleRelation.First().SingleRelation.Val.Id()); foreach (var class2 in class1.MultipleRelation) { var loadedClass2 = Modl <Class2> .Get(class2.Id()); Assert.NotNull(loadedClass2.SingleRelation.Val); Assert.Equal(2, loadedClass2.SingleRelation.Val.MultipleRelation.Count()); Assert.True(loadedClass2.SingleRelation.Val.MultipleRelation.Any(x => x.Id() == loadedClass2.Id())); } }
public void AddSingleRelationWithoutModlValue() { var class2 = new Class3().Modl(); class2.SingleRelation = new Class1(); class2.Save(); class2.SingleRelation.Save(); Assert.NotNull(class2.SingleRelation); Assert.Equal(1, class2.SingleRelation.MultipleRelation.Count()); Assert.Equal(class2.Id(), class2.SingleRelation.MultipleRelation.First().Id()); var loadedClass2 = Modl <Class3> .Get(class2.Id()); Assert.NotNull(loadedClass2.SingleRelation); Assert.Equal(1, loadedClass2.SingleRelation.MultipleRelation.Count()); Assert.Equal(loadedClass2.Id(), loadedClass2.SingleRelation.MultipleRelation.First().Id()); var loadedClass1 = Modl <Class1> .Get(class2.SingleRelation.Id()); Assert.Equal(1, loadedClass1.MultipleRelation.Count()); Assert.Equal(loadedClass1.Id(), loadedClass1.MultipleRelation.First().SingleRelation.Id); Assert.Equal(loadedClass1.Id(), loadedClass1.MultipleRelation.First().SingleRelation.Val.Id()); }
public void CRUD() { //Modl<Car>.Settings.ConfigurePipeline(new JsonModl<Car>(), new FileModl<Car>()); //ModlConfig.GlobalSettings.Serializer = new JsonModl(); //ModlConfig.GlobalSettings.Endpoint = new FileModl(); //Modl<Manufacturer>.Settings.Serializer = new JsonModl(); //Modl<Manufacturer>.Settings.Endpoint = new FileModl(); //Car car = Modl<Car>.New(); var car = new Car(); Assert.False(car.IsModified()); car.Name = "M3"; car.Manufacturer.Val = new Manufacturer("BMW"); car.Type = new CarType(); car.Type.Description = "Sedan"; car.Tags = new List <string>(); car.Tags.Add("Nice"); car.Tags.Add("Fast"); car.Tags.Add("Blue"); car.Tags.Add("Awful"); Assert.Equal("Sedan", car.Type.Description); Assert.True(car.IsModified()); car.Save(); Assert.False(car.IsNew()); Assert.False(car.IsModified()); car.Manufacturer.Val.Save(); Car car2 = Modl <Car> .Get(car.Id); AssertEqual(car, car2); Assert.Equal("Sedan", car2.Type.Description); car2.Manufacturer.Val.Name = "Mercedes"; Assert.Equal("Mercedes", car2.Manufacturer.Val.Name); car2.Manufacturer.Val.Save(); Car car3 = Modl <Car> .Get(car.Id); Assert.Equal("Mercedes", car3.Manufacturer.Val.Name); car3.Delete(); Assert.True(car3.IsDeleted()); Assert.False(car3.Manufacturer.Val.IsDeleted()); car3.Manufacturer.Val.Delete(); Assert.True(car3.Manufacturer.Val.IsDeleted()); Assert.Throws <NotFoundException>(() => Modl <Car> .Get(car.Id)); }
public void Save() { var testClass = new EmptyClass(); var id = testClass.Id(); testClass.Save(); Assert.False(testClass.IsNew()); Assert.False(testClass.IsModified()); Assert.Equal(id, testClass.Id()); var loadedTestClass = Modl <EmptyClass> .Get(id); Assert.Equal(id, loadedTestClass.Id()); Assert.False(loadedTestClass.IsNew()); Assert.False(loadedTestClass.IsModified()); }
public void TestRaces() { var testClass = new ThreadClass1(); testClass.Save(); var id = testClass.Id(); Parallel.For(0, 10, i => { var test = Modl <ThreadClass1> .Get(id); SetAndTest(test, 100); SetAndTest(test, 200); SetAndTest(test, 300); SetAndTest(test, 400); SetAndTest(test, 500); }); }
public void SetIdExplicit() { var id = Guid.NewGuid(); Manufacturer m1 = Modl <Manufacturer> .New(id); m1.Name = "Audi"; Assert.Equal(id, m1.ManufacturerID); m1.Save(); Assert.Equal(id, m1.ManufacturerID); var m2 = Modl <Manufacturer> .Get(m1.ManufacturerID.ToString()); AssertEqual(m1, m2); m2.Save(); Assert.Equal(id, m2.ManufacturerID); m2.Delete(); }
public void TestRaces() { Settings.GlobalSettings.InstanceSeparation = InstanceSeparation.Custom; Settings.GlobalSettings.CustomInstanceSeparationDictionary = () => GetDictionary(Thread.CurrentThread.ManagedThreadId); var testClass = new PerRequestThreadClass(); testClass.Save(); var id = testClass.Id(); Parallel.For(0, 10, i => { var test = Modl <PerRequestThreadClass> .Get(id); SetAndTest(test, 100); SetAndTest(test, 200); SetAndTest(test, 300); SetAndTest(test, 400); SetAndTest(test, 500); }); }
public void Save() { var testClass = new AutomaticIdGuidClass(); var id = testClass.Id(); testClass.Save(); Assert.False(testClass.IsNew()); Assert.False(testClass.IsModified()); Assert.Equal(id, testClass.Id()); Assert.True(id == testClass.CustomId); var loadedTestClass = Modl <AutomaticIdGuidClass> .Get(id); Assert.True(id == loadedTestClass.CustomId); Assert.Equal(id, loadedTestClass.Id()); Assert.True(id == loadedTestClass.CustomId); Assert.False(loadedTestClass.IsNew()); Assert.False(loadedTestClass.IsModified()); Assert.Throws <InvalidIdException>(() => loadedTestClass.Id(Guid.NewGuid())); }