使实体框架实现一个界面 [英] Making Entity framework implement an interface

查看:117
本文介绍了使实体框架实现一个界面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用IoC与Entity框架和Ninject。我想我需要生成的实体类来实现一个接口ICRUD。有一个演练显示如何强制实体框架实现一个接口。我遵循指示,我的EntityObjectCodeGenerator.cs文件确实显示ICrud,但不实现该接口。文章说我应该在EntityObjectCodeGenerator.tt下看不到任何子类。我收到错误


'BugnetMvc.Models.BugNetEntities'
不实现接口成员
'BugnetMvc.Services。 ICrud.Update()'


更新

目标是创建一个可测试的,可扩展的MVC-3内部网,利用实体框架,还支持强类型视图和部分视图。从目前为止,我的Ninject经验很少,我相信我需要使用View的服务来重载Controller的构造函数(假设每个接口可以使用CRUD方法),每个局部视图都有一个:



EG

  public HomeController(HomeService homeCrudService,PartialViewService1 partialviewService)

Update2

要清楚并希望帮助别人,该代码可以实现如下:



这是可以如何扩展Entity

 命名空间BugnetMvc.Models //确保命名空间匹配实体
{
public partial class Milestone:ICrud< Milestone> //实体,请注意CRUD通用。这给了我们很多的灵活性使用Ninject
{
public bool Create()
{
throw new System.NotImplementedException();
}

public List<里程碑> Read()
{
var里程碑=新列表<里程碑>();

var result =从一个新的BugNetEntities1()里程碑
其中a.MilestoneID> = 0
选择新的{a.Milestone1};

里程碑= result.AsEnumerable()
。选择(o => new Models.Milestone
{
Milestone1 = o.Milestone1
}) .ToList();
返回里程碑;
}

public bool Update()
{
throw new System.NotImplementedException();
}

public bool Delete()
{
throw new System.NotImplementedException();
}
}

示例Mock实体:

 命名空间BugnetMvc.Services 
{
public class MilestoneServiceMock:ICrud< MilestoneMock>
{
public MilestoneServiceMock()
{

}

public bool Create()
{
throw新的System.NotImplementedException();
}

public bool Update()
{
throw new System.NotImplementedException();
}

public bool Delete()
{
throw new System.NotImplementedException();
}


列表< MilestoneMock> ICrud< MilestoneMock> .Read()
{
// string [] mileStones = new string [14];
列表< MilestoneMock> milestoneMocks = new List< MilestoneMock>();
milestoneMocks.Add(new MilestoneMock(New));
milestoneMocks.Add(new MilestoneMock(Assessment));
milestoneMocks.Add(new MilestoneMock(Pending Approval));
milestoneMocks.Add(new MilestoneMock(Pending Start));
milestoneMocks.Add(new MilestoneMock(Planning));
milestoneMocks.Add(new MilestoneMock(Dev-In Process));
milestoneMocks.Add(new MilestoneMock(Dev-Pending Approval to QA));
milestoneMocks.Add(new MilestoneMock(Dev-Pending Move to QA));
milestoneMocks.Add(new MilestoneMock(QA-In Process));
milestoneMocks.Add(new MilestoneMock(QA-UAT));
milestoneMocks.Add(new MilestoneMock(QA-Pending Approval to Prod));
milestoneMocks.Add(new MilestoneMock(QA-Pending Move to Prod));
milestoneMocks.Add(new MilestoneMock(On-Going));
return milestoneMocks;
}
}
}
//Global.asax
内部类SiteModule:NinjectModule
{
public override void Load()
{
bool MOCKDB = true;
MOCKDB = false;
if(MOCKDB)
{
//在这里设置ninject绑定。
绑定< ICrud< MilestoneMock>>()。到< MilestoneServiceMock>();
绑定< ICrud< Application>>()。到< ApplicationService>();
}
else
{
//在这里设置ninject绑定。
绑定< ICrud<里程碑>>()。到<里程碑>();
绑定< ICrud< Application>>()。到< ApplicationService>();
}
}
}

需要Read Id),可能会出现,在这种情况下,使用上述相同基本思想的新界面应该做到这一点。甚至可以更新ICrud以将模型类型传递到方法中。有很多选择。这对我来说很有用,感谢Jon Skeet的专家指导。

解决方案

大概是生成的实体类是部分类,正确吗?



如果是这样,您可以添加自己的部分类文件来指定要实现的接口,并提供所需的任何实际实现方法。我怀疑这将比更改生成的内容要简单得多。


