AutoPoco的使用
官方开发指导https://autopoco.codeplex.com/documentation
初步使用:
SimpleUser是自己要批量创建的类
1)创建管理工厂
1 IGenerationSessionFactory factory = AutoPocoContainer.Configure(x => 2 3 { 4 5 x.Conventions(c => 6 7 { 8 9 c.UseDefaultConventions(); 10 11 }); 12 13 x.AddFromAssemblyContainingType<SimpleUser>(); 14 15 });
2) 从工厂中创建会话
IGenerationSession session = factory.CreateSession();
3) 使用会话创建集合,List中的100表示创建含一百个元素的集合,创建的时候并没对集合中的元素进行赋值。
1 SimpleUser user = session.Single<SimpleUser>().Get(); 2 3 List<SimpleUser> users = session.List<SimpleUser>(100).Get();
在初步的基础上进行赋值
1 session.List<SimpleUser>(100) 2 3 .First(50) 4 5 .Impose(x => x.FirstName, "Rob") 6 7 .Impose(x => x.LastName, "Ashton") 8 9 .Next(50) 10 11 .Impose(x => x.FirstName, "Luke") 12 13 .Impose(x => x.LastName, "Smith") 14 15 .All().Random(25) 16 17 .Impose(x => x.Role,roleOne) 18 19 .Next(25) 20 21 .Impose(x => x.Role,roleTwo) 22 23 .Next(50) 24 25 .Impose(x => x.Role, roleThree) 26 27 .All() 28 29 .Invoke(x => x.SetPassword("Password1")) 30 31 .Get();
测试发现:
1、Next方法必须在First 或者Random使用之后才能使用,并且First只能使用一次在没调用All方法之前,First、Random、Next使用完之后必须调用All方法;
2、每次只能为一个属性赋值;
从数据源中创建
1 mFactory = AutoPocoContainer.Configure(x => 2 3 { 4 5 x.Conventions(c => 6 7 { 8 9 c.UseDefaultConventions(); 10 11 }); 12 13 14 15 x.AddFromAssemblyContainingType<SimpleUser>(); 16 17 x.Include<SimpleUser>() 18 19 .Setup(c => c.EmailAddress).Use<EmailAddressSource>() 20 21 .Setup(c => c.FirstName).Use<FirstNameSource>() 22 23 .Setup(c => c.LastName).Use<LastNameSource>() 24 25 .Invoke(c => c.SetPassword(Use.Source<String, PasswordSource>())); 26 27 x.Include<SomeType>() 28 29 .Setup(c => c.SomeString).Use<RandomStringSource>(5,10); 30 31 });
Use中的泛型就是传递给集合元素实例数据源,是个类。该类必须继承抽象泛型类DatasourceBase<T> 泛型T表示对应属性的数据类型。该抽象类中只有一个抽象方法Next,该方法就是返回数据给属性,实现给属性赋值。从而达到数据绑定;
在Conventions中实现数据源绑定
For example
1 A convention to set all String EmailAddress properties to use the EmailAddressSource 2 3 public class EmailAddressPropertyConvention : ITypePropertyConvention 4 5 { 6 7 public void Apply(ITypePropertyConventionContext context) 8 9 { 10 11 context.SetSource<EmailAddressSource>(); 12 13 } 14 15 16 17 public void SpecifyRequirements(ITypeMemberConventionRequirements requirements) 18 19 { 20 21 requirements.Name(x => String.Compare(x, "EmailAddress", true) == 0); 22 23 requirements.Type(x => x == typeof(String)); 24 25 } 26 27 } 28 29 30 31 A convention to set all String EmailAddress fields to use the EmailAddressSource 32 33 public class EmailAddressFieldConvention : ITypeFieldConvention 34 35 { 36 37 public void Apply(ITypeFieldConventionContext context) 38 39 { 40 41 context.SetSource<EmailAddressSource>(); 42 43 } 44 45 46 47 public void SpecifyRequirements(ITypeMemberConventionRequirements requirements) 48 49 { 50 51 requirements.Name(x => String.Compare(x, "EmailAddress", true) == 0); 52 53 requirements.Type(x => x == typeof(String)); 54 55 } 56 57 } 58 59 60 61 62 63 x.Conventions(c => c.Register(typeof(IdPropertyConvention))); 64 65 x.AddFromAssemblyContainingType<SimpleUser>();
在context中有个Setvalue方法 ,应该是给绑定数据源传值的,测试使用的时候并没有效果,传递多个值理论是使用的数组。问题未解决。