干实体框架搜索调用

时间:2013-07-26 13:34:32

标签: c# asp.net-mvc-4 entity-framework-5

我有一个名为DbContext的实体框架(v5.0)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
    );
}

重新分解后的示例。

using (var db = new Entities())
{
    var activeEvents = db.Events.GetActive();
    // Or maybe it looks like db.GetActiveEvents();
    // I assume this is dictated by best practice
}

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

2 个答案:

答案 0 :(得分:2)

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

  • 表示层
    这是MVC网站(但可能是移动网站,应用程序,无论如何)
  • 服务层
    它处理Presenation层和数据层之间的调用,应用业务逻辑或任何其他可能需要的校验和(并使其远离表示层)
  • 数据层
    这里您的Entities驻留在数据库的上下文中。

为了简单起见,您可以将其保留在一个项目中,但如果这成为一个大型应用程序,您可能希望将其重构为分离的库。

现在,至于如何重构你的情况现在,我们在那里添加服务层。我喜欢使用接口,因此以后可以轻松测试,我可以实现一个&#34; dummy&#34;我进行单元测试时IWhateverService,但在运行实际应用程序时保留当前实现。然后,您实现接口以与数据接口并返回您需要的内容(或执行任何必要的操作[CRUD])。 e.g。

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);
    }
}

随着项目的发展,您可以使用CRUD操作更新IEventService(就像我之前提到的那样):

public interface IEventService
{
    IEnumerable<Event> All { get; }

    void AddOrUpdateEvent(Event event);
    IEnumerable<Event> GetActive();
    void RemoveEvent(Event event);
}

当然会将其传递给EventService,然后最终在EventsController内进行访问。

为了更进一步,你可以查看依赖注入,你指定(一次)如何构建IEventsService然后,当你需要它时,将它作为参数传递给你的控制器&# 39; s构造函数(如此):

public OtherController : Controller
{
    private readonly IUserService;
    private IEventService eventService;

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

    /* actions */
}

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

container.Register(
    Component.For<IEventService>().ImplementedBy<EventService>()
        .LifestyleSingleton()
);

基本上说,每次我需要IEventService提供EventService

答案 1 :(得分:1)

可能你可以使用部分类来实现这一点。

接口:

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

部分类:

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();
    }
}