考虑下课
public class AccountGroup : Entity<int>
{
public AccountGroup()
{
Accounts = new HashSet<Account>();
Groups = new HashSet<AccountGroup>();
}
// option 1 - read only properties
public bool IsRoot { get { return Parent == null; } }
public bool IsLeaf { get { return !Groups.Any(); } }
public Account MainAccount { get { return Accounts.FirstOrDefault(a=>a.Type == AccountType.MainAccount); } }
// option 2 - parameter-less methods
//public bool IsRoot() { return Parent == null; }
//public bool IsLeaf() { return !Groups.Any(); }
//public Account GetMainAccount() { return Accounts.FirstOrDefault(a => a.Type == AccountType.MainAccount); }
public string Name { get; set; }
public string Description { get; set; }
public virtual ISet<Account> Accounts { get; private set; }
public virtual ISet<AccountGroup> Groups { get; private set; }
public virtual AccountGroup Parent { get; set; }
}
如果我想“丰富”上面的类,我应该使用哪种选项方法。
选项1
我是否应该使用只读参数知道 EF 不喜欢它们(尝试在 Where 子句中使用 IsRoot 会抛出 ex,与The specified type member 'IsRoot' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
选项 2
或者我应该使用无参数方法(不确定有什么缺点)
一般来说(不考虑 EF),当功能等效时,上面考虑哪种方法是首选的(即,如果我调用.IsRoot
或,我会得到相同的功能.IsRoot()
)