我试图想出一个工厂静态类,它使用2个参数启动正确的类
1 - 传递数据库表名称
2 - 传递存储库类型(可以扩展为EF,CSV,XML,Service WCF等使用...)
我写了一个简单的控制台应用程序(检查下面的代码 - 使用区域以提高可读性),您可以通过复制/粘贴到单个C#控制台程序来使用它。
我有两个目标:
1st - 在静态虚空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
试图使其与名称一起使用//这不起作用。
第二个 - 第二个目标(在完成第一个目标之后:))是强烈输入传递给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();
}
}
}
答案 0 :(得分:0)
我建议你做的一些事情:
我会将IRepositoryComputers
和IRepositoryUsers
命名为IComputerRepository
和IUserRepository
,而不是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();
}
}
和<?xml version="1.0" encoding="UTF-8"?>
<actor-template default-chain-id="claimCoupon"
xsi:noNamespaceSchemaLocation="http://www.atg.com/xsds/actorChain_1.0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<actor-chain id="claimCoupon" transaction="TX_SUPPORTS">
<form id="claimCoupon" handle="claimCoupon" var="claimCoupon" name="/atg/store/order/purchase/CouponFormHandler"
use-forwards="true" requires-session-confirmation="true">
<input name="couponCode" value="${param.couponCode}"/>
<input name="applyCouponErrorURL" value="/model/atg/userprofiling/ProfileActor/error"/>
<input name="applyCouponSuccessURL" value="/model/atg/commerce/promotion/CouponActor/claimCoupon-success"/>
</form>
</actor-chain>
<actor-chain id="claimCoupon-success" transaction="TX_SUPPORTS">
<output id="status" name="status" value="Success" />
</actor-chain>
</actor-template>
。
您应该将Factory分成两个方法来返回指定接口(这将解决您的第一个问题):
mov ax,1
AgainA:
...
inc ax
cmp ax,1000
jbe AgainA