다음 DBML 수정이 있습니다 (저는 Linq에서 DAL로 SQL을 사용하고 있습니다).일반 공장 패턴의 IRepository 패턴
public interface ILinqSQLObject { }
// these are objects from SQL Server mapped into Linq to SQL
public partial class NEWDEBT : ILinqSQLObject { }
public partial class OLDDEBT : ILinqSQLObject { }
public partial class VIPDEBT : ILinqSQLObject { }
다른 Linq 개체를보다 적절하게 조작 할 수 있습니다.
방금 IRepository 패턴을 구현했습니다.
public interface IDebtManager<T>
{
IQueryable<T> GetAllDebts();
IQueryable T GetSpecificDebt(System.Linq.Expressions.Expression<Func<T, bool>> predicate);
void Insert(T debt);
// other methods
}
public class DebtManager<T> : IDebtManager<T> where T : class, ILinqSQLObject
{
DebtContext conn = new DebtContext();
protected System.Data.Linq.Table<T> table;
public DebtManager()
{
table = conn.GetTable<T>();
}
public void Insert(T debt)
{
throw new NotImplementedException();
}
public IQueryable<T> GetSpecificDebt(System.Linq.Expressions.Expression<Func<T, bool>> predicate)
{
return table.Where(predicate);
}
public IQueryable<T> GetAllDebts()
{
return table;
}
}
그리고 완벽하게 작동합니다. 하지만 때때로 컴파일 시간에 어떤 특정 테이블을 사용할 지 모르겠습니다. 이를 위해 저는 DebtManager를위한 간단한 제네릭 팩토리를 만들려고했습니다.
public static class DebtFactoryManager
{
public static DebtManager<ILinqSQLObject> GetDebtManager(string debtType)
{
switch (debtType)
{
case "New Client":
return new DebtManager<NEWDEBT>();
case "Old Client":
return new DebtManager<OLDDEBT>();
case "VIP Client":
return new DebtManager<VIPDEBT>();
default:
return new DebtManager<NEWDEBT>();
}
return null;
}
}
그러나 작동하지 않습니다. 그것은 암시 적으로 DebtManager<NEWDEBT>
을 DebtManager<ILinqSQLObject>
으로 변환 할 수 없다고 말하지만 NEWDEBT가 ILinqSQLObject를 구현하면 왜 컴파일러는 그것을 인식하지 못합니까? 분명히 나는 약간의 실수를하고있다. 그러나 나는 그것을 볼 수 없다.
이 코드는 무엇입니까? static factory 메소드에 이름이없는 것 같습니다. –
실수로 이름을 삭제했습니다. 방금 편집했습니다. 감사합니다. – AdrianoRR