我应该将托管对象上下文作为方法的参数包含在内吗?

发布于 2024-10-10 21:13:42 字数 1412 浏览 3 评论 0原文

问题

当我使用 Core Data 时,我将托管对象上下文作为方法的参数包含在内。

虽然这使得代码更容易测试,但它很混乱。

问题

  • 这是好还是坏做法?
  • 有没有一种更简洁、更简单的方法来保持方法的可测试性?

后台

下面的示例是一个具有自己的上下文的后台操作。

来自更有经验的编码人员的任何建议将不胜感激!

代码

@interface JGTrainingGroupCleanupOperation : JGCoreDataOperation {
    NSManagedObjectContext  *imoc;
}

...

@implementation JGTrainingGroupCleanupOperation

-(void)main {
    [self startOperation];                    // Setting up the local context etc
    [self cleanupTrainingGroupsInMOC:imoc];   
    [self finishOperation];
}

-(void)cleanupTrainingGroupsInMOC:(NSManagedObjectContext *)moc {
    NSSet *trainedGroups = [self fetchAllTrainedGroupsInMOC:moc];

    [self deleteDescendantsOfGroups:trainedGroups fromMOC:moc];
    [self removeStubAncestorsOfGroups:trainedGroups fromMOC:moc];    
}

-(NSSet *)fetchAllTrainedGroupsInMOC:(NSManagedObjectContext *)moc_ {
    return [moc_ fetchObjectsForEntityName:kTrainingGroup withPredicate:[NSPredicate predicateWithFormat:@"projectEditedAtTopLevel == nil"]];
}

-(void)deleteDescendantsOfGroups:(NSSet *)trainedGroups fromMOC:(NSManagedObjectContext *)moc_ {
    // More code here
}

-(void)deleteDescendantsOfGroup:(JGTrainingGroup *)trainedGroup fromMOC:(NSManagedObjectContext *)moc_ {
    // More code here
}

Problem

I'm including the managed object context as a parameter of a method when I work with Core Data.

Although this makes the code easier to test, it's messy.

Questions

  • Is this good or bad practice?
  • Is there a neater, easier way of doing this that keeps methods testable?

Background

The example below is a background operation that has it's own context.

Any advice from more experienced coders would be much appreciated!

Code

@interface JGTrainingGroupCleanupOperation : JGCoreDataOperation {
    NSManagedObjectContext  *imoc;
}

...

@implementation JGTrainingGroupCleanupOperation

-(void)main {
    [self startOperation];                    // Setting up the local context etc
    [self cleanupTrainingGroupsInMOC:imoc];   
    [self finishOperation];
}

-(void)cleanupTrainingGroupsInMOC:(NSManagedObjectContext *)moc {
    NSSet *trainedGroups = [self fetchAllTrainedGroupsInMOC:moc];

    [self deleteDescendantsOfGroups:trainedGroups fromMOC:moc];
    [self removeStubAncestorsOfGroups:trainedGroups fromMOC:moc];    
}

-(NSSet *)fetchAllTrainedGroupsInMOC:(NSManagedObjectContext *)moc_ {
    return [moc_ fetchObjectsForEntityName:kTrainingGroup withPredicate:[NSPredicate predicateWithFormat:@"projectEditedAtTopLevel == nil"]];
}

-(void)deleteDescendantsOfGroups:(NSSet *)trainedGroups fromMOC:(NSManagedObjectContext *)moc_ {
    // More code here
}

-(void)deleteDescendantsOfGroup:(JGTrainingGroup *)trainedGroup fromMOC:(NSManagedObjectContext *)moc_ {
    // More code here
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜未央樱花落 2024-10-17 21:13:42

以我(不那么谦虚)的观点,我想说这主要是风格问题。您可以这样做,也可以@synthesize moc 并调用[self moc]self.moc

我?我个人会选择访问器路线,主要是因为无论如何都不应该告诉类成员在哪里找到 iVar 取消引用的对象。如果您要访问同一类中的 iVar,我会直接使用 iVar 或访问器。

我相信性能差异可以忽略不计,所以我不会在这方面太费心(即使你没有问)。

In my (not so humble) opinion I'd say it's mostly a matter of style. You can do it this way or you can @synthesize the moc and call [self moc] or self.moc.

Me? I'd go the accessor route personally, mostly because class members shouldn't have to be told where to find an object dereferenced by an iVar anyway. If you're accessing something that's an iVar within the same class, I'd use the iVar directly or an accessor.

I believe the difference in performance would be negligible, so I wouldn't really bother much on that front (even though you didn't ask).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文