作業単位パターンに従ってインターフェイスを作成しています。私のインターフェースは次のようになります:作業単位とEF
public interface IDataContext : IDisposable
{
void SaveChanges();
TSource Create<TSource>(TSource toCreate) where TSource : class;
TSource Update<TSource>(TSource toUpdate) where TSource : class;
bool Delete<TSource>(TSource toDelete) where TSource : class;
IQueryable<TSource> Query<TSource>();
}
これまでのところとても良いです。今私は、データプロバイダとしてEF4を使用するデータレイヤーに実装します。私は "クエリ"メソッドのためのこのコードを思いついたが、それは非常にきれいではないと思う、私はそれを行う巧妙な方法があると感じるが、私は本当に把握することはできません。
public IQueryable<TSource> Query<TSource>()
{
var type = typeof(TSource);
IQueryable item = null;
if (type == typeof(Activity)) item = _entities.ActivitySet;
if (type == typeof(Company)) item = _entities.CompanySet;
if (type == typeof(Phase)) item = _entities.PhasesSet;
if (type == typeof(Project)) item = _entities.ProjectSet;
if (type == typeof(ProjectState)) item = _entities.ProjectStateSet;
if (type == typeof(ProjectType)) item = _entities.ProjectTypeSet;
if (type == typeof(User)) item = _entities.UserSet;
if (type == typeof(UserType)) item = _entities.UserTypeSet;
if (item != null) return item as IQueryable<TSource>;
throw new NotImplementedException(string.Format("Query not implemented for type {0}", type.FullName));
}
私はここを参照してください問題は、まだ私にはかなりひどい見える場合は、他のすべてのIFSは、カスケードで、私はそれらをチェーンできalthoughtをするたびに検査を受けるです。他の問題は、追加される可能性のある新しいエンティティごとに手動で1行追加する必要があることですが、それは私の主な関心事ではありません。
誰もがこれに関する良いアドバイスをしていますか?ありがとう。
「_entities」とは何ですか?あなたは '_entities.GetEntity()'のようなものに変更できるかもしれませんが、それは何であるかによって異なります。 –
SwDevMan81
_entitiesは、Entity Frameworkの自動生成クラスです。 –