Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 干式实体框架搜索调用_C#_Asp.net Mvc 4_Entity Framework 5 - Fatal编程技术网

C# 干式实体框架搜索调用

C# 干式实体框架搜索调用,c#,asp.net-mvc-4,entity-framework-5,C#,Asp.net Mvc 4,Entity Framework 5,我有一个实体框架(v5.0)DbContext,名为Entities,它是由框架自动生成的(我认为我的设计模式是数据库优先) 我在整个应用程序中调用了这个搜索命令(如下面的代码段所示)。我想干掉我的控制器代码,并将其重新考虑到方法调用中 using (var db = new Entities()) { DateTime now = DateTime.Today; var activeEvents = db.Events.Where( b => !b.rem

我有一个实体框架(v5.0)
DbContext
,名为
Entities
,它是由框架自动生成的(我认为我的设计模式是数据库优先)

我在整个应用程序中调用了这个搜索命令(如下面的代码段所示)。我想干掉我的控制器代码,并将其重新考虑到方法调用中

using (var db = new Entities())
{
    DateTime now = DateTime.Today;
    var activeEvents = db.Events.Where(
        b => !b.removed &&
        b.start_date <= now &&
        b.end_date >= now
    );
}

如何实现这一目标?什么是最佳实践?

可能是您可以使用分部类来实现这一点

接口:

interface IMethods<T>
{
    IEnumerable<T> ActiveItems();
}

我可能会采取服务的方式;您使用三个主要组件构建站点:

  • 表示层
    这是MVC网站(但可以是移动网站和应用程序,无论什么)
  • 服务层
    这将处理表示层和数据层之间的调用,应用业务逻辑或任何其他可能需要的校验和(并使其远离表示层)
  • 数据层
    这里是您的
    实体
    所在的位置和数据库的上下文
为了简单起见,您可以将其保留在一个项目中,但如果它成为一个大型应用程序,您可能希望将其重构到单独的库中

现在,关于如何重构您的情况,我们在其中添加了服务层。我喜欢使用接口,这样以后就可以很容易地进行测试。当我进行单元测试时,我可以实现一个“虚拟的”
IWhateverService
,但在运行实际应用程序时保持当前的实现。然后实现接口与数据接口,并返回所需内容(或执行任何必要的操作[CRUD])。e、 g

随着项目的发展,您可以使用CRUD操作更新
IEventService
(如我前面提到的):

然后,您可以使用Castle Windsor、ninject或任何其他解决方案,这些解决方案将涉及到这些接口的单个映射,然后(神奇地)将它们提供给控制器的构造函数以供使用。举个例子,下面是Castlewindsor配置:

container.Register(
    Component.For<IEventService>().ImplementedBy<EventService>()
        .LifestyleSingleton()
);
container.Register(
Component.For().ImplementedBy()实现
.生活方式单身()
);

本质上说,每当我需要
IEventService
supply
EventService

时,我可能会将其放入
IEventService
,使用DI将其推入ctor,只需调用
var-activeEvents=this.eventsService.GetActive();-)@BradChristie我是C#编程语言的新手。你说的话对我来说像是一门外语。您可以详细说明一下吗?您有数据上下文,然后您有一个位于该上下文之上的服务,并管理对数据库的调用和来自数据库的调用,然后您的MVC操作使用该服务进行调用。DO容器只是使添加对该服务的引用变得更容易,但您也可以同样容易地在controller ctor中添加
this.eventService=new eventService(new Entities())类似这样的内容:您是否最终使用服务模型将其连接起来?我不知道为什么客户一直拒绝对您的代码进行编辑。但是,如果不在
的Where
调用中添加.ToEnumerable(),此方法将不起作用。
partial class Event :IMethods<Event>
{
    public IEnumerable<Event> ActiveItems()
    {
        try
        {
            using (var db = new Entities())
            {
                DateTime now = DateTime.Today;
                return db.Events.Where(b => !b.removed && b.start_date <= now && b.end_date >= now);
            }
        }
        catch (Exception)
        {
            return null;
        }

    }
}
 public class TestClass
{
    public void MyTest()
    {
        var ativevnts = new Event().ActiveItems();
    }
}
public interface IEventService
{
    IEnumerable<Event> GetActive();
}

public class EventService : IEventService
{
    private readonly Entities entities;
    public EventService(Entities entities)
    {
        this.entities = entities;
    }
    public IEnumerable<Event> GetActive()
    {
        DateTime now = DateTime.Today;
        return this.entities.Events
            .Where(x => !x.removed)
            .Where(x => x.start_date <= now && x.end_date >= now)
            .AsEnumerable();
    }
}
public class EventsController : Controller
{
    private readonly IEventService eventService;

    public EventsService()
    {
        this.eventsService = new EventsService(new Entities());
    }

    // action that gets and views the active events
    public ActionResult Active()
    {
        var activeEvents = this.eventsService.Getactive();
        return View(activeEvents);
    }
}
public interface IEventService
{
    IEnumerable<Event> All { get; }

    void AddOrUpdateEvent(Event event);
    IEnumerable<Event> GetActive();
    void RemoveEvent(Event event);
}
public OtherController : Controller
{
    private readonly IUserService;
    private IEventService eventService;

    public OtherController(IUserService userService, IEventService eventService)
    {
        this.userService = userService;
        this.eventService = eventService;
    }

    /* actions */
}
container.Register(
    Component.For<IEventService>().ImplementedBy<EventService>()
        .LifestyleSingleton()
);