Wednesday, October 5, 2011

Some Resources on Patterns of Data Access with EF

Ayende's thoughts on Repository Pattern.


(I'm not convinced by this article. It talks about separation from Data Access but it doesn't look unit testable to me. Check out the fields on the BLL class Repository, how would you mock/stub that?)

This definitely more interesting than the above article, but I still can see how the Intelligent Query Pattern gives much cleaner separation of concerns. Although, I'm only about 90% there. There's still a case (in my mind at least) to still allow simple linq queries direct against EF.



A long read, but some complimentary points on EF and good insights into NHibernate.

An tiny lightweight alternative to EF PetaPoco (although it appears it relies on SQL strings - yuck).

Tuesday, October 4, 2011

Serialising a Linq Query to Json


IEnumerable<Employee> empJson = from emp in employees
                                where emp.Department == "IT Department"
                                select emp;
var ser = new DataContractJsonSerializer(typeof(IEnumerable<Employee>));
using (stream = new MemoryStream()) {
    ser.WriteObject(stream, empJson);
    string json = Encoding.Default.GetString(stream.ToArray());
    return json;
}

Friday, September 23, 2011

Resharper's Type Member Layout

I've just started using the Type Member Layout feature in Resharper, it wasn't new in Resharper 6, but I haven'd noticed it util recently. I've found Regionerate a handy tool to ensure that members in a class are sorted in a predictable order that isn't open to interpretation like sorting by flow or logical grouping. I've never been a fan of regions for that same reason, what to put in a region is largely a matter of opinion, and can make code less discoverable. But, my biggest bugbear with Regionerate is the double spacing it leaves between members despite countless hours fiddling with its config xml.

Resharper's Type Member Layout is, well, sharper.
I spent some time finding and fine tuning a 100% StyleCop compliant format here's the xml configuration for it.
The Type Member Layout configuration is actually included in the main Resharper xml configuration.

Wednesday, August 31, 2011

Handy Debugging Attributes

http://msdn.microsoft.com/en-us/library/ms228992.aspx

[DebuggerDisplay("Count = {count}")]
class MyHashtable
{
    public int count = 4;
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
public static string y = "Test String";
[DebuggerTypeProxy(typeof(HashtableDebugView))]
class MyHashtable : Hashtable
{
    private const string TestString = 
        "This should not appear in the debug window.";

    internal class HashtableDebugView
    {
        private Hashtable hashtable;
        public const string TestStringProxy = 
            "This should appear in the debug window.";

        // The constructor for the type proxy class must have a 
        // constructor that takes the target type as a parameter.
        public HashtableDebugView(Hashtable hashtable)
        {
            this.hashtable = hashtable;
        }
    }
}