tag:blogger.com,1999:blog-804206524255460042024-03-08T14:05:53.078-08:00SynericsDevJohann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.comBlogger15125tag:blogger.com,1999:blog-80420652425546004.post-44379307268095838342011-09-01T04:07:00.000-07:002011-09-01T04:07:03.024-07:00Versioning JavaScript FilesA common problem web developers face is dealing with versions of JavaScript files (and other resources like CSS file, images etc.) On the one hand we want to cache the file so that it doesn't get downloaded every time, but on the other hand we need to be able to force the browser to get a new version if the file changes.<br />
<br />
My solution is to treat my own JavaScript files differently from the files I include from frameworks such as jQuery. <br />
<br />
For framework files I add a version number to the file name e.g. jQuery-1.5.1.min.js. When the framework is updated, I will use a new file with the new version number in its name and the browser will fetch the new file.<br />
<br />
For my own files I append a query string to the end of the url that contains a version number. This version number is fetched from the DLL that the controller code runs from. The effect is that each time a new build is deployed, the browser will refetch the JavaScript file, but in between builds it will cache it.<br />
<br />
To this end I have two different URL helpers that I use when referencing files as seen below.<br />
<br />
<pre class="csharpcode"><span class="kwrd"><</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span> <span class="attr">language</span><span class="kwrd">="javascript"</span> <span class="attr">src</span><span class="kwrd">="<%: Url.Content("</span>~/<span class="attr">Content</span>/<span class="attr">Scripts</span>/<span class="attr">jquery-1</span>.<span class="attr">5</span>.<span class="attr">1</span>.<span class="attr">min</span>.<span class="attr">js</span><span class="kwrd">") %>"</span><span class="kwrd">></</span><span class="html">script</span><span class="kwrd">></span></pre><pre class="csharpcode"><span class="kwrd"><</span><span class="html">script</span> <span class="attr">type</span><span class="kwrd">="text/javascript"</span> <span class="attr">language</span><span class="kwrd">="javascript"</span> <span class="attr">src</span><span class="kwrd">="<%: Url.VersionedLink("</span>~/<span class="attr">Content</span>/<span class="attr">Scripts</span>/<span class="attr">General</span>.<span class="attr">js</span><span class="kwrd">") %>"</span><span class="kwrd">></</span><span class="html">script</span><span class="kwrd">></span></pre>Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com2tag:blogger.com,1999:blog-80420652425546004.post-61242721481077226712011-07-28T01:04:00.000-07:002011-07-28T01:04:01.584-07:00Formatting Dates in MVCWhen you want to display a value in MVC, for instance a date, that needs to be formatted and may even be null, it can result in a lot of code in the view. For example.<br />
<pre class="csharpcode"><span class="asp"><%</span>: benefit.BenefitCancelledDt.HasValue ? benefit.BenefitCancelledDt.Value.ToString(Constants.SHORTDATE_FORMAT) : <span class="str">""</span> <span class="asp">%></span></pre>There is a better alternative. By using data annotations we can do this it in a much shorter for.<br />
<br />
Add the following annotation to the property on the model.<br />
<pre class="csharpcode">[DisplayFormat (DataFormatString = <span class="str">"{0:"</span> + Constants.SHORTDATE_FORMAT + <span class="str">"}"</span>)]
<span class="kwrd">public</span> DateTime? BenefitCancelledDt { get; set; }</pre>The view can then be rewritten like this.<br />
<pre class="csharpcode"><span class="asp"><%</span>: Html.DisplayFor (x => x.PolicyBenefits[i].BenefitCancelledDt)<span class="asp">%></span></pre>Note that I used <I>Html.DisplayFor</I>. The formatting annotation is not used by the other methods like <I>Html.DisplayTextFor</I>. Also note that the expression needs to bind directly to the model and not to a intermediate variable like <I>benefit</I> in the inital code.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-86004487598403576602011-07-25T00:11:00.000-07:002011-07-25T00:11:24.966-07:00Chucking Out the RegistryI have refactored away from the registry I used previously to store the DataAccessAdaptor, UnitOfWork, ServiceHome, etc. I am now using a DataContext class that has references to the other objects (DataAccessAdapterm etc.) but is itself managed by the DI container. So to access the DataAccessAdapter, instead of using<br />
<!-- code formatted by http://manoli.net/csharpformat/ --><br />
<pre class="csharpcode">Registry.GetDataAccessAdapter();</pre>you now use<br />
<!-- code formatted by http://manoli.net/csharpformat/ --><br />
<pre class="csharpcode">ObjectFactory.GetInstance<IDataContext> ().Adapter;</pre>All the connection and transaction management is now handled by the DataContext as well.<br />
<br />
Here are some pieces of the DataContext class.<br />
<!-- code formatted by http://manoli.net/csharpformat/ --><br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> DataContext
: IDataContext
{
<span class="kwrd">public</span> ActionProcedures ActionProcedures { get; set; }
<span class="kwrd">public</span> IRetrievalProcedures RetrievalProcedures { get; set; }
<span class="kwrd">public</span> DataAccessAdapterBase Adapter { get; set; }
...
<span class="kwrd">public</span> <span class="kwrd">void</span> Clear ()
{
<span class="rem">// If clear is called while there is still a transaction, the client code has a bug</span>
<span class="kwrd">if</span> (Adapter != <span class="kwrd">null</span> && Adapter.IsTransactionInProgress)
{
<span class="kwrd">throw</span> <span class="kwrd">new</span> Exception (<span class="str">"There is still an active transaction."</span>);
}
<span class="rem">// First save any changes in UnitOfWork</span>
<span class="kwrd">if</span> (UnitOfWork != <span class="kwrd">null</span>)
{
UnitOfWork.Commit (Adapter, <span class="kwrd">true</span>);
}
<span class="rem">// Dispose and remove adapter</span>
<span class="kwrd">if</span> (Adapter != <span class="kwrd">null</span>)
{
Adapter.Dispose ();
}
ActionProcedures = <span class="kwrd">null</span>;
RetrievalProcedures = <span class="kwrd">null</span>;
Adapter = <span class="kwrd">null</span>;
...
}
<span class="kwrd">public</span> <span class="kwrd">void</span> Initialize (
DataAccessAdapterBase adapter)
{
Adapter = adapter;
ActionProcedures = <span class="kwrd">new</span> ActionProcedures ();
RetrievalProcedures = <span class="kwrd">new</span> RetrievalProcedures ();
...
}
<span class="kwrd">public</span> <span class="kwrd">virtual</span> <span class="kwrd">void</span> StartTransaction (
IsolationLevel isolationLevel,
<span class="kwrd">string</span> transactionName)
{
Adapter.StartTransaction (isolationLevel, transactionName);
}
...
}</pre>The next step would now be to get rid of the DataAccessorHome and ServiceHome completely and simply get the data accessors and services from the DI container directly.<br />
<br />
Thanks to Francois and Schalk for their inputs.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-30558595755953159892011-07-24T23:58:00.000-07:002011-07-24T23:58:58.372-07:00Setting the Date for TestingI've added the following code to the remoting sink on the remoting server side to enable us to set the date on the server for automated ui testing.<br />
<pre class="csharpcode"><span class="kwrd">if</span> (!<span class="kwrd">string</span>.IsNullOrWhiteSpace (ConfigurationManager.AppSettings[<span class="str">"Date"</span>]))
{
ObjectFactory.GetInstance<IDataContext> ().ServiceHome.DateService.SetDate (Utils.ParseDateTime (ConfigurationManager.AppSettings[<span class="str">"Date"</span>]));
}</pre>Please make sure this setting never gets set on the production environment.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-27933890071442790162011-07-14T03:59:00.000-07:002011-07-14T03:59:53.228-07:00Automatic Database Script ExecutionWe are using an automatic database script execution mechanism such as the one recommended by Scott Ambler. The basic working of it is that every database refactoring is placed in its own script file and that each file has a sequence number. We also store the sequence number of the last run script in a table called Version along with a column called DatabaseName that we can query to find out which database we are running the script against.<br />
<br />
When the automated deployment takes place, our PowerShell script will automatically run all the scripts since the last one that was run against that database and update the version number.<br />
<br />
During an automated build, we actually create the entire unit test database from scratch with a few initialization scripts and then apply all the update scripts in sequence to arrive at the latest database schema.<br />
<br />
We also have a script that we us when we download a production database to our development machines, to bring that database up to speed with the latest scripts to match our code.<br />
<br />
I recently had an issue where I needed to create a stored procedure on the unit test database that I didn't want in production. This procedure wipes all the data in the database except lookup data. I needed this when using recorded tests from Selenium, because unlike our other tests, we can't do the entire test in one transaction and I needed to clean up after the test.<br />
<br />
In order to achieve this I used some dynamic SQL like so (thanks for the tip Schalk):<br />
<pre class="csharpcode"><span class="kwrd">if</span> dbo.GetDatabaseName () = <span class="str">'policy-unittests'</span>
<span class="kwrd">BEGIN</span>
<span class="kwrd">DECLARE</span> @<span class="kwrd">Sql</span> NVARCHAR(<span class="kwrd">MAX</span>)
<span class="kwrd">SET</span> @<span class="kwrd">Sql</span> =
<span class="str">'CREATE PROCEDURE CleanDatabase
AS
-- Lots of delete statements
DELETE FROM Policy
DELETE FROM Person'</span>
<span class="kwrd">EXECUTE</span> sp_executesql @<span class="kwrd">Sql</span>
<span class="kwrd">END</span>
</pre>Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com1tag:blogger.com,1999:blog-80420652425546004.post-25172923708731574542011-07-14T00:10:00.000-07:002011-07-14T00:10:38.928-07:00Adding Convenience Methods to ClassesCode can often be simplified by adding a few convenience methods to our classes. For instance, one set of classes were littered with code like this:<br />
<pre class="csharpcode">CommandInitDataEntity data = CommandEntity.InitDatas.SingleOrDefault (x => x.Key == InitDataKeys.MoiBliscyUpgradeType.ToString ());
<span class="kwrd">if</span> (data != <span class="kwrd">null</span>)
{
<span class="kwrd">this</span>.UpgradeType = (UpgradeType) Enum.Parse (<span class="kwrd">typeof</span> (UpgradeType), data.Value);
}</pre>I added the following convenience methods to the CommandEntity class:<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">bool</span> HasInitData (
<span class="kwrd">string</span> key)
{
<span class="kwrd">return</span> <span class="kwrd">this</span>.InitDatas.ToList ().Exists (x => x.Key == key);
}
<span class="kwrd">public</span> <span class="kwrd">bool</span> HasInitData (
Enum key)
{
<span class="kwrd">return</span> HasInitData (key.ToString ());
}
<span class="kwrd">public</span> <span class="kwrd">string</span> GetValueFromInitData (
<span class="kwrd">string</span> key)
{
<span class="kwrd">return</span> <span class="kwrd">this</span>.InitDatas.Single (x => x.Key == key).Value;
}
<span class="kwrd">public</span> <span class="kwrd">string</span> GetValueFromInitData (
Enum key)
{
<span class="kwrd">return</span> GetValueFromInitData (key.ToString ());
}
<span class="kwrd">public</span> T GetValueFromInitData (
Enum key)
<span class="kwrd">where</span> T : <span class="kwrd">struct</span>, IConvertible
{
Type type = <span class="kwrd">typeof</span> (T);
<span class="kwrd">if</span> (type.IsEnum)
{
<span class="kwrd">return</span> (T) Enum.Parse (<span class="kwrd">typeof</span> (T), GetValueFromInitData (key));
}
<span class="kwrd">throw</span> <span class="kwrd">new</span> NotImplementedException (<span class="str">"Unsupported type"</span>);
}
</pre>I could then rewrite the original code like this:<br />
<pre class="csharpcode"><span class="kwrd">if</span> (commandEntity.HasInitData (InitDataKeys.MoiBliscyUpgradeType))
{
<span class="kwrd">this</span>.UpgradeType = commandEntity.GetValueFromInitData (InitDataKeys.MoiBliscyUpgradeType);
}
</pre>It may not seem like a big difference, but well designed software is achieved though hundreds of little decisions that all add up.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com1tag:blogger.com,1999:blog-80420652425546004.post-10352606457797997022011-07-12T03:58:00.000-07:002011-07-12T03:58:30.200-07:00How to Make Selenium Wait for Ajax CallsI needed to be able to wait for Selenium to complete an Ajax call before checking the effects. After looking at many different solutions, I came up with this one.<br />
<br />
Add a hidden field to the master page to store the number of Ajax requests that have been done.<br />
<pre class="csharpcode"><span class="kwrd"><</span><span class="html">input</span> <span class="attr">type</span><span class="kwrd">="hidden"</span> <span class="attr">id</span><span class="kwrd">="NumAjaxRequests"</span> <span class="attr">value</span><span class="kwrd">="0"</span> <span class="kwrd">/></span></pre>Hook into the jQuery Ajax framework to automatically increment the field for each request that completes.<br />
<pre class="csharpcode">$('.ajaxindicator').ajaxStop(function ()
{
$('#NumAjaxRequests').val(parseInt($('#NumAjaxRequests').val() + 1));
});</pre>You can then reset the field before performing the action that causes the Ajax call, perform that action and tell Selenium to wait until the expected number of Ajax calls have taken place.<br />
<pre class="csharpcode">| type | NumAjaxRequests | 0 |
| select | Mb_SpouseId | label=Spouse Name |
| waitForValue | NumAjaxRequests | 1 |
</pre>Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com2tag:blogger.com,1999:blog-80420652425546004.post-27488185555043139202011-07-11T02:31:00.000-07:002011-07-11T02:31:51.497-07:00Master Pages RefactoredThe master pages have been refactored. There are now two levels of master pages.<br />
<br />
Page.Master is the top level master page that contains most of the structure, script, links, etc.<br />
<br />
MvcSite-NoSummary.Master, MvcSite-WideSummary.Master and MvcSite.Master inherit from Page.Master and do the following:<br />
<br />
1. Reference different style sheets<br />
2. Repeat content place holders that the actual pages need from Page.Master<br />
3. Add layout structure for the right panel if neededJohann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-89457356544538094522011-07-08T03:15:00.000-07:002011-07-08T03:15:40.077-07:00Removing Duplicate Transaction Management CodeThere is a method called WrapInTransaction on RemotingObjectBase that you should use to add transaction management to code in the remoting facade.<br />
<br />
Instead of writing<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">void</span> RaiseBirthdayEvents ()
{
StartTransaction (IsolationLevel.ReadCommitted, <span class="str">"RaiseBirthdayEvents"</span>);
<span class="kwrd">bool</span> committed = <span class="kwrd">false</span>;
<span class="kwrd">try</span>
{
List<<span class="kwrd">int</span>> personIds = Accessors.PersonDA.FetchPersonIdsByBirthDt (Services.DateService.Today.Month, Services.DateService.Today.Day);
<span class="kwrd">foreach</span> (<span class="kwrd">int</span> personId <span class="kwrd">in</span> personIds)
{
Services.EventingService.RaiseEvent (EventType.Birthdate, Services.DateService.Today, Table.Person, personId);
}
CommitTransaction ();
committed = <span class="kwrd">true</span>;
}
<span class="kwrd">finally</span>
{
<span class="kwrd">if</span> (!committed)
{
RollbackTransaction ();
}
}
}</pre><br />
you can now write<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">void</span> RaiseBirthdayEvents ()
{
WrapInTransaction (<span class="str">"RaiseBirthdayEvents"</span>, () =>
{
List<<span class="kwrd">int</span>> personIds = Accessors.PersonDA.FetchPersonIdsByBirthDt (Services.DateService.Today.Month, Services.DateService.Today.Day);
<span class="kwrd">foreach</span> (<span class="kwrd">int</span> personId <span class="kwrd">in</span> personIds)
{
Services.EventingService.RaiseEvent (EventType.Birthdate, Services.DateService.Today, Table.Person, personId);
}
});
}</pre><br />
Similarly if the code being wrapped needs to return a value, then<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> CreateProlinkManualApplication ()
{
<span class="kwrd">bool</span> committed = <span class="kwrd">false</span>;
StartTransaction (IsolationLevel.ReadCommitted, <span class="str">"CreateApplication"</span>);
<span class="kwrd">try</span>
{
ApplicationEntity application = ApplicationEntity.Create ();
application.SaleSourceId = (<span class="kwrd">int</span>) SaleSource.ProsperityApplicationForm;
Adapter.SaveEntity (application);
CommitTransaction ();
committed = <span class="kwrd">true</span>;
<span class="kwrd">return</span> application.ApplicationId;
}
<span class="kwrd">finally</span>
{
<span class="kwrd">if</span> (!committed)
{
RollbackTransaction ();
}
}
}</pre>becomes<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">int</span> CreateProlinkManualApplication ()
{
<span class="kwrd">return</span> WrapInTransaction (<span class="str">"CreateApplication"</span>, () =>
{
ApplicationEntity application = ApplicationEntity.Create ();
application.SaleSourceId = (<span class="kwrd">int</span>) SaleSource.ProsperityApplicationForm;
Adapter.SaveEntity (application);
<span class="kwrd">return</span> application.ApplicationId;
});
}</pre>Finally here is the code for the two overloads of the wrapping method:<br />
<pre class="csharpcode"><span class="kwrd">protected</span> <span class="kwrd">void</span> WrapInTransaction (
<span class="kwrd">string</span> transactionName,
Action action)
{
StartTransaction (IsolationLevel.ReadCommitted, transactionName);
<span class="kwrd">bool</span> committed = <span class="kwrd">false</span>;
<span class="kwrd">try</span>
{
action.Invoke ();
CommitTransaction ();
committed = <span class="kwrd">true</span>;
}
<span class="kwrd">finally</span>
{
<span class="kwrd">if</span> (!committed)
{
RollbackTransaction ();
}
}
}
<span class="kwrd">protected</span> T WrapInTransaction <T> (
<span class="kwrd">string</span> transactionName,
Func<T> function)
{
StartTransaction (IsolationLevel.ReadCommitted, transactionName);
<span class="kwrd">bool</span> committed = <span class="kwrd">false</span>;
<span class="kwrd">try</span>
{
T result = function.Invoke ();
CommitTransaction ();
committed = <span class="kwrd">true</span>;
<span class="kwrd">return</span> result;
}
<span class="kwrd">finally</span>
{
<span class="kwrd">if</span> (!committed)
{
RollbackTransaction ();
}
}
}</pre>Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-91068733738238507772011-07-07T23:39:00.000-07:002011-07-07T23:42:37.360-07:00Mocking LlblGen RetrievalProceduresIf you need to create a mock for one of the LlblGen classes such as RetrievalProcedures, you are faced with the problem that there is no interface for you to use. Fortunately most of the classes generated by LlblGen are partials so you can use add your own code to them.<br />
<br />
Suppose you need to mock out RetrievalProcedures and that it has a method called GetPins you want to mock:<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">partial</span> <span class="kwrd">class</span> RetrievalProcedures
{
<span class="kwrd">public</span> <span class="kwrd">virtual</span> DataTable GetPins(System.Int32 numberOfPins)
{
<span class="kwrd">using</span>(DataAccessAdapter dataAccessProvider = <span class="kwrd">new</span> DataAccessAdapter())
{
<span class="kwrd">return</span> GetPins(numberOfPins, dataAccessProvider);
}
}
<span class="kwrd">public</span> <span class="kwrd">virtual</span> DataTable GetPins(System.Int32 numberOfPins, IDataAccessCore dataAccessProvider)
{
<span class="kwrd">using</span>(StoredProcedureCall call = CreateGetPinsCall(dataAccessProvider, numberOfPins))
{
DataTable toReturn = call.FillDataTable();
<span class="kwrd">return</span> toReturn;
}
}
...
}
</pre><br />
You can create a partial class next to it as well as the interface you need and let the partial class implement the interface:<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">interface</span> IRetrievalProcedures
{
DataTable GetPins(System.Int32 numberOfPins);
DataTable GetPins(System.Int32 numberOfPins, IDataAccessCore dataAccessProvider);
}</pre><br />
<pre class="csharpcode"><span class="kwrd">partial</span> <span class="kwrd">class</span> RetrievalProcedures : IRetrievalProcedures
{
}</pre><br />
The original class now implements the new interface and you can go forth and mock.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-58575558157438850682011-07-07T06:23:00.000-07:002011-07-07T06:23:21.607-07:00Enums: The Dark SideIn a previous post I advocated using enums as much as possible. There are however times when this is not a good idea.<br />
<br />
If a lookup table requires the ability to easily add new lookup values without redeploying the code base, you should avoid using enums in data accessors, view models, etc. <br />
<br />
The situation we had involved document types. After adding a new document type, the code had to be updated. This shouldn't be necessary in this case and should have just worked.<br />
<br />
So as with most things, use common sense. I suggest keeping a list of lookups that should not be dealt with using enums for your project.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-61114929514785392732011-07-07T05:53:00.000-07:002011-07-07T06:06:11.101-07:00Unit Testing Private MethodsSometimes you want to write a unit test against a private method. <br />
<br />
First let me say that most of the time this is an indication of a design problem. Rather consider whether your class really wants to be more than one class or whether you should be testing the code indirectly using one of the other public methods of the class.<br />
<br />
If you are convinced you really want to write a unit test for the private method, there is an alternative to calling the method using AsDynamic (). The problem with AsDynamic () is that you lose all the benefits of strong typing, including support for refactoring.<br />
<br />
Lets say you have a class called Class1 like this:<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> Class1
{
<span class="kwrd">private</span> <span class="kwrd">void</span> Method1 ()
{
}
}</pre><br />
In order to call Method1 you can change Method1 in Class1 to protected and create a testing subclass like this:<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> <span class="kwrd">class</span> TestClass1
: Class1
{
<span class="kwrd">public</span> <span class="kwrd">void</span> TestMethod1 ()
{
Method1 ();
}
}</pre><br />
In your tests you can then create an instance of TestClass1 and call TestMethod1.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-86099083248862749432011-07-06T02:05:00.000-07:002011-07-06T02:05:24.173-07:00Configuring Moq to return different resultsIf you need to create a mock that can return one result the first time and another the second time, you can use the MoqExtensions class like this:<br />
<br />
<pre class="csharpcode">MockPremiumCalculationService = <span class="kwrd">new</span> Mock<IPremiumCalculationService> ();
PremiumCalculationResponse response1 = <span class="kwrd">new</span> PremiumCalculationResponse ();
response1.Premium = 55;
PremiumCalculationResponse response2 = <span class="kwrd">new</span> PremiumCalculationResponse ();
response2.Premium = 60;
MockPremiumCalculationService.Setup (
pcs => pcs.Calculate (It.IsAny<PremiumCalculationRequest> ()))
.ReturnsInOrder (response1, response2);</pre><br />
For more info see this article by Phil Haack: <br />
<br />
<a href="http://haacked.com/archive/2009/09/29/moq-sequences.aspx">http://haacked.com/archive/2009/09/29/moq-sequences.aspx</a>Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-47750201458282160872011-07-05T01:29:00.000-07:002011-07-06T01:48:00.619-07:00Data Access from LD.FunctionalTestInstead of writing a separate query for each entity in your test class e.g.<br />
<br />
<pre class="csharpcode"><span class="kwrd">private</span> List<PremiumEntity> FetchPremiums(
<span class="kwrd">params</span> <span class="kwrd">int</span>[] policyIds)
{
LinqMetaData metaData = <span class="kwrd">new</span> LinqMetaData(Adapter);
var query = (from p <span class="kwrd">in</span> metaData.Premium
<span class="kwrd"> where</span> policyIds.Contains (p.PolicyId)
select p);
<span class="kwrd"> return</span> query.ToList();
}</pre><br />
there is now a method on LinqMetadata you can use along with a property called MetaData on the TestBase class.The above code can now be replaced by a call inside your test that looks like this:<br />
<br />
<pre class="csharpcode">List<PremiumEntity> partnerPremiums = MetaData.GetEntities<PremiumEntity> ();</pre><br />
Similarly you can get the number of entities in the database by calling<br />
<br />
<pre class="csharpcode">List<PremiumEntity> partnerPremiums = MetaData.GetNumEntities<PremiumEntity> ();</pre><br />
Note that you don't need to get the number of entities in the database at the start and compare them with the number at the end because we always start with a clean database.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0tag:blogger.com,1999:blog-80420652425546004.post-13110312408639680972011-07-01T03:14:00.000-07:002011-07-06T01:48:48.148-07:00Use of enumsTry to use enums as much as possible.<br />
<br />
For example, instead of writing<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> DocumentEntity CreateDocument (
<span class="kwrd"> int</span> documentTypeId)
{
DocumentEntity document = <span class="kwrd">new</span> DocumentEntity ();
document.DocumentTypeId = documentTypeId;
<span class="kwrd"> return</span> document;
}</pre><br />
use<br />
<br />
<pre class="csharpcode"><span class="kwrd">public</span> DocumentEntity CreateDocument (
DocumentType documentType)
{
DocumentEntity document = <span class="kwrd">new</span> DocumentEntity ();
document.DocumentTypeId = (<span class="kwrd">int</span>)documentType;
<span class="kwrd"> return</span> document;
}</pre><br />
This holds for data accessors, remoting objects, etc.Johann Strydomhttp://www.blogger.com/profile/09486615837039168410noreply@blogger.com0