using hashset in entity framework

entity-framework

Question

I'm curious to know the difference between generating classes that use a "hashset" in the constructor and those that do not.

One may create models like this by using the code first technique (4.3):

public class Blog
 {
     public int Id { get; set; }
     public string Title { get; set; }
     public string BloggerName { get; set;}
     public virtual ICollection<Post> Posts { get; set; }
  }

public class Post
 {
    public int Id { get; set; }
    public string Title { get; set; }
    public DateTime DateCreated { get; set; }
    public string Content { get; set; }
    public int BlogId { get; set; }
    public ICollection<Comment> Comments { get; set; }
 }

or can produce models similar to this:

public class Customer
{
    public Customer()
    {
        BrokerageAccounts = new HashSet<BrokerageAccount>();
    }
    public int Id { get; set; }
    public string FirstName { get; set; }
    public ICollection<BrokerageAccount> BrokerageAccounts { get; set; }
}

public class BrokerageAccount
{

    public int Id { get; set; }
    public string AccountNumber { get; set; }
    public int CustomerId { get; set; }

}

Why is hashset even present?

Should the first two models also use hashset?

Is there a piece of writing that illustrates how hashset is used?

1
33
6/21/2012 6:02:09 AM

Accepted Answer

Although I'm still learning about Entity Framework, this is how I see it. Any type that implements may serve as a collection type.ICollection<T> . A HashSet, in my view, is often the semantically appropriate collection type. HashSet best reflects the rule that each member of most collections should only appear once (there shouldn't be any duplicates). I've begun writing my lessons as shown here, and thus far, it's been effective. Take note of the collection's type:ISet<T> also private is the setter.

public class Customer
{
    public Customer()
    {
        BrokerageAccounts = new HashSet<BrokerageAccount>();
    }
    public int Id { get; set; }
    public string FirstName { get; set; }
    public ISet<BrokerageAccount> BrokerageAccounts { get; private set; }
}
18
3/15/2013 1:11:37 PM

Popular Answer

In general, it's ideal to use the collection that most accurately conveys your goals. I wouldn't utilize the HashSet if you weren't going to leverage its special qualities in some way.

It is unordered and does not allow for index lookups. The fact that it enables you to add the same item several times without making duplicates is only beneficial if you have a reason to use it for that, and it is not as well suited for sequential reads as other collections. If you don't mean it to, it might disguise bad code and make it hard to pinpoint issues.

The HashSet is particularly beneficial when processing data or other scenarios when insertion and removal timings are crucial. When processing data, it is also very helpful for comparing collections of data utilizing operations like intersect, except, and union. In any other case, the disadvantages usually exceed the advantages.

Think about how inserts and deletions are rather infrequent when dealing with blog posts compared to reads, and you often want to read the data in a specified sequence nonetheless. The HashSet excels at doing the exact opposite of that, so to speak. I don't understand why you would utilize set-based operations on posts in a class like that since it is exceedingly unlikely that you would ever aim to add the same post again, for whatever reason.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow