繁体   English   中英

C#储存库工厂

[英]C# Repository Factory

我正在尝试提出一个工厂静态类,该类使用2个参数来初始化正确的类

1-传递数据库表名称

2-传递存储库类型(可以扩展为EF,CSV,XML,Service WCF等使用)

我编写了一个简单的控制台应用程序(请检查下面的代码-正在使用区域以提高可读性),您可以通过复制/粘贴到单个C#控制台程序来使用该应用程序。

我有2个目标:

1-在静态void Main中,您看到我正在m using dynamic run time loading of the object to call the Methods implemented by the objects Interface. I would rather call/see the methods to display after I do varName.MethodName();. I commented out the sections that I m using dynamic run time loading of the object to call the Methods implemented by the objects Interface. I would rather call/see the methods to display after I do varName.MethodName();. I commented out the sections that I m using dynamic run time loading of the object to call the Methods implemented by the objects Interface. I would rather call/see the methods to display after I do varName.MethodName();. I commented out the sections that I想使其使用//的部分,这不起作用。

2nd-第二个目标(在第一个目标完成之后:))是强烈键入传递给RepositoryFactory.GetRepository()的参数。 而不是将字符串传递给它。 在此示例中,我尝试传递枚举值,但它也不起作用。

在此先感谢您的帮助。

    using System;

namespace AppTests
{
    #region Repositories

    #region Computers
    public class RepositoryComputersSQL : IRepositoryComputers
    {
        public void GetComputers()
        {
            Console.WriteLine("Returning COMPUTERS SQL information...");
        }
    }

    public class RepositoryComputersXML : IRepositoryComputers
    {
        public void GetComputers()
        {
            Console.WriteLine("Returning COMPUTERS XML information...");
        }
    }
    #endregion

    #region Users
    public class RepositoryUsersSQL : IRepositoryUsers
    {
        public void GetUsers()
        {
            Console.WriteLine("Returning USER SQL information...");
        }
    }

    public class RepositoryUsersXML : IRepositoryUsers
    {
        public void GetUsers()
        {
            Console.WriteLine("Returning USER XML information...");
        }
    }
    #endregion

    #endregion

    #region Interfaces
    public interface IRepository
    {
    }
    public interface IRepositoryComputers : IRepository
    {
        void GetComputers();
    }
    public interface IRepositoryUsers : IRepository
    {
        void GetUsers();
    }
    #endregion

    #region Repository Factory
    public static class RepositoryFactory
    {
        public static IRepository GetRepository(string repositoryTable, string repositoryType)
        {
            IRepository results = null;

            switch (repositoryTable)
            {
                case "Computers":
                    if (repositoryType == "SQL")
                    {
                        results = new RepositoryComputersSQL();
                    }
                    else if (repositoryType == "XML")
                    {
                        results = new RepositoryComputersXML();
                    }
                    else
                        throw new ArgumentException("Invalid Repository Type");
                    break;

                case "Users":
                    if (repositoryType == "SQL")
                    {
                        results = new RepositoryUsersSQL();
                    }
                    else if (repositoryType == "XML")
                    {
                        results = new RepositoryUsersXML();
                    }
                    else
                        throw new ArgumentException("Invalid Repository Type");
                    break;

                default:
                    throw new ArgumentException("Invalid Repository Table");
            }

            return results;
        }
    }
    #endregion


    //Exe Console
    class Program
    {
        static void Main(string[] args)
        {
            // Computers
            var repoCpSql = RepositoryFactory.GetRepository("Computers", "SQL");
            //This doesn`t work
            //repoCpSql.GetComputers();
            dynamic CpSQL = repoCpSql;
            CpSQL.GetComputers();

            IRepository repoCpxMl = RepositoryFactory.GetRepository("Computers", "XML");
            //This doesn`t work
            //repoCpxMl.GetComputers();
            dynamic CpXML = repoCpxMl;
            CpXML.GetComputers();

            // Users
            IRepository repoUsSql = RepositoryFactory.GetRepository("Users", "SQL");
            //This doesn`t work
            //repoUsSql.GetComputers();
            dynamic usSQL = repoUsSql;
            usSQL.GetUsers();

            IRepository repoUsXml = RepositoryFactory.GetRepository("Users", "XML");
            //This doesn`t work
            //repoUsXml.GetComputers();
            dynamic usXML = repoUsXml;
            usXML.GetUsers();

            Console.ReadKey();
        }
    }
}

新代码审查-让我知道您的想法

using System;

namespace AppTests
{
    #region Public Enum Repository Selectors
    public enum Init
    {
        Users_SQL,
        Users_XML,