I want to use IoC with Entity framework and Ninject. I figure I need the Generated Entity classes to implement an interface, ICRUD. There's a walkthrough that shows how to force Entity framework to implement an interface. I followed the directions and my EntityObjectCodeGenerator.cs file indeed shows "ICrud", but doesn't implement the interface. I don't see any subclasses under EntityObjectCodeGenerator.tt as the article says I'm supposed to. I get error

'BugnetMvc.Models.BugNetEntities' does not implement interface member 'BugnetMvc.Services.ICrud.Update()'

UPDATE
The goal is to create a testable, extensible MVC-3 intranet utilizing entity framework that also supports strongly typed Views and partial views. From my small level of experience with Ninject thus far, I believe I need to overload my Controller's constructor with a service for the View itself (Assume CRUD methods available for each interface) and one for each partial view:

E.G.

public HomeController(HomeService homeCrudService, PartialViewService1 partialviewService)

Update2
To be clear and hopefully help others, the code can be implemented as follows:

This is how one can extend Entity

namespace BugnetMvc.Models//ensure namespace matches entity
{
    public partial class Milestone : ICrud<Milestone>//Entity, note the CRUD generic.  This gives us a lot of flexibility working with Ninject
    {
        public bool Create()
        {
            throw new System.NotImplementedException();
        }

        public List<Milestone> Read()
        {
            var milestones = new List<Milestone>();

            var result = from a in new BugNetEntities1().Milestones
                            where a.MilestoneID >= 0
                            select new { a.Milestone1 };

            milestones = result.AsEnumerable()
                                        .Select(o => new Models.Milestone
                                        {
                                            Milestone1 = o.Milestone1
                                        }).ToList();
            return milestones;
        }

        public bool Update()
        {
            throw new System.NotImplementedException();
        }

        public bool Delete()
        {
            throw new System.NotImplementedException();
        }
    }

A sample Mock entity:

namespace BugnetMvc.Services
{
    public class MilestoneServiceMock : ICrud<MilestoneMock>
    {
        public MilestoneServiceMock()
        {

        }

        public bool Create()
        {
            throw new System.NotImplementedException();
        }

        public bool Update()
        {
            throw new System.NotImplementedException();
        }

        public bool Delete()
        {
            throw new System.NotImplementedException();
        }


        List<MilestoneMock> ICrud<MilestoneMock>.Read()
        {
            //string[] mileStones = new string[14];
            List<MilestoneMock> milestoneMocks = new List<MilestoneMock>();
            milestoneMocks.Add(new MilestoneMock("New"));
            milestoneMocks.Add(new MilestoneMock("Assessment"));
            milestoneMocks.Add(new MilestoneMock("Pending Approval"));
            milestoneMocks.Add(new MilestoneMock("Pending Start"));
            milestoneMocks.Add(new MilestoneMock("Planning"));
            milestoneMocks.Add(new MilestoneMock("Dev-In Process"));
            milestoneMocks.Add(new MilestoneMock("Dev-Pending Approval to QA"));
            milestoneMocks.Add(new MilestoneMock("Dev-Pending Move to QA"));
            milestoneMocks.Add(new MilestoneMock("QA-In Process"));
            milestoneMocks.Add(new MilestoneMock("QA-UAT"));
            milestoneMocks.Add(new MilestoneMock("QA-Pending Approval to Prod"));
            milestoneMocks.Add(new MilestoneMock("QA-Pending Move to Prod"));
            milestoneMocks.Add(new MilestoneMock("On-Going"));
            return milestoneMocks;
        }
    }
}
//Global.asax
        internal class SiteModule : NinjectModule
        {
            public override void Load()
            {
                bool MOCKDB = true;
                MOCKDB = false;
                if (MOCKDB)
                {
                    //Set up ninject bindings here.
                    Bind<ICrud<MilestoneMock>>().To<MilestoneServiceMock>();
                    Bind<ICrud<Application>>().To<ApplicationService>();
                }
                else
                {
                    //Set up ninject bindings here.
                    Bind<ICrud<Milestone>>().To<Milestone>();
                    Bind<ICrud<Application>>().To<ApplicationService>();
                }
            }
        }

The need for Read(int Id), may arise, in which case a new interface using the same basic ideas above should do the trick. One could even update ICrud to pass the model type into the methods as well. There's plenty of options. This worked for me, thanks to Jon Skeet for his expert guidance.

解决方案

Presumably the generated entity classes are partial classes, correct?

If so, you can just add your own partial class files to specify the interfaces to be implemented - and to provide any actual implementation methods you need. I suspect that will be a lot simpler than changing what gets generated.

这篇关于使实体框架实现一个界面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