SkillAgentSearch skills...

Ignorance

A little Persistence Ignorance framework that features those Unit of Work, Repository, and Inversion of Control patterns you keep hearing so much about.

Install / Use

/learn @jonfazzaro/Ignorance
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

ignorance

<h1>A little <strong>Persistence Ignorance</strong> framework that features those <em>Unit of Work</em>, <em>Repository</em>, and <em>Inversion of Control</em> patterns you keep hearing so much about.</h1> <p>Only, now you're not pecking them out and maintaining them in your application code, because <strong><em>Ignorance</em> already did that</strong>.</p> <p>So, in much the same way that your business logic will be carefree and clueless about how your data is persisted, <em>you</em> get to skip down the hallway and be all, &quot;la la, business code, business code&quot;, and not give two squirts about implementing another <em>IRepository</em>.</p> <h2>You Know I'm Not Switching To Another ORM Tomorrow, Right?</h2> <p>Yep. Because that's exactly what I said.</p> <p>But let's say you have this amazing web app, and now you want to build out a occasionally-connected native client for it. Well, now your logic needs to work with local storage. And that local storage<a href="http://msdn.microsoft.com/en-us/library/hh202860(v=VS.92).aspx" target="_blank"> doesn't do Entity Framework</a>. If your domain and client logic is peppered with Entity Framework-specific hooks, you can't just reuse that DLL. That DLL that already contains your carefully curated and tested and trusted domain logic. That DLL that is the<em> heart and identity of your application</em>.</p> <p><em>Persistence Ignorance</em> makes that logic portable, self-contained, instantly reusable. What's more, it's dead simple to test what's unique and important about your app. Mock up your database, don't muck up your database.</p> <h2>Just Show Me The Code</h2> <p>Okay. So your client code will be all:</p> <pre style="background: white"><span style="font-family: consolas"><span><span style="color: #008000"><span>// hey kids, let's add a new contact&#160; </span></span></span><span><br /><span><span style="color: #0000ff">using</span></span> (<span><span style="color: #0000ff">var</span></span> work = Ignorance.<span><span style="color: #2b91af">Create</span></span>.Work())<br />{<br />&#160;&#160;&#160; <span><span style="color: #008000">// create a service object,&#160;&#160; </span></span><br />&#160;&#160;&#160; <span><span style="color: #008000">// which houses any logic around data types&#160; </span></span><br />&#160;&#160;&#160; <span><span style="color: #0000ff">var</span></span> contacts = <span><span style="color: #0000ff">new</span></span>&#160;<span><span style="color: #2b91af">ContactService</span></span>(work);<br /> <br />&#160;&#160;&#160; <span><span style="color: #008000">// instantiate &amp; init a new contact object</span></span><br />&#160;&#160;&#160; <span><span style="color: #0000ff">var</span></span> c = contacts.Create();<br />&#160;&#160;&#160; c.FirstName = <span><span style="color: #a31515">&quot;Merlin&quot;</span></span>;<br />&#160;&#160;&#160; c.LastName = <span><span style="color: #a31515">&quot;Mann&quot;</span></span>;<br />&#160;&#160;&#160; c.Title = <span><span style="color: #a31515">&quot;Hi. Can I Axe You A Question?&quot;</span></span>;<br /> <br />&#160;&#160;&#160; <span><span style="color: #008000">// add it to your list of contacts (repository)</span></span><br />&#160;&#160;&#160; contacts.Add(c);<br /> <br />&#160;&#160;&#160; <span><span style="color: #008000">// validate and commit changes</span></span><br />&#160;&#160;&#160; work.Save();<br />}&#160;&#160;&#160;&#160;&#160;&#160; </span></span></pre> <p>Then, your logic/business/domain code is all:</p> <pre style="background: white"><span style="font-family: consolas"><span><span style="color: #0000ff"><span>using</span></span></span><span> System;<br /><span><span style="color: #0000ff">using</span></span> System.Collections.Generic;<br /><span><span style="color: #0000ff">using</span></span> Ignorance;<br /><span><span style="color: #0000ff">using</span></span> Ignorance.Domain;<br /><span><span style="color: #0000ff">using</span></span> YourApplicationHere.Data;<br /> <br /><span><span style="color: #0000ff">namespace</span></span> YourApplicationHere.Domain<br />{<br />&#160;&#160;&#160; <span><span style="color: #0000ff">public</span></span>&#160;<span><span style="color: #0000ff">class</span></span>&#160;<span><span style="color: #2b91af">ContactService</span></span> : <span><span style="color: #2b91af">Service</span></span>&lt;<span><span style="color: #2b91af">Contact</span></span>&gt;<br />&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">public</span></span> ContactService(<span><span style="color: #2b91af">IWork</span></span> work) : <span><span style="color: #0000ff">base</span></span>(work) { }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">protected</span></span>&#160;<span><span style="color: #0000ff">override</span></span>&#160;<span><span style="color: #0000ff">void</span></span> OnCreated(<span><span style="color: #2b91af">Contact</span></span> entity)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; entity.FirstName = <span><span style="color: #a31515">&quot;New&quot;</span></span>;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; entity.LastName = <span><span style="color: #a31515">&quot;Guy&quot;</span></span>;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; entity.Suffix = <span><span style="color: #a31515">&quot;Esq.&quot;</span></span>;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">protected</span></span>&#160;<span><span style="color: #0000ff">override</span></span>&#160;<span><span style="color: #0000ff">void</span></span> OnSaving(<span><span style="color: #2b91af">Contact</span></span> entity)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">if</span></span> (!entity.EmailAddress.Contains(<span><span style="color: #a31515">&quot;@&quot;</span></span>))<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">throw</span></span>&#160;<span><span style="color: #0000ff">new</span></span>&#160;<span><span style="color: #2b91af">ApplicationException</span></span>(<span><span style="color: #a31515">&quot;That's not an email address.&quot;</span></span>);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">protected</span></span>&#160;<span><span style="color: #0000ff">override</span></span>&#160;<span><span style="color: #0000ff">void</span></span> OnDeleting(<span><span style="color: #2b91af">Contact</span></span> entity)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">if</span></span> (entity.LastName == <span><span style="color: #a31515">&quot;Fazzaro&quot;</span></span>)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">throw</span></span>&#160;<span><span style="color: #0000ff">new</span></span>&#160;<span><span style="color: #2b91af">ApplicationException</span></span>(<span><span style="color: #a31515">&quot;Don't delete me or my relatives!&quot;</span></span>);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">public</span></span>&#160;<span><span style="color: #2b91af">IEnumerable</span></span>&lt;<span><span style="color: #2b91af">Contact</span></span>&gt; GetContactsWithSuffixesBecauseTheyAreAwesome()<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">var</span></span> repo = GetStore();<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">return</span></span> repo.Some(p =&gt; p.Suffix != <span><span style="color: #0000ff">null</span></span>);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br /> <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">public</span></span>&#160;<span><span style="color: #0000ff">void</span></span> JustGoAheadAndMakeSureEveryoneHasSuffixesNow()<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">var</span></span> repo = GetStore();<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">var</span></span> unsuffixedPeeps = repo.Some(p =&gt; p.Suffix == <span><span style="color: #0000ff">null</span></span>);<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span><span style="color: #0000ff">foreach</span></span> (<span><span style="color: #0000ff">var</span></span> peep <span><span style="color: #0000ff">in</span></span> unsuffixedPeeps)<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; {<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; peep.Suffix = <span><span style="color: #a31515">&quot;Esq.&quot;</span></span>;<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160;&#160;&#160;&#160;&#160; }<br />&#160;&#160;&#160; }<br />}</span></span><br /></pre>

And that's more or less all you're left holding the bag for. I know, right?

<p>That <em>Contact</em> object (and all of its brother and sister data objects) is just
View on GitHub
GitHub Stars5
CategoryDevelopment
Updated7y ago
Forks2

Languages

C#

Security Score

55/100

Audited on Jan 5, 2019

No findings