readonly-collection

Read-only list or unmodifiable list in .NET 4.0

筅森魡賤 提交于 2019-11-28 06:39:41
From what I can tell, .NET 4.0 still lacks read-only lists. Why does the framework still lack this functionality? Isn't this one of the commonest pieces of functionality for domain-driven design ? One of the few advantages Java has over C# is this in the form of the Collections.unmodifiablelist(list) method, which it seems is long overdue in IList<T> or List<T>. Using IEnumerable<T> is the easiest solution to the question - ToList can be used and returns a copy. LukeH You're looking for ReadOnlyCollection , which has been around since .NET2. IList<string> foo = ...; // ... ReadOnlyCollection

Entity Framework read only collections

梦想与她 提交于 2019-11-27 08:20:01
Consider a domain where a Customer, Company, Employee, etc, etc, have a ContactInfo property which in turn contains a set of Address(es), Phone(es), Email(s), etc, etc... Here is my abbreviated ContactInfo: public class ContactInfo : Entity<int> { public ContactInfo() { Addresses = new HashSet<Address>(); } public virtual ISet<Address> Addresses { get ; private set; } public Address PrimaryAddress { get { return Addresses.FirstOrDefault(a => a.IsPrimary); } } public bool AddAddress(Address address) { // insure there is only one primary address in collection if (address.IsPrimary) { if

Read-only list or unmodifiable list in .NET 4.0

不打扰是莪最后的温柔 提交于 2019-11-27 01:26:02
问题 From what I can tell, .NET 4.0 still lacks read-only lists. Why does the framework still lack this functionality? Isn't this one of the commonest pieces of functionality for domain-driven design? One of the few advantages Java has over C# is this in the form of the Collections.unmodifiablelist(list) method, which it seems is long overdue in IList<T> or List<T>. Using IEnumerable<T> is the easiest solution to the question - ToList can be used and returns a copy. 回答1: You're looking for

Entity Framework read only collections

痴心易碎 提交于 2019-11-26 14:07:25
问题 Consider a domain where a Customer, Company, Employee, etc, etc, have a ContactInfo property which in turn contains a set of Address(es), Phone(es), Email(s), etc, etc... Here is my abbreviated ContactInfo: public class ContactInfo : Entity<int> { public ContactInfo() { Addresses = new HashSet<Address>(); } public virtual ISet<Address> Addresses { get ; private set; } public Address PrimaryAddress { get { return Addresses.FirstOrDefault(a => a.IsPrimary); } } public bool AddAddress(Address

ReadOnlyCollection or IEnumerable for exposing member collections?

ε祈祈猫儿з 提交于 2019-11-26 12:03:15
Is there any reason to expose an internal collection as a ReadOnlyCollection rather than an IEnumerable if the calling code only iterates over the collection? class Bar { private ICollection<Foo> foos; // Which one is to be preferred? public IEnumerable<Foo> Foos { ... } public ReadOnlyCollection<Foo> Foos { ... } } // Calling code: foreach (var f in bar.Foos) DoSomething(f); As I see it IEnumerable is a subset of the interface of ReadOnlyCollection and it does not allow the user to modify the collection. So if the IEnumberable interface is enough then that is the one to use. Is that a proper