Entity framework 4 如果使用IObjectSet,如何在ObjectContext类中使用方法<;T>;用于单元测试?

Entity framework 4 如果使用IObjectSet,如何在ObjectContext类中使用方法<;T>;用于单元测试?,entity-framework-4,Entity Framework 4,我正在将EF4与POCO一起使用,并试图使其成为可测试的体系结构。 因此,我创建IObjectContext接口如下: public interface IObjectContext { IObjectSet<Employee> Employees { get; } IObjectSet<Team> Teams { get; } void Commit(); } public partial class H

我正在将EF4与POCO一起使用,并试图使其成为可测试的体系结构。 因此,我创建IObjectContext接口如下:

public interface IObjectContext
    {
        IObjectSet<Employee> Employees { get; }
        IObjectSet<Team> Teams { get; }
        void Commit();
    }
public partial class HRManagementEntities : ObjectContext, IUnitOfWork
    {
        // skip some codes here...........

        public IObjectSet<Employee> Employees
        {
            get { return _employees  ?? (_employees = CreateObjectSet<Employee>("Employees"));         
        }
        private IObjectSet<Employee> _employees;

        public IObjectSet<Team> Teams
        {
            get { return _teams  ?? (_teams = CreateObjectSet<Team>("Teams")); }
        }
        private IObjectSet<Team> _teams;

        public void Commit()
        {
            SaveChanges();
        }
}
公共接口IObjectContext
{
IObjectSet雇员{get;}
IObjectSet团队{get;}
无效提交();
}
然后,我将real ObjectContext类中的属性类型更改为IObjectSet,如下所示:

public interface IObjectContext
    {
        IObjectSet<Employee> Employees { get; }
        IObjectSet<Team> Teams { get; }
        void Commit();
    }
public partial class HRManagementEntities : ObjectContext, IUnitOfWork
    {
        // skip some codes here...........

        public IObjectSet<Employee> Employees
        {
            get { return _employees  ?? (_employees = CreateObjectSet<Employee>("Employees"));         
        }
        private IObjectSet<Employee> _employees;

        public IObjectSet<Team> Teams
        {
            get { return _teams  ?? (_teams = CreateObjectSet<Team>("Teams")); }
        }
        private IObjectSet<Team> _teams;

        public void Commit()
        {
            SaveChanges();
        }
}
公共部分类HRManagementEntities:ObjectContext,IUnitOfWork
{
//跳过此处的一些代码。。。。。。。。。。。
公共对象集雇员
{
获取{return\u employees???(\u employees=CreateObjectSet(“employees”);
}
私人IObjectSet_员工;
公共对象集团队
{
获取{return\u teams???(\u teams=CreateObjectSet(“teams”);}
}
私人IObjectSet团队;
公共无效提交()
{
保存更改();
}
}
在我的服务层中,按如下方式使用EF:

public class Service
    {
         private IObjectContext ctx;

         public HRService(IObjectContext ctx)
         {
            this.ctx = ctx;
         }

         public List<Team> GetAllTeams()
         {
            return ctx.Teams.ToList();
         }
    }
公共类服务
{
私有IObjectContext ctx;
公共人力资源服务(IObjectContext ctx)
{
this.ctx=ctx;
}
公共列表GetAllTeams()
{
返回ctx.Teams.ToList();
}
}
这是我的问题,如何在ObjectContext中调用方法,例如ApplyCurrentValues()、ExecuteStoreCommand()等等

我是否需要在IObjectContext中实现这些方法才能使用

编辑

根据RPM的建议,我为ApplyCurrentValues()方法创建了以下扩展方法,其他方法也可以以同样的方式进行扩展

public static T UpdateModel<T>(this IObjectSet<T> iObjectSet, T currentEntity) where T : class
        {
            ObjectSet<T> objectSet = iObjectSet as ObjectSet<T>;
            if (objectSet == null || currentEntity == null)
            throw new ArgumentNullException();
            return objectSet.ApplyCurrentValues(currentEntity);
        }
public static T UpdateModel(此IObjectSet IObjectSet,T currentEntity),其中T:class
{
ObjectSet ObjectSet=IOObjectSet作为ObjectSet;
if(objectSet==null | | currentEntity==null)
抛出新ArgumentNullException();
返回objectSet.ApplyCurrentValues(currentEntity);
}

您需要为所需的方法创建扩展方法,并将
ioobjectset
强制转换为
ObjectSet

例如,如果需要执行
.Include
(即时加载),请使用以下扩展方法:

public static IQueryable<TSource> Include<TSource>(this IQueryable<TSource> source, string path)
    {
        var objectQuery = source as ObjectQuery<TSource>;

        if (objectQuery != null)
        {
            objectQuery.Include(path);
        }

        return source;
    }
公共静态IQueryable包含(此IQueryable源,字符串路径)
{
var objectQuery=源作为objectQuery;
if(objectQuery!=null)
{
objectQuery.Include(路径);
}
返回源;
}
对于
IObjectContext
,您可能也可以做同样的事情,但不知道为什么要模仿它。您不应该将OC暴露在存储库之外,只有存储库应该知道OC

在您的示例中,您的服务正在实体/存储库上调用ctx.Teams

在我看来,您的服务应该调用ctx.Find,它将是团队对象上下文的强类型(通过泛型)

一个常见的陷阱是过度模仿。不要为了模仿而模仿一切,要模仿单元测试所需的功能


如果你想抽象出对象上下文,那么就使用工作单元模式。

事实上,由于我的团队成员根本不熟悉ORM,我打算让架构尽可能简单。这就是为什么我尝试使用ObjectContext作为工作单元而不使用存储库模式的原因。你给我展示的代码我已经尝试过了,但是当它出现时谈到IObjectContext,如果不使用存储库模式,就找不到任何解决方案。如果可能的话,你能给我看一些示例代码吗?你可以让你的体系结构变得简单,但拥有
公共接口IObjectContext
并不是IMO的发展方向。我不是说你需要一个疯狂/健壮的存储库,一个有met的简单存储库hods
public ICollection GetTeams
就可以了。我的观点是,调用像
ApplyCurrentValues()、ExecuteStoreCommand()这样的方法,
应该是DAL做出的决定,而不是你的服务-因此没有理由通过
IObjectContext
界面公开该方法。知道我的意思吗?另外,看看你的代码,
service
几乎就是一个存储库。我会将
HRService
重命名为
HRRepository
,并更改
IObjectContext
ObjectContext
。这是这里最简单的解决方案。