예제 #1
0
 public object Build(Type type)
 {
     if (type == null)
     {
         throw new ArgumentNullException("type");
     }
     Sealed();
     using (var session = new BuildSession(this, _session, _random))
     {
         var obj = session.Build(type);
         foreach (var hook in PostBuildHooks)
         {
             hook(session);
         }
         return(obj);
     }
 }
예제 #2
0
        public IGenerator GetGenerator(Type type, BuildSession session)
        {
            if (type == null)
            {
                throw new ArgumentNullException("type");
            }
            if (session == null)
            {
                throw new ArgumentNullException("session");
            }
            var thisGenerator = _instancesGenerator.GeneratesType(type, session.Builder, session) ? _instancesGenerator : _generators.LastOrDefault(g => g.GeneratesType(type, session.Builder, session));

            if (thisGenerator == null && _parent != null)
            {
                thisGenerator = _parent.GetGenerator(type, session);
            }
            return(thisGenerator);
        }
예제 #3
0
 public BuildSession(IConfiguredBuilder builder, BuildSession parent, Random random)
 {
     _builder        = builder;
     _wrappedBuilder = new SessionedBuilder(builder, this);
     if (parent != null && !parent.IsDisposed)
     {
         _sessionId        = parent._sessionId;
         _random           = parent._random;
         _constructedNodes = parent._constructedNodes;
         _objectTreeRoot   = parent._objectTreeRoot;
         _memberStack      = parent._memberStack;
         CurrentObject     = parent.CurrentObject;
     }
     else
     {
         _sessionId        = random.Next();
         _random           = random;
         _constructedNodes = new List <ObjectBuildTreeEntry>();
         _objectTreeRoot   = new ObjectBuildTreeEntry(null, null, -1);
         _memberStack      = new Stack <MemberInfo>();
         CurrentObject     = _objectTreeRoot;
     }
 }
예제 #4
0
 protected Builder(int seed, Builder parent, BuildSession session)
 {
     _seed            = seed;
     _session         = session;
     _random          = new Random(seed);
     _maxInstances    = new TypeRegistry <int?>();
     _collectionSizes = new TypeRegistry <Range>();
     _ignoreUnset     = new Dictionary <Type, bool>();
     _generators      = new List <IGenerator>();
     _postbuildHooks  = new List <Action <BuildSession> >();
     _parent          = parent;
     if (_parent != null)
     {
         _instancesGenerator = _parent._instancesGenerator;
         _allMaxInstances    = new InheritedTypeRegistry <int?>(_parent._allMaxInstances, _maxInstances);
         _allCollectionSizes = new InheritedTypeRegistry <Range>(_parent._allCollectionSizes, _collectionSizes);
     }
     else
     {
         _instancesGenerator = new DefaultReusableInstancesGenerator();
         _allMaxInstances    = new InheritedTypeRegistry <int?>(null, _maxInstances);
         _allCollectionSizes = new InheritedTypeRegistry <Range>(null, _collectionSizes);
     }
 }
예제 #5
0
 public IConfiguredBuilder CreateNew(BuildSession session)
 {
     return(new Builder(this._seed + 1, this, session));
 }
예제 #6
0
 public static TType FindOrBuildParent <TType>(object obj, IBuilder builder, BuildSession session)
     where TType : class
 {
     return(FindParent <TType>(obj, builder, session) ?? builder.Build <TType>());
 }
예제 #7
0
 public IBuilder CreateNew(BuildSession session)
 {
     return(_parent.CreateNew(session));
 }