C# 干式实体框架搜索调用
我有一个实体框架(v5.0)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
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
supplyEventService
时,我可能会将其放入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()
);