Exemple #1
0
        public void Example1Test()
        {
            SimpleFactory factory = new SimpleFactory();
            IProduct      product = factory.Create();

            Assert.AreEqual <Type>(product.GetType(), typeof(ConcreteProductA));
        }
    static void Main()
    {
        var simpleFactory = new SimpleFactory();
        var entity        = simpleFactory.Create(1);

        entity.Something();
    }
Exemple #3
0
        //工厂类用于调用者和具体实现对象的解耦
        static void Main(string[] args)
        {
            //Simple Factory
            IPhone phone = SimpleFactory.Create <IMiPhone>();

            Console.WriteLine(phone.Name);

            phone = SimpleFactory.Create <IHWPhone>();
            Console.WriteLine(phone.Name);

            //Factory Method
            FactroyMethodBase method = new MiPhoneFactoryMethod();

            Console.WriteLine(method.CreatePhone().Name);

            method = new HwPhoneFactoryMethod();
            Console.WriteLine(method.CreatePhone().Name);

            //Virtual
            VirtualFactoryBase factory = new XiaoMiFactory();

            Console.WriteLine(factory.CreatePhone().Name);
            Console.WriteLine(factory.CreateTV().Name);

            factory = new HWFactory();
            Console.WriteLine(factory.CreatePhone().Name);
            Console.WriteLine(factory.CreateTV().Name);

            Console.ReadKey();
        }
 public void Accepts_custom_action()
 {
     var factory = new SimpleFactory<Rabbit>(x => x.Age = 3);
     var product = factory.Create(x => x.Name = "Fluffy");
     Assert.Equal(3, product.Age);
     Assert.Equal("Fluffy", product.Name);
 }
 public void Can_handle_null_custom_action()
 {
     var factory = new SimpleFactory<Rabbit>();
     Rabbit product = null;
     Assert.DoesNotThrow(() =>
     {
         product = factory.Create(null);
     });
     Assert.NotNull(product);
 }
        static void Main(string[] args)
        {
            Console.WriteLine("=======Start===========");
            try
            {
                {
                    CantoneseChicken cantoneseChicken = new CantoneseChicken();
                    cantoneseChicken.Chief        = "CantoneseChief";
                    cantoneseChicken.Leeks        = "CantonLeeks";
                    cantoneseChicken.Ginger       = "CantonGinger";
                    cantoneseChicken.ChickenDices = "CantonChicken";
                    Display <CantoneseChicken>(cantoneseChicken);
                    cantoneseChicken.ShowUniSkill();
                    cantoneseChicken.StirFry += cantoneseChicken.PutGingerIn;
                    cantoneseChicken.StirFry += cantoneseChicken.PutLeeksIn;
                    cantoneseChicken.StirFry += cantoneseChicken.PutChickenIn;
                    cantoneseChicken.StirFry += () => { Console.WriteLine("Put sugar in."); };
                    cantoneseChicken.SetOilTemperature(110);
                }
                {
                    ShandongChicken shandongChicken = new ShandongChicken();
                    shandongChicken.Chief        = "ShandongChief";
                    shandongChicken.Leeks        = "shandongLeeks";
                    shandongChicken.Ginger       = "CantonGinger";
                    shandongChicken.ChickenDices = "CantonChicken";
                    Display <ShandongChicken>(shandongChicken);
                    shandongChicken.ShowUniSkill();
                    shandongChicken.ThickenJuices += () => Console.WriteLine("Make thicken juices");
                    shandongChicken.ShandongCooking();
                    //shandongChicken.StirFry += shandongChicken.PutGingerIn;
                    //shandongChicken.StirFry += shandongChicken.PutLeeksIn;
                    //shandongChicken.StirFry += shandongChicken.PutChickenIn;
                    //shandongChicken.StirFry += () => { Console.WriteLine("Put sugar in."); };
                    //shandongChicken.SetOilTemperature(200);
                }

                {
                    //Create a new ShandongChicken instance from a json file.
                    Console.WriteLine("==Create a new shandong chicken by simple factory==");
                    ShandongChicken shandongChicken = SimpleFactory.Create <ShandongChicken>();
                    Display <ShandongChicken>(shandongChicken);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }

            Console.Read();
        }
        /// <summary>
        /// 生成代码文本集合
        /// </summary>
        /// <param name="table">表名</param>
        /// <param name="namespacePfx">命名空间前辍</param>
        /// <param name="type">类型</param>
        /// <param name="fileNames">文件名集合</param>
        /// <returns>代码文本集合</returns>
        protected override string[] BuilderCodeTexts(TableInfo table, string namespacePfx, string type, out string[] fileNames)
        {
            string name        = $"{table.Name.FristUpper()}Info";
            string parentClass = "UserInfo".Equals(name) ? "BasicUserInfo" : "PersonTimeInfo";

            EnumGenerator enumGenerator = new EnumGenerator();

            fileNames = new string[] { $"{name}.cs" };
            StringBuilder propCode = new StringBuilder();

            ITypeMapperService typeMapper = SimpleFactory.Create(type);

            if (!table.Columns.IsNullOrCount0())
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    ColumnInfo c        = table.Columns[i];
                    string     propName = c.Name.FristUpper();
                    if (IGNORE_PROP_NAMES.Contains(propName))
                    {
                        continue;
                    }

                    if ("UserInfo".Equals(name))
                    {
                        if (USER_IGNORE_PROP_NAMES.Contains(propName))
                        {
                            continue;
                        }
                    }

                    string      propType = typeMapper.GetPropertyType(c);
                    CommentInfo comment  = null;
                    if (!string.IsNullOrWhiteSpace(c.Description))
                    {
                        try
                        {
                            comment = JsonUtil.Deserialize <CommentInfo>(c.Description);
                            if (comment != null && comment.Enum != null)
                            {
                                propType = enumGenerator.BuilderCodeText(comment.Enum, namespacePfx);
                            }
                        }
                        catch { }
                    }

                    string        commentDesc = null;
                    StringBuilder attrCode    = new StringBuilder();
                    if (!c.IsNull)
                    {
                        attrCode.Append(GetAttrCode("Required"));
                        attrCode.AppendLine();
                    }
                    if (c.Length != null && "string".Equals(propType))
                    {
                        attrCode.Append(GetAttrCode(string.Format("MaxLength({0})", c.Length)));
                        attrCode.AppendLine();
                    }
                    if (comment != null)
                    {
                        if (string.IsNullOrWhiteSpace(comment.Desc))
                        {
                            commentDesc = c.Description;
                        }
                        else
                        {
                            commentDesc = comment.Desc;
                        }

                        if (!string.IsNullOrWhiteSpace(comment.Name))
                        {
                            attrCode.Append(GetAttrCode(string.Format("DisplayName(\"{0}\")", comment.Name)));
                            attrCode.AppendLine();
                        }

                        if (comment.MinLength != null && "string".Equals(propType))
                        {
                            attrCode.Append(GetAttrCode(string.Format("MinLength({0})", comment.MinLength)));
                            attrCode.AppendLine();
                        }

                        if (comment.Range != null && comment.Range.Length == 2)
                        {
                            attrCode.Append(GetAttrCode(string.Format("Range({0}, {1})", comment.Range[0], comment.Range[1])));
                            attrCode.AppendLine();
                        }
                    }
                    else
                    {
                        commentDesc = c.Description;
                    }

                    if (string.IsNullOrWhiteSpace(commentDesc))
                    {
                        commentDesc = propName;
                    }

                    propCode.Append(PropertyTemplate
                                    .Replace("|Description|", commentDesc)
                                    .Replace("|JsonName|", c.Name.FristLower())
                                    .Replace("|Attribute|", attrCode.ToString())
                                    .Replace("|Type|", propType)
                                    .Replace("|Name|", propName)
                                    .Replace("|Order|", (i + 1).ToString()));

                    if (i == table.Columns.Count - 1)
                    {
                        continue;
                    }

                    propCode.AppendLine();
                    propCode.AppendLine();
                }
            }

            var desc = string.IsNullOrWhiteSpace(table.Description) ? name : table.Description;

            return(new string[]
            {
                ClassTemplate
                .Replace("|NamespacePfx|", namespacePfx)
                .Replace("|Description|", desc)
                .Replace("|Name|", name)
                .Replace("|Inherit|", parentClass)
                .Replace("|Property|", propCode.ToString())
            });
        }
Exemple #8
0
 public ServiceResult() {
     Errors = SimpleFactory<List<string>>.Create();
 }
 public void Can_build_product()
 {
     var factory = new SimpleFactory<Rabbit>();
     var product = factory.Create();
     Assert.NotNull(product);
 }