EF通过参数将包含列表发送到存储库

时间:2017-05-31 09:02:01

标签: entity-framework

此时我的存储库中已经填充了多个获取方法。

如参见。 Get1() => cxt.Entites.Include(e => e.obj1); Get2() => cxt.Entities.Include(e => e.obj1).Include(e => e.obj2)

等等。

有没有好方法,模式有一个GET方法,我可以通过参数发送包含?

2 个答案:

答案 0 :(得分:1)

我在我的项目中做了以下事情:

public Entity[] GetAll(bool includeObj1, bool includeAllOthers) {
    IQueryable<Entity> entity = ctx.Entities;

    if (includeObj1) 
        entity = entity.Include(e => e.obj1);

    if (includeAllOthers) {
        entity = entity
            .Include(e => e.obj2)
            .Include(e => e.obj3)
            .Include(e => e.obj4)
            .Include(e => e.obj5);
    }

    return entity.ToArray();
}

提供includeObj1includeObj2之类的参数将存储库的使用者与实现分开,并封装任何数据访问逻辑。
将直接“包含这些属性”命令传递到存储库意味着您知道存储库的工作原理并假设它是某种模糊抽象的ORM。

答案 1 :(得分:1)

{
  "aps": {
          "content-available": 1,
          "sound": ""
         }

}


    -(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {
NSDictionary *userInfo1 = userInfo;
NSLog(@"userInfo: %@", userInfo1);

//self.textView.text = [userInfo description];
// We can determine whether an application is launched as a result of the user tapping the action
// button or whether the notification was delivered to the already-running application by examining
// the application state.

if (application.applicationState == UIApplicationStateActive)
{
    //opened from a push notification when the app was on background

    NSLog(@"userInfoUIApplicationStateactive->%@",[userInfo objectForKey:@"aps"]);

    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Did receive a Remote Notification" message:[NSString stringWithFormat:@"Your App name received this notification while it was Running:\n%@",[[userInfo objectForKey:@"aps"] objectForKey:@"alert"]]delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];

    [alertView show];


}
else
{
    // a push notification when the app is running. So that you can display an alert and push in any view

    NSLog(@"userInfoUIApplicationStateBackground->%@",[userInfo objectForKey:@"aps"]);
      [self scheduleAlarmForDate1:[NSDate date]alarmDict:userInfo];
}
}

查看msdn中的存储库模式 您可以使用

public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {
        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList();
        }
    }

包括与lambda相同的

_sampleRepostiory.Get(h=>h.Id>1,null,"Employees.Departments");

像这样消费

public virtual IEnumerable<TEntity> Get(
            Expression<Func<TEntity, bool>> filter = null,
            Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
            Expression<Func<TEntity, object>>[] includes)
        {
            IQueryable<TEntity> query = dbSet;

            if (filter != null)
            {
                query = query.Where(filter);
            }

             if (includes != null)
           {
              query = includes.Aggregate(query, 
                  (current, include) => current.Include(include));
            }

            if (orderBy != null)
            {
                return orderBy(query).ToList();
            }
            else
            {
                return query.ToList();
            }
        }

Similar SO question