        Computers_SQL,
        Computers_XML,
    }
    #endregion

    #region Repositories

    #region Computers
    public class RepositoryComputersSQL : IComputersRepository
    {
        public void GetComputers()
        {
            Console.WriteLine("Returning COMPUTERS SQL information...");
        }
    }

    public class RepositoryComputersXML : IComputersRepository
    {
        public void GetComputers()
        {
            Console.WriteLine("Returning COMPUTERS XML information...");
        }
    }
    #endregion

    #region Users
    public class RepositoryUsersSQL : IUsersRepository
    {
        public void GetUsers()
        {
            Console.WriteLine("Returning USER SQL information...");
        }
    }

    public class RepositoryUsersXML : IUsersRepository
    {
        public void GetUsers()
        {
            Console.WriteLine("Returning USER XML information...");
        }
    }
    #endregion

    #endregion

    #region Interfaces
    public interface IRepository
    {
    }
    public interface IComputersRepository : IRepository
    {
        void GetComputers();
    }
    public interface IUsersRepository : IRepository
    {
        void GetUsers();
    }
    #endregion

    #region Repository Factory
    public static class RepositoryFactory
    {
        private enum RepositoryTable
        {
            Users,
            Computers,
        }

        private enum RepositoryType
        {
            SQL,
            XML,
        }

        private static IRepository GetRepository(RepositoryTable repositoryTable, RepositoryType repositoryType)
        {
            IRepository results = null;

            switch (repositoryTable)
            {
                case RepositoryTable.Users:
                    if (repositoryType == RepositoryType.SQL)
                        results = new RepositoryUsersSQL();
                    else if (repositoryType == RepositoryType.XML)
                        results = new RepositoryUsersXML();
                    else
                        throw new ArgumentException("Invalid Repository Type");
                    break;

                case RepositoryTable.Computers:
                    if (repositoryType == RepositoryType.SQL)
                        results = new RepositoryComputersSQL();
                    else if (repositoryType == RepositoryType.XML)
                        results = new RepositoryComputersXML();
                    else
                        throw new ArgumentException("Invalid Repository Type");
                    break;

                default:
                    throw new ArgumentException("Invalid Repository Table");
            }

            return results;
        }

        #region Public Access Repositories

        public static IRepository StartRepository(Init repositoryType)
        {
            IRepository results = null;

            switch (repositoryType)
            {
                case Init.Users_SQL:
                    results = RepositoryFactory.GetRepository(RepositoryTable.Users, RepositoryType.SQL);
                    break;
                case Init.Users_XML:
                    results = RepositoryFactory.GetRepository(RepositoryTable.Users, RepositoryType.XML);
                    break;
                case Init.Computers_SQL:
                    results = RepositoryFactory.GetRepository(RepositoryTable.Computers, RepositoryType.SQL);
                    break;
                case Init.Computers_XML:
                    results = RepositoryFactory.GetRepository(RepositoryTable.Computers, RepositoryType.XML);
                    break;
                default:
                    break;
            }
            return results;
        }

        #endregion
    }
    #endregion


    //Exe Console
    class Program
    {
        static void Main(string[] args)
        {

            IComputersRepository cp1 = (IComputersRepository)RepositoryFactory.StartRepository(Init.Computers_SQL);
            cp1.GetComputers();
            IComputersRepository cp2 = (IComputersRepository)RepositoryFactory.StartRepository(Init.Computers_XML);
            cp2.GetComputers();

            IUsersRepository us1 = (IUsersRepository)RepositoryFactory.StartRepository(Init.Users_SQL);
            us1.GetUsers();
            IUsersRepository us2 = (IUsersRepository)RepositoryFactory.StartRepository(Init.Users_XML);
            us1.GetUsers();


            Console.ReadKey();
        }
    }
}

我建议您注意以下几点:

  1. 我将它们命名为IComputerRepositoryIUserRepository而不是IRepositoryComputersIRepositoryUsers

  2. 您应该将Factory分为两种方法以返回指定的接口(这将解决您的第一个问题):

     public enum RepositoryTypes { SQL = 0, XML = 1 } public static class RepositoryFactory { public static IRepositoryComputers GetComputers(RepositoryTypes repositoryType) { if (repositoryType.Equals(RepositoryTypes.SQL)) return new RepositoryComputersSQL(); return new RepositoryComputersXML(); } public static IRepositoryUsers GetUsers(RepositoryTypes repositoryType) { if (repositoryType.Equals(RepositoryTypes.SQL)) return new RepositoryUsersSQL(); return new RepositoryUsersXML(); } } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM