Exemplo n.º 1
0
        /// <summary>
        /// Gets or create Singleton entity. Creates if singleton does not exist
        /// </summary>
        public T Singleton <T>() where T : class
        {
            var instance = Session.GetSingleton(typeof(T));

            if (instance != null)
            {
                return(instance as T);
            }

            instance = Make <T>();

            Session.AddSingleton(typeof(T), instance);

            return(Session.GetSingleton(typeof(T)) as T);
        }
        public object Create(object request, ISpecimenContext context)
        {
            var propertyInfo = request as PropertyInfo;
            var typeInfo     = request as TypeInfo;

            if (propertyInfo == null || ShouldReturnNoSpecimen(propertyInfo))
            {
                return(new NoSpecimen());
            }

            var propertyIsEntity = propertyInfo.PropertyType.Implements <IEntity>();

            if (propertyIsEntity)
            {
                var singleton = _session.GetSingleton(propertyInfo.PropertyType);

                if (singleton != null)
                {
                    return(singleton);
                }
            }

            var instance = _fixture.CreateByType(propertyInfo.PropertyType);

            // if (instance is OmitSpecimen)
            //     return new NoSpecimen();

            if (propertyIsEntity && instance != null && instance is not OmitSpecimen)
            {
                _session.AddSingleton(propertyInfo.PropertyType, instance);
            }

            return(instance);
        }
Exemplo n.º 3
0
        public object Create(object request, ISpecimenContext context)
        {
            var propertyInfo = request as PropertyInfo;
            var typeInfo     = request as TypeInfo;

            if (propertyInfo == null || ShouldReturnNoSpecimen(propertyInfo))
            {
                return(new NoSpecimen());
            }

            var propertyIsEntity = propertyInfo.PropertyType.Implements <IEntity>();

            if (propertyIsEntity)
            {
                var singleton = _session.GetSingleton(propertyInfo.PropertyType);

                if (singleton != null)
                {
                    return(singleton);
                }
            }

            object instance;

            // if (propertyIsEntity)
            // {
            //     MethodInfo method = _fabricator.GetType().GetMethod(nameof(Fabricator.Make));
            //     MethodInfo generic = method.MakeGenericMethod(propertyInfo.PropertyType);
            //     instance = generic.Invoke(this, null);
            // }
            // else
            // {
            instance = _fixture.CreateByType(propertyInfo.PropertyType);
            // }

            // var instance = _fabricator.Make(propertyInfo.PropertyType);

            // if (instance is OmitSpecimen)
            //     return new NoSpecimen();

            if (propertyIsEntity && instance != null && instance is not OmitSpecimen)
            {
                _session.AddSingleton(propertyInfo.PropertyType, instance);
            }

            return(instance);
        }