标签:
官方开发指导https://autopoco.codeplex.com/documentation
SimpleUser是自己要批量创建的类
1)创建管理工厂
IGenerationSessionFactory factory = AutoPocoContainer.Configure(x =>
{
x.Conventions(c =>
{
c.UseDefaultConventions();
});
x.AddFromAssemblyContainingType<SimpleUser>();
});
2) 从工厂中创建会话
IGenerationSession session = factory.CreateSession();
3) 使用会话创建集合,List中的100表示创建含一百个元素的集合,创建的时候并没对集合中的元素进行赋值。
SimpleUser user = session.Single<SimpleUser>().Get();
List<SimpleUser> users = session.List<SimpleUser>(100).Get();
session.List<SimpleUser>(100)
.First(50)
.Impose(x => x.FirstName, "Rob")
.Impose(x => x.LastName, "Ashton")
.Next(50)
.Impose(x => x.FirstName, "Luke")
.Impose(x => x.LastName, "Smith")
.All().Random(25)
.Impose(x => x.Role,roleOne)
.Next(25)
.Impose(x => x.Role,roleTwo)
.Next(50)
.Impose(x => x.Role, roleThree)
.All()
.Invoke(x => x.SetPassword("Password1"))
.Get();
测试发现:
1、Next方法必须在First 或者Random使用之后才能使用,并且First只能使用一次在没调用All方法之前,First、Random、Next使用完之后必须调用All方法;
2、每次只能为一个属性赋值;
mFactory = AutoPocoContainer.Configure(x =>
{
x.Conventions(c =>
{
c.UseDefaultConventions();
});
x.AddFromAssemblyContainingType<SimpleUser>();
x.Include<SimpleUser>()
.Setup(c => c.EmailAddress).Use<EmailAddressSource>()
.Setup(c => c.FirstName).Use<FirstNameSource>()
.Setup(c => c.LastName).Use<LastNameSource>()
.Invoke(c => c.SetPassword(Use.Source<String, PasswordSource>()));
x.Include<SomeType>()
.Setup(c => c.SomeString).Use<RandomStringSource>(5,10);
});
Use中的泛型就是传递给集合元素实例数据源,是个类。该类必须继承抽象泛型类DatasourceBase<T> 泛型T表示对应属性的数据类型。该抽象类中只有一个抽象方法Next,该方法就是返回数据给属性,实现给属性赋值。从而达到数据绑定;
For example
A convention to set all String EmailAddress properties to use the EmailAddressSource
public class EmailAddressPropertyConvention : ITypePropertyConvention
{
public void Apply(ITypePropertyConventionContext context)
{
context.SetSource<EmailAddressSource>();
}
public void SpecifyRequirements(ITypeMemberConventionRequirements requirements)
{
requirements.Name(x => String.Compare(x, "EmailAddress", true) == 0);
requirements.Type(x => x == typeof(String));
}
}
A convention to set all String EmailAddress fields to use the EmailAddressSource
public class EmailAddressFieldConvention : ITypeFieldConvention
{
public void Apply(ITypeFieldConventionContext context)
{
context.SetSource<EmailAddressSource>();
}
public void SpecifyRequirements(ITypeMemberConventionRequirements requirements)
{
requirements.Name(x => String.Compare(x, "EmailAddress", true) == 0);
requirements.Type(x => x == typeof(String));
}
}
x.Conventions(c => c.Register(typeof(IdPropertyConvention)));
x.AddFromAssemblyContainingType<SimpleUser>();
在context中有个Setvalue方法 ,应该是给绑定数据源传值的,测试使用的时候并没有效果,传递多个值理论是使用的数组。问题未解决。
标签:
原文地址:http://www.cnblogs.com/gsjlovenet/p/5443675.html