tag:blogger.com,1999:blog-86193588982279911992024-03-13T10:51:24.869+02:00Ivan's BlogIvan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.comBlogger35125tag:blogger.com,1999:blog-8619358898227991199.post-37675257831149548682012-08-26T23:19:00.000+03:002012-09-25T12:00:41.116+03:00ServiceStack: Reusing DTOs<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="color: red;"><b>UPDATE:</b></span><br />
<blockquote class="tr_bq">
This post is no longer relevant, because these ideas were included into the heart of the ServiceStack <a href="https://github.com/ServiceStack/ServiceStack/wiki/New-API">New API</a>. It just a pleasure to use such a great framework!</blockquote>
<br />
I've recently worked on HTTP API for one of our products. As it was a bit of time pressure, we've decided to use <a href="http://servicestack.net/">ServiceStack</a> - an open source web services framework. If you didn't hear about it go ahead and <a href="https://github.com/ServiceStack/ServiceStack/wiki">read</a>. It is simple yet very powerful, well written framework which just works and makes things done without friction. And it exists for years already (if you would ask why not we were using Web API, which was not yet released at the moment).<br />
<br />
I'd like to share some experience gained during my work. So here is the context. The first consumer of API will be our another project having back-end written in C# as well. ServiceStack allows to provide clients with strongly-typed assembly reusing the same requests/responses dtos used to build service (note that no code generation is needed). In fact this is <a href="https://github.com/ServiceStack/ServiceStack/wiki/Clients-overview">recommended approach</a> for C# clients.<br />
<br />
The preferred way to call web service is to utilize <a href="https://github.com/ServiceStack/ServiceStack/wiki/Endpoints">REST endpoints</a>, which can be configured using RestService attribute per each request DTO. Example below (<a href="https://gist.github.com/3482626">here is a gist</a>) shows how we can call services via REST endpoints, and how DTOs are reused.<br />
<br />
<pre class="brush: csharp; gutter: false;">/// DTOs
[RestService("/orders/{id}", "GET")]
[RestService("/orders/by-code/{code}", "GET")]
[RestService("/orders/search", "GET")]
public class GetOrders {
public int? Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Customer { get; set; }
}
public class GetOrdersResponse {
public Order[] Orders { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class Order {
public int Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Customer { get; set; }
}
[RestService("/orders", "POST")] // to create new order.
[RestService("/orders/{id}", "PUT")] // to create or update existing order with specified Id.
public class SaveOrder {
public int? Id { get; set; }
// Order details.
}
public class SaveOrderResponse {
public int Id { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
//// Rest endpoints usage
IRestClient client = new JsonServiceClient();
var orderById = client.Get<GetOrdersResponse>("/orders/" + 5).Orders.Single();
var orderByCode = client.Get<GetOrdersResponse>("orders/by-code/" + Uri.EscapeDataString(orderById.Code)).Orders.Single();
var searchUrl = "orders/search?Name={0}&Customer={1}".Fmt(
Uri.EscapeDataString(orderByCode.Name), Uri.EscapeDataString(orderByCode.Customer));
var foundOrders = client.Get<GetOrdersResponse>(searchUrl).Orders;
var createOrderResponse = client.Post<SaveOrderResonse>("/orders", new SaveOrder { /* Order details */});
int orderId = createOrderResponse.Id;
var updateOrderResponse = client.Put<SaveOrderResonse>("/orders/" + orderId, new SaveOrder { /* Order details */ });
</pre>
<br />
Notice that dtos follow naming convention - response classes named exactly like request classes but with <span style="font-family: Courier New, Courier, monospace;">Response</span><span style="font-family: inherit;"> prefix. This simplifies clients life, as it becomes obvious what response is expected. It also allows to generate <a href="https://github.com/ServiceStack/ServiceStack/wiki/Metadata-page">services metadata</a> automatically.</span><br />
<br />
However, there are several things that bothers me here. While I still want clients to call services via REST endpoints, I'd like to have more generic and easy to follow api. Here are things that come to mind:<br />
<br />
<ul>
<li>Whether we really need to specify Response type, while we already know what kind of request we send. Couldn't we automatically determine it by the request type?</li>
<li>We already know urls available for given request type (from RestService attributes). It would nice if we can simplify developers life by picking and populating them automatically based on request state.</li>
<li>We can go further and automatically determine required HTTP method.</li>
<li>And last - for GET and DELETE request we could send additional request properties (not mapped in url template) as query parameters.</li>
</ul>
<br />
As side effect of achieving this, we'll get another important benefit - ability to change Urls and even Http methods without breaking clients code. And this is essential for me - I'm sure I would like to change some Urls while it used by our other product only (but before it goes public).<br />
<br />
So this is a method I'd like to have:<br />
<span style="font-family: Courier New, Courier, monospace;">IRestClient.Send<TResponse>(IRequest<TResponse> request)</span><br />
<br />
where IRequest - is just a marker interface applied to all request types, thus allowing to determine corresponding response type at compile time. And this is how our example will looks like (<a href="https://gist.github.com/3483088">here is the gist</a>):<br />
<br />
<pre class="brush: csharp; gutter: false;">// Response types omitted.
// Note that request types now marked with IRequest<tresponse> interface.
// This allows Send method to determine response type at compile time.
[RestService("/orders/{id}", "GET")]
[RestService("/orders/by-code/{code}", "GET")]
[RestService("/orders/search", "GET")]
public class GetOrders : IRequest<GetOrdersResponse> {
public int? Id { get; set; }
public string Code { get; set; }
public string Name { get; set; }
public string Customer { get; set; }
}
[RestService("/orders", "POST")] // to create new order.
[RestService("/orders/{id}", "PUT")] // to create or update existing order with specified Id.
public class SaveOrder : IRequest<SaveOrderResonse> {
public int? Id { get; set; }
// Order details.
}
//// Proposed interface
// Marker interface allowing to determine response type at compile time.
public interface IRequest<TResponse> {
}
public static class RestServiceExtensions
{
public static TResponse Send<TResponse>(this IRestClient client, IRequest<TResponse> request) {
// Determine matching REST endpoint for specified request (via RestService attributes).
// Populate url with encoded variables and optional query parameters.
// Invoke corresponding REST method with proper Http method
// and return strongly typed response
}
}
//// Service usage
IRestClient client = new JsonServiceClient();
// We don't specify response type - it is determined automatically based on request type.
// We don't specify URLs or HTTP verbs - they are determined based on request state.
// We don't write boilerplate code to encode url parts. It is done automatically.
// GET /orders/5
var orderById = client.Send(new GetOrders { Id = 5 }).Orders.Single();
// GET /orders/by-code/Code
var orderByCode = client.Send(new GetOrders { Code = orderById.Code }).Orders.Single();
// GET /orders/search?Name=Name&Customer=Customer
var getOrders = new GetOrders { Name = orderById.Name, Customer = orderByCode.Customer };
var foundOrders = client.Send(getOrders).Orders;
// POST /orders
var createOrderResponse = client.Send(new SaveOrder { /* Order details */});
// PUT /orders/id
int orderId = createOrderResponse.Id;
var updateOrderResponse = client.Send(new SaveOrder { Id = orderById.Id, /* Order details */ });
</pre>
<br />
<br />
That being said, I've implemented such extension method for IRestService interface. You can find source code with a test in a <a href="https://gist.github.com/3483193">gist at github</a>. There are several things missed there, but they should not be hard to implement:<br />
<br />
<ul style="text-align: left;">
<li>Same extension for Async service.</li>
<li>Special formatting for DateTime, List variables in url.</li>
<li>Other opinionated decisions on how to choose url when several urls matches specified request.</li>
</ul>
<div>
Hope you find this useful. Anyway, please let me know event if it feels like completely wrong approach.</div>
</div>
Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com3tag:blogger.com,1999:blog-8619358898227991199.post-91389412277665027522011-08-26T18:43:00.001+03:002011-08-26T18:46:47.343+03:00Reducing size of RTF file with image<div dir="ltr" style="text-align: left;" trbidi="on">
We use RTF format to generate documents in our products as it is widely supported by software vendors. The bad thing about RTF files is a file size, which drastically increases when document contains images (compared to the same document saved in "doc" format). This becomes a big issue when you have hundreds of thousands documents stored in a databases, and send them to other parties.<br />
<br />
As it turned out, the file size issue is not caused by RTF format itself, but by the way MS Word saves RTF files with images. When MS Word saves document in RTF format, it saves two copies of each image - original one and also a copy converted to WMF format. Fortunately, there is way to disable such a strange behavior. You can read how at Microsoft knowledge base article <a href="http://support.microsoft.com/kb/224663">Document file size increases with EMF, PNG, GIF, or JPEG graphics in Word</a>.</div>
Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com2tag:blogger.com,1999:blog-8619358898227991199.post-77212570681208779872011-08-17T01:43:00.004+03:002012-02-21T11:12:15.678+02:00Using NuGet to download all packages for the solution<div dir="ltr" style="text-align: left;" trbidi="on">
Here I'm talking about technique which allows you to <a href="http://docs.nuget.org/docs/workflows/using-nuget-without-committing-packages">use NuGet without committing packages to source control</a> (which is handy in case you use Mercurial). First time I've heard about this approach from <a href="http://joseoncode.com/2011/05/31/nuget-keep-your-package-folder-out-of-your-cvs/">José F. Romaniello post</a>. In essence: each project within solution has a pre-build step ensuring that all packages are downloaded via NuGet.exe command line utility. While this approach works well for a small project, it has some drawbacks with large solutions:<br />
<ul style="text-align: left;">
<li>It forces me to insert that build step into each project within solution, which is a little bit annoying.</li>
<li>It increases solution build time. This becomes visible for a solution containing a lot of projects. I guess that was one of the main reasons why <a href="http://simoncropp.posterous.com/introducingpepitaget">Simon Cropp wrote this post</a>.</li>
</ul>
<div>
So, do I really need to check for NuGet packages on each build? It think that checking for dependencies at CI server, and perhaps after pulling changes from source control might be enough.<br />
<br />
To achieve this I use a Powershell <a href="https://gist.github.com/1150094">script</a> which looks for packages.config files within a solution and executes NuGet.exe for each of them.</div>
<br />
<script src="https://gist.github.com/1150094.js">
</script>
<br />
This script uses repositories.config file to locate packages.config files instead of searching them in file system. Obviously to make it work, repositories.config should be placed in source control and path variables should be adjusted according to the solution folders structure.<br />
<br />
Hope you find this post helpful. At least I've learned some new things about powershell :). Please share your thoughts.</div>Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-54789278683466765812011-03-06T14:49:00.000+02:002011-03-06T14:49:17.163+02:00Великий піст 2011.Наступний тиждень доведеться холостякувати, ще й грошей залишилось 200 грн. Що ж, саме вчасно наступає Великий піст в цьому році.<br />
<br />
Якщо хтось теж збирається постити, то ось схема із сайту <a href="http://www.sestry.ru/church/content/velpost2011/index">sestry.ru</a>, яка допомагає розібратися що можна їсти і коли.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEkt6UGs4mb2FkIYP-6iqMQi6vHuu9gNkV5SCUF-1XMV57vVYYHeYtBI7x3Vhr0OnY3u24o8Uj2n8RPirWcvdmqFCdOdgACoIV4W43OysComi-zNZqhZ0tqwA3JkYs1igIgLaeh05i0VCg/s1600/velpost.jpg" imageanchor="1" style="margin-left:1em; margin-right:1em"><img border="0" height="400" width="242" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEkt6UGs4mb2FkIYP-6iqMQi6vHuu9gNkV5SCUF-1XMV57vVYYHeYtBI7x3Vhr0OnY3u24o8Uj2n8RPirWcvdmqFCdOdgACoIV4W43OysComi-zNZqhZ0tqwA3JkYs1igIgLaeh05i0VCg/s400/velpost.jpg" /></a></div>Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-34755086108428211432010-09-05T00:55:00.001+03:002010-09-05T01:00:07.365+03:00How to integrate MSpec into Hudson CI server<p>If you’re find this post, you might already know what is MSpec and Hudson CI server. If not, I’ll tell you.</p> <p><a href="http://http://github.com/machine/machine.specifications">MSpec</a> (short for Machine.Specifications) is an awesome .Net BDD style testing framework for, which allows you write tests without language noise.</p> <p><a href="http://wiki.hudson-ci.org/display/HUDSON/Meet+Hudson">Hudson CI</a> is an open source continuous integration server, which is very easy to install and configure with a user friendly interface. It allows you continuously build you projects and verify the quality of these builds. And it has a lot of plugins, which make developing with Hudson more interesting and fun.</p> <h4>So what does it mean integrate and why should I do this?</h4> <p>MSpec has a command line runner, which can fail the build in case some tests are failing, and it can generate nice HTML report which can be placed in build artifacts. So what else do I need, and why i should bother with Hudson integration?</p> <p>Because dealing with tests is one of the Hudson core features. It can show you information about tests for a particular build and tests history information, such as when they started breaking, how many new tests were added, how test duration changed from build to build, etc. Hudson can also show nice history trend charts.</p> <p><a href="http://lh6.ggpht.com/_77xe419MW1k/TILAed--VoI/AAAAAAAAGiY/aDw_PSESuDg/s1600-h/TestResult8.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="TestResult" border="0" alt="TestResult" src="http://lh3.ggpht.com/_77xe419MW1k/TILAf-2leQI/AAAAAAAAGic/CHK-ishWoP8/TestResult_thumb6.png?imgmax=800" width="644" height="514" /></a> </p> <p>At the screenshot above you can see the test result for a single build.</p> <p> </p> <p><a href="http://lh3.ggpht.com/_77xe419MW1k/TILAgXE3D4I/AAAAAAAAGig/GfY9Hf1wBFE/s1600-h/TestResultTrend4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="TestResultTrend" border="0" alt="TestResultTrend" src="http://lh6.ggpht.com/_77xe419MW1k/TILAhV5KpZI/AAAAAAAAGik/KI5c0h9_lB8/TestResultTrend_thumb2.png?imgmax=800" width="513" height="241" /></a> </p> <p>At this screenshot (from another project) you can see tests trend.</p> <p>And the most fun feature (at least to me) is the test tracking in a <a href="http://wiki.hudson-ci.org/display/HUDSON/The+Continuous+Integration+Game+plugin">Hudson Continuous Integration game</a> plugin, which gives points to user on improving the builds. This is my favorite plugin (ok, maybe after <a href="http://wiki.hudson-ci.org/display/HUDSON/ChuckNorris+Plugin">Chack Norris plugin</a>).</p> <p><a href="http://lh4.ggpht.com/_77xe419MW1k/TILAh3YWV8I/AAAAAAAAGio/JXmMxJBtCOI/s1600-h/SoreCard4.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SoreCard" border="0" alt="SoreCard" src="http://lh5.ggpht.com/_77xe419MW1k/TILAi4dHaBI/AAAAAAAAGis/cSVR4QZ3kjk/SoreCard_thumb2.png?imgmax=800" width="469" height="158" /></a> </p> <p>Would not it be great if MSpec tests will participate in such activities?</p> <h4>How to achieve that?</h4> <p>So how does Hudson know about all available testing tools for various programming languages? Of cause it doesn’t know about all of them. It works internally with JUnit xml reports, but there are many special plugins for most popular testing tool, which in fact transform testing tool output to JUnit format, thus allowing them to be used by Hudson. Unfortunately, there is no such plugin for MSpec currently. And I’m not familiar with Java to write it :)</p> <p>Good news is that MSpec can generate xml report (although it not as informative as i would like it to be). And we can write XSLT transformation to convert MSpec xml output to JUnit format and use it to provide test results to Hudson directly as JUnit tests.</p> <p>So here is such XSLT:</p> <script src="http://gist.github.com/565471.js?file=MSpecToJUnit.xslt"></script> <p>So now you can transform MSpec results to JUnit output in you build script and than use it in Hudson. If you are using MSBuil you can use it XsltTransformation task to do the conversion:</p> <script src="http://gist.github.com/565480.js?file=MSpecToJUnitWithMSBuild.xml"></script> <p>And finally you configure Hudson to use test results by specifying converted xml output. </p> <p><a href="http://lh6.ggpht.com/_77xe419MW1k/TILAjg41iYI/AAAAAAAAGiw/9dbLJa0cb_k/s1600-h/PublishTestResults%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PublishTestResults" border="0" alt="PublishTestResults" src="http://lh6.ggpht.com/_77xe419MW1k/TILAkUFqSCI/AAAAAAAAGi0/CUAvOHdGFro/PublishTestResults_thumb%5B1%5D.png?imgmax=800" width="609" height="118" /></a> </p> <p>Additionally it is a good idea to publish MSpec HTML report. You may do that with help of <a href="http://wiki.hudson-ci.org/display/HUDSON/HTML+Publisher+Plugin">HTML Publisher Plugin</a>:</p> <p><a href="http://lh4.ggpht.com/_77xe419MW1k/TILAlPT5qDI/AAAAAAAAGi4/SFmJ2cXmVFE/s1600-h/PublishSpecs%5B3%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="PublishSpecs" border="0" alt="PublishSpecs" src="http://lh6.ggpht.com/_77xe419MW1k/TILAmCq7WdI/AAAAAAAAGi8/SQ4Ynms4q-A/PublishSpecs_thumb%5B1%5D.png?imgmax=800" width="566" height="128" /></a> </p> <p>That is all you need (except of course that you need to write build script which would run you specifications and will generate xml and html reports). At the end you may see you specifications included as usual tests in Hudson. And as a bonus you get nice HTML report:</p> <p><a href="http://lh5.ggpht.com/_77xe419MW1k/TILAnGB2z2I/AAAAAAAAGjA/dAN5EF6iGnU/s1600-h/HudsonView%5B17%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="HudsonView" border="0" alt="HudsonView" src="http://lh3.ggpht.com/_77xe419MW1k/TILApMtwrDI/AAAAAAAAGjE/GjiB8tBefpw/HudsonView_thumb%5B11%5D.png?imgmax=800" width="684" height="772" /></a> </p> <p></p> <p></p> <p></p> <p>The link at the top of the page points to the Specifications HTML report:</p> <p><a href="http://lh3.ggpht.com/_77xe419MW1k/TILAqAUQazI/AAAAAAAAGjI/yoimyx7I-9c/s1600-h/SpecificationsReport%5B9%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="SpecificationsReport" border="0" alt="SpecificationsReport" src="http://lh4.ggpht.com/_77xe419MW1k/TILArdzYsNI/AAAAAAAAGjM/ukF5N5laVGI/SpecificationsReport_thumb%5B7%5D.png?imgmax=800" width="537" height="484" /></a> </p> <p><a href="http://lh3.ggpht.com/_77xe419MW1k/TILAsQiDhLI/AAAAAAAAGjQ/U3c6zsdd2Nc/s1600-h/Specifications%5B4%5D.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Specifications" border="0" alt="Specifications" src="http://lh3.ggpht.com/_77xe419MW1k/TILAuQgf2EI/AAAAAAAAGjY/UEvTZk_eY5I/Specifications_thumb%5B2%5D.png?imgmax=800" width="551" height="772" /></a> </p> <h4></h4> <p>That’s not all I wanted to say. But I’m a bit tired already. So much words :) Hope it would be useful for someone. And hope that yours Mr. Hudson and Chack Noris will always be happy:).</p> Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com6tag:blogger.com,1999:blog-8619358898227991199.post-55481355938977613882010-03-04T17:08:00.000+02:002010-03-04T17:08:38.035+02:00XML file in Visual Studio - Request for permission failed.Напишу для себе про помилку, яку показувала Visual Studio коли я відкривав xml файл. Бо не знатиму, що робити з цією помилкою, коли вона виникне наступного разу.<br />
<div><br />
</div><div>Отже ситуація така. Включив до проекту скачаний з інтернету XML файл. Відкрив його в Visual Studio 2008 і побачив таку помилку:<br />
<br />
</div><pre><span class="Apple-style-span" style="font-family: 'Times New Roman';"><span class="Apple-style-span" style="white-space: normal;"><span class="Apple-style-span" style="font-family: monospace;"><span class="Apple-style-span" style="white-space: pre;">
</span></span></span></span></pre><div><div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWE96qPKhjMpqcHsEOvRwaDDy6mkvaBiW30x510vWG0irBNc_BRITKCN_0WL4iQmJ88abf3lFxb4NoFEe_fCMz69GLA6EUVUslf8gkQ6YN-yE3aHAic5l3t579epHQ_EO04MHoX0IzXtLM/s1600-h/ErrorIndication.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="98" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWE96qPKhjMpqcHsEOvRwaDDy6mkvaBiW30x510vWG0irBNc_BRITKCN_0WL4iQmJ88abf3lFxb4NoFEe_fCMz69GLA6EUVUslf8gkQ6YN-yE3aHAic5l3t579epHQ_EO04MHoX0IzXtLM/s640/ErrorIndication.png" width="640" /></a></div><br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHrgvfzKfznBpEEjg-JxyLAzeLmREIqXRHoDjDU1l8v-y1FpuQQWkbd2DoW4PRlixwBPpkaRukAqaZV6b8rAn_eGjx96Ss1aiWuYGopug2D9K5FL1xy_YxH76OyQ4h4C3dT1TtT0IgKUDd/s1600-h/Error.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="108" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHrgvfzKfznBpEEjg-JxyLAzeLmREIqXRHoDjDU1l8v-y1FpuQQWkbd2DoW4PRlixwBPpkaRukAqaZV6b8rAn_eGjx96Ss1aiWuYGopug2D9K5FL1xy_YxH76OyQ4h4C3dT1TtT0IgKUDd/s640/Error.png" width="640" /></a></div><br />
Повідомлення про помилку:</div><pre>Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.</pre><br />
Якщо в DOCTYPE зазначено url схеми в інтернеті, то відповідно виникає помилка:<br />
<pre>Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
</pre><br />
При цьому до проекту було вже підключено кілька XML файлів із таким жe DTD.<br />
<br />
Гугл мені не допоміг. Здогадався подивитися у властивості файлу. Проблема виявилась простою - Windows заблокувала файл, тому що він був завантажений з інтернету.<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_W7HeqgGQ3I3R55h62RFXSFsZ60IJxbJgPih2Ek_rcW1yxxD45tZBUuzwNAe9Z81spTbSVu0q1PgqkSdd8qA1ll-CyDBuukn3fwcaHeoCcYmjftEQMZDrB8P6oG_m6hdnbqvTKbPsyu6P/s1600-h/Unlock.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_W7HeqgGQ3I3R55h62RFXSFsZ60IJxbJgPih2Ek_rcW1yxxD45tZBUuzwNAe9Z81spTbSVu0q1PgqkSdd8qA1ll-CyDBuukn3fwcaHeoCcYmjftEQMZDrB8P6oG_m6hdnbqvTKbPsyu6P/s640/Unlock.png" width="486" /></a></div><br />
Дуже дякую. Це ж мабуть я мав відразу здогадатися? Витратив кучу часу.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com2tag:blogger.com,1999:blog-8619358898227991199.post-30133076964138883442010-02-15T21:26:00.003+02:002011-03-06T14:52:01.751+02:00Великий піст 2010. Що можна їсти?<div dir="ltr" style="text-align: left;" trbidi="on"><span class="Apple-style-span" style="color: red;">UPD</span>: Оновлену схему посту для 2011 року можна подивитись <a href="http://korneliuk.blogspot.com/2011/03/2011.html">тут</a>.<br />
<br />
Сьогодні, 15 лютого, розпочався Великий піст. Для мене це привід змінити себе на краще, <br />
поборотися зі шкідливими звичками. І хоча всі кажуть, що піст – це не дієта, все ж утримання від їжі - це головний атрибут посту.<br />
<br />
<span class="Apple-style-span"><span class="Apple-style-span" style="font-size: x-large;">Яку їжу можна вживати в піст</span></span><br />
<br />
Є різні правила. Біль та менш строгі. Я користуюсь тими, що описані на <a href="http://www.sestry.ru/church/content/vpost">сторінці</a> сайту <a href="http://www.sestry.ru/">http://www.sestry.ru</a>. Там я знайшов і цю зручну схему:<br />
<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKOJm3h1swm8oqYmxsQlZyJlwRaTgT2_Ks2XyjGzy6eyEbudwoz8DO0bKMdopHypTATTdcdSYk9rpx7vFYpgOI9akboOsmzk_GzgpGLZb6yhhTACmDlfnxj7CsCaCOF9G2zc36lpp_zoE-/s1600-h/vpost_img.jpg" imageanchor="1" style="display: inline !important; margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKOJm3h1swm8oqYmxsQlZyJlwRaTgT2_Ks2XyjGzy6eyEbudwoz8DO0bKMdopHypTATTdcdSYk9rpx7vFYpgOI9akboOsmzk_GzgpGLZb6yhhTACmDlfnxj7CsCaCOF9G2zc36lpp_zoE-/s640/vpost_img.jpg" width="384" /></a></div><br />
Отже оcновні правила такі:<br />
<ul><li>Не можна їсти м'ясо, рибу, яйця, молоко та інші продукти тавринного походження.</li>
<li>У перші два дні посту (15 та 16 лютого) та в передостанній день (2 квітня) - строгий піст. Рекомендується повне утримання від їжі, або невелика кількість пісної їжі.</li>
<li>В середу та п'ятницю - «сухояденіє». Не можна вживати ні варену, ні приготовану на пару їжу. Не можна вживати олію. Дозволяються хліб, свіжі, сушені й квашені овочі та фрукти.</li>
<li>В інші дні дозволена рослинна олія.</li>
<li>В суботу, в неділю а також в свята 25 лютого, 9 березня та 22 березня можна вживати морепродукти. </li>
<li>В Вербну неділю (28 березня) можна їсти рибу. А в Лазареву суботу (напередодні Вербної неділі) можна їсти ікру.</li>
</ul><div>І наостанок, слід пам'ятати, що головне чого не можна їсти - це своїх ближніх.</div></div>Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com3tag:blogger.com,1999:blog-8619358898227991199.post-64852802993861897612010-01-24T01:52:00.006+02:002012-08-27T14:19:00.641+03:00Null arguments in a constructor. To check or not to check.We've had interesting discussion recently, on whether to check constructor arguments on nulls. I want to share my thought on this.<br />
<br />
Our coding convention has a rule stating that all public methods arguments should be checked for nulls. So our methods usually looks like this one:<br />
<pre class="brush: csharp">public void SomeMethod(string arg)
{
Precondition.Argument.NotNull(arg, "arg");
// Real code comes here...
}
</pre>And I'm quite happy with such <a href="http://en.wikipedia.org/wiki/Design_by_Contract">DbC</a> like coding style. But... with one exception.<br />
<br />
<i>I don't like to write code checking for null dependencies in a service constructor.</i> For two main reasons:<br />
<br />
<ol><li><b>It is easier to write tests for the service. </b>In case when particular dependency is not used in test it is easier to pass null instead of creating stub dependencies only to satisfy checks.</li>
<li><b>It useless in case when all services are instantiated through <a href="http://en.wikipedia.org/wiki/Dependency_injection">DI container</a>.</b> That is because, as far as I know, most DI containers will throw an exception by self when it can't find dependency for the service.</li>
</ol>I think code will show my point better. Suppose we need a service to login users. Service responsibility will be to retrieve user by the name provided, verify that provided password is correct and to set the user as a current user in the application. In case when supplied user name or password are incorrect, service should log appropriate message.<br />
<br />
So service will need some repository to retrieve users, some service to authenticate user and a logger to log messages in case when incorrect user credentials specified.<br />
<br />
I'll disregard TDD practice and will show the code before test (shame on me). So the LoginService code:<br />
<pre class="brush: csharp; gutter: false;">public class LoginService : ILoginService
{
readonly IUserRepository userRepository;
readonly IAuthenticationService authenticationService;
readonly ILog log;
public LoginService(
IUserRepository userRepository,
IAuthenticationService authenticationService,
ILog log)
{
// This is the subject of discussing.
Precondition.Argument.NotNull(userRepository, "userRepository");
Precondition.Argument.NotNull(authenticationService, "authenticationService");
Precondition.Argument.NotNull(log, "log");
this.userRepository = userRepository;
this.authenticationService = authenticationService;
this.log = log;
}
public bool Login(UserCredentials userCredentials)
{
Precondtion.Argument.NotNull(userCredentials);
var user = this.userRepository.FindByName(userCredentials.Name);
if (user == null)
{
this.log.Info("Login attempt failed due to invalid user name");
return false;
}
if (!this.authenticationService.PasswordMatches(user, userCredentials.Password))
{
this.log.Info("Login attempt failed due to invalid password");
return false;
}
SetCurrentUser(user);
return true;
}
}
</pre>Lets write a test for scenario when incorrect user name is provided. For this scenario we don't need IAuthenticationService.<br />
<pre class="brush: csharp; gutter: false;">[Subject(typeof(LoginService))]
public class when_unknown_user_name_provided
{
const string UnknownUserName = "Unknown user";
User user;
UserCredentials credentials;
LogSpy logSpy;
LoginService loginService;
bool loginResult;
Establish context = () =>
{
credentials = new UserCredentials(UnknownUsername, "stub password");
var userRepository = MockRepository.GenerateStub<IUserRepository>();
userRepository.Stub(x => x.FindByName(UnknownUserName).Return(null);
// We really do not need an authenticationService for this test.
// But should stub it only to satisfy null check constraints.
// Isn't it easier and more readable to use such obviuos construction?
// var authenticationServiceNotUsed = null;
var authenticationServiceNotUsed =
MockRepository.GenerateStub<IAutenticationService>();
logSpy = new LogSpy();
loginService = new LoginService(
userRepository, authenticationServiceNotUsed, LogSpy);
};
Because of = () => loginResult = loginService.Login(userCredentials);
It should_indicate_that_user_login_failed = () => loginResult.ShouldBeFalse();
It should_not_set_current_user = () => User.Current.ShouldBeNull();
It should_log_invalid_login_attempt = () =>
logSpy.InfoMessage.ShouldBeEqualTo(
"Login attempt failed due to invalid user name");
}
</pre>And wait, as far as I have serviceAuthentication instance I might want to ensure that it was not called within a test:<br />
<pre class="brush: csharp">It should_not_use_authentication_service = () =>
authenticationService.AssertWasNotCalled(
x => x.PasswordMatches(Arg<User>.Is.Anyting, Arg<string>.Is.Anything));
</pre>Seems like a waste of time. Intent is more clear when null authenticationService is used. That's all about testing challenge.<br />
<br />
Now lets me return back to my statement that such null checks in a constructor are useless. So what is the advantage they might give? Earlier problem discovering by throwing ArgumentNullException in a constructor, instead of NullReferenceException somewhere while executing method later. I believe that in production code such services should not be instantiated directly by hand, but resolved with DI container (in a <a href="http://ayende.com/Blog/archive/2010/01/22/rejecting-dependency-injection-inversion.aspx">recent Ayende's post</a> you can see a very nice picture of dependencies graph). As I've already said, most of DI containers will throw an exception itself if it can't find dependency. But with DI containers even better approach is to test container configuration. For given example test may look like: <br />
<pre class="brush: csharp">[Subject(typeof(ApplicationConfiguration))]
public class when_configuring
{
Because of = () => ApplicationConfiguration.Initialize();
It should_initialize_service_locator = () =>
ServiceLocator.Current.ShouldNotBeNull();
It should_configure_login_service = () =>
ServiceLocator.Current.GetInstance<ILoginService>().ShouldNotBeNull();
}
</pre>With this approach we get even earlier problem discovering.<br />
<br />
<b>Summary</b>: This post explains why using null arguments checks in services constructors is not a good idea. The better approach is letting them be null for testing purposes and testing you DI container configuration to ensure that services may be resolved in a run-time.<br />
<br />
<b>Note:</b> Tests are using syntax of the awesome Machine.Specifications framework which you can find <a href="http://github.com/machine/machine.specifications">here</a>. All the code examples were written without any code editor. So I expect it contains errors.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com2tag:blogger.com,1999:blog-8619358898227991199.post-82318094181510710732009-12-29T01:23:00.004+02:002010-01-24T11:28:52.975+02:00Configuring Log4Net AdoNetAppender with a connection string from application configuration fileI read Ben Scheirman's "<a href="http://flux88.com/blog/changing-log4net-connection-string-at-runtime">Changing log4net connection string at runtime</a>" post and wanted to leave a comment describing solution I use to solve log4net ConnectionString configuring problem. But as I wanted to show some code, it might be better to make a blog post.<br />
<br />
So the problem I faced with was:<br />
<blockquote>How to configure <a href="http://logging.apache.org/log4net/index.html"><span style="color: black;"><span class="Apple-style-span" style="text-decoration: none;">log4net</span></span></a> AdoNetAppender to use connection string specified in a <connectionStrings> section of an application configuration file?</blockquote>There is no obvious way to do this in a current release of log4net. But <a href="https://issues.apache.org/jira/browse/LOG4NET-88">such issue</a> is already <a href="http://svn.apache.org/viewvc/logging/log4net/trunk/src/Appender/AdoNetAppender.cs?r1=607475&r2=607748&diff_format=h">fixed</a> in a log4net trunk, thus it will be possible to do so in the next release.<br />
<br />
I don't really like to use the code which not yet officially released. So I use a simple workaround - a class inherited from AdoNetAppender with a new property ConnectionStringName. The code is pretty obvious:<br />
<br />
<pre class="brush: csharp">using System.Configuration;
using log4net.Appender;
using log4net.Core;
/// <summary>
/// Allows to use a connection string specified in the
/// <see cref="ConfigurationManager.ConnectionStrings"/> section of an application
/// configuration file.
/// </summary>
/// <remarks>
/// Such feature is already implemented in a log4net trunk (see
/// https://issues.apache.org/jira/browse/LOG4NET-88). But it is not yet released.
/// <para/>
/// TODO: Remove this class as soon as log4net 1.2.11 will be released.
/// </remarks>
public class Log4NetConnectionStringNameAdoNetAppender : AdoNetAppender
{
private string connectionStringName;
/// <summary>
/// Gets or sets the name of the connection string from the application
/// configuration file.
/// </summary>
/// <value>The name of the connection string.</value>
public string ConnectionStringName
{
get
{
return this.connectionStringName;
}
set
{
this.connectionStringName = value;
if (!string.IsNullOrEmpty(this.connectionStringName))
{
var settings =
ConfigurationManager.ConnectionStrings[this.connectionStringName];
if (settings == null)
{
string msg = string.Format(
"Unable to find [{0}] ConfigurationManager.ConnectionStrings item",
this.connectionStringName);
throw new LogException(msg);
}
this.ConnectionString = settings.ConnectionString;
}
}
}
}
</pre><br />
And the log4net configuration looks like this:<br />
<pre class="brush: xml"><appender name="DBAppender" type="NamespaceWhereClassLives.Log4NetConnectionStringNameAdoNetAppender,AssemblyContainingClass">
<connectionStringName value="ConnectionStringNameFromConfigFile"/>
<!-- Other parameters -->
</appender>
</pre>This looks like a hack, but it works for me pretty well.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com3tag:blogger.com,1999:blog-8619358898227991199.post-22666620340161179972009-10-14T23:53:00.000+03:002009-10-14T23:53:38.107+03:00На Украине? На Росії!<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj06AfS_Q2MWysY7UE7gNnLdidnlcwXgEK50PJxSK4koeZUDLsNOqCM7N9Ak_-DDCjZoGukeTm2Gl42pWkxszWS6ncub247WscBOnvLPlsgXzc1WTZOXAX-38EN9Mg8XTcTzgEbk8KKxbv9/s1600-h/Map_of_Kiev_Rus'.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj06AfS_Q2MWysY7UE7gNnLdidnlcwXgEK50PJxSK4koeZUDLsNOqCM7N9Ak_-DDCjZoGukeTm2Gl42pWkxszWS6ncub247WscBOnvLPlsgXzc1WTZOXAX-38EN9Mg8XTcTzgEbk8KKxbv9/s200/Map_of_Kiev_Rus'.png" /></a><br />
</div>Вчора дізнався, що фінською правильно вживати <i>"Я був </i><b><i>на Росії</i></b><i>"</i> (Minä olin Venäjallä), але <i>"Я живу в Україні" </i>(Minä asun Ukrainassa).<br />
<br />
На відміну від інших країн, для Росії, в фінській мові використовуються <a href="http://en.wikipedia.org/wiki/Finnish_language_noun_cases#External_Locatives">зовнішні місцеві відмінки</a>. Приклади <a href="http://en.wiktionary.org/wiki/Ven%C3%A4j%C3%A4#Usage_notes">тут</a>.<br />
<br />
PS. Взагалі то я не дуже переймаюсь тим як саме кажуть росіяни - <i>"на Украине"</i> чи <i>"в Украине"</i>. Мені це навіть імпонує трохи. Все таки підкреслюється унікальність нашої країни. Писав же Некрасов <a href="http://ru.wikipedia.org/wiki/%D0%9A%D0%BE%D0%BC%D1%83_%D0%BD%D0%B0_%D0%A0%D1%83%D1%81%D0%B8_%D0%B6%D0%B8%D1%82%D1%8C_%D1%85%D0%BE%D1%80%D0%BE%D1%88%D0%BE">"Кому на Руси жить хорошо"</a>. Так от я подумав, може це <i>"на"</i> нам у спадок від Київської Русі дісталась?<br />
<br />
Та не про те хотів сказати. Я не знаю як правильно вживати "в Україні" російською мовою. Але я <a href="http://mova.kreschatic.kiev.ua/41.htm#1">знаю</a>, що українською правильно казати: "Я живу <b>в Україні</b>".Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-39872532748288769272009-10-14T21:45:00.002+03:002009-10-14T23:56:43.368+03:00За кого голосувати на виборах?Послухайте <a href="http://sashko.com.ua/?p=10">чудову пісню</a> Сашка Положинського. Мене дуже порадувала.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com2tag:blogger.com,1999:blog-8619358898227991199.post-27709937475448292032009-09-30T09:34:00.004+03:002009-10-13T23:23:18.112+03:00Майкл К. Физерс. Эффективная работа с унаследованным кодом<a href="http://www.blogger.com/"></a><span id="goog_1254291409403"></span><span id="goog_1254291409404"></span><br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy0_uLqDjYuI8ZHmmvqPJtuYyCFphKRXbm0R41mKDRRjmsm_gPKx6pkBOyItzUo8R0-YdMmkTx45RDxzHaagUITr8WzInZ1j2eFWsohHHPqxnaZQtiaBNb4I5_9Rxk2FQVYJRjZ3K9SYc7/s1600-h/FWELC.jpg" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiy0_uLqDjYuI8ZHmmvqPJtuYyCFphKRXbm0R41mKDRRjmsm_gPKx6pkBOyItzUo8R0-YdMmkTx45RDxzHaagUITr8WzInZ1j2eFWsohHHPqxnaZQtiaBNb4I5_9Rxk2FQVYJRjZ3K9SYc7/s320/FWELC.jpg" /></a><br />
</div>Останнім часом я зустрічав багато посилань на книгу <a href="http://www.williamspublishing.com/Books/978-5-8459-1530-6.html">"Эффективная работа с унаследованным кодом"</a> із дуже позитивним відгуками. Тому сподівався, що книга мені сподобається. Але прочитавши її, мої очікування не виправдались. Навпаки, книга здалась мені дуже нудною. Не знаю чия це вина, автора, перекладачів, чи мого сприйняття. Багато сторінок, я просто переглядав по діагоналі. Часто ловив себе на тому, що прочитавши якийсь розділ нічого не зрозумів, бо думки гуляли деінде, а перечитувати ніякого бажання немає. І взагалі не розумію, як можна стільки уваги приділити питанню - як розірвати залежність між кодом, і жодним словом не обмовитись про <a href="http://en.wikipedia.org/wiki/Dependency_injection">Dependecy Injection Containers (IOC)</a>. А, точно, це ж успадкований код. До того ж судячи з книги, більшість успадкованого коду - це код на C та C++. Але мене це мало цікавить.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-91522324770459873162009-09-27T16:12:00.008+03:002010-01-23T16:19:13.137+02:00High/Low generator with SQLIn current project, we use High/Low generator strategy to generate identifiers for entities. (Fabio Maulo wrote a good explanation <a href="http://fabiomaulo.blogspot.com/2008/12/identity-never-ending-story.html">why using identity is a bad thing</a>). The question I sometimes see in NHibernate forums is what to do if some other applications also use the same database. Usual answer is - implement some service which would generate id, and use it in that applications.<br />
Yesterday I met similar issue. What I needed to do is to insert some fake data using SQL only. For that purpose I wrote such stored procedure:<br />
<br />
<pre class="brush: sql">IF OBJECT_ID('dbo.GenerateHiLoIdRange') IS NOT NULL
DROP PROCEDURE dbo.GenerateHiLoIdRange
GO
CREATE PROCEDURE dbo.GenerateHiLoIdRange (
@IdsCount INT
)
AS
/* =============================================================== */
/* == Generates a range of identifiers using High/Low strategy. == */
/* =============================================================== */
BEGIN
SET NOCOUNT ON;
IF @IdsCount <= 0
BEGIN
RAISERROR('@IdsCount should be positive. But was %d', 16, -1, @IdsCount)
RETURN(-100);
END
DECLARE @NextHi INT
DECLARE @MaxLo INT
SET @MaxLo = 100 -- Hardcoded value used over all tables in a database.
BEGIN TRANSACTION UpdateHiLoTable
SET @NextHi = (
SELECT next_hi
FROM hibernate_unique_key WITH (UPDLOCK, ROWLOCK)
)
UPDATE hibernate_unique_key
SET next_hi = @NextHi + 1 + (@IdsCount - 1) / @MaxLo
WHERE next_hi = @NextHi
COMMIT TRANSACTION UpdateHiLoTable
SELECT @NextHi * @MaxLo + IntValue
FROM dbo.Integers(@IdsCount - 1)
END
GO
</pre><br />
<div style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;">Main points of procedure:<br />
</div><ul><li>It uses hard coded MaxLo value (100) , which is used over all entities in our application.</li>
<li>Calculates new NextHi value based on requested identifiers count and updates <span style="font-family: monospace; white-space: pre;">hibernate_unique_key</span> table in transaction.</li>
<li>To return a result set containing generated identifiers procedure needs some sort of "Integers" table. In my case i simulate such table with a table-valued function.</li>
</ul><pre class="brush: sql">IF OBJECT_ID('dbo.Integers') IS NOT NULL
DROP FUNCTION dbo.Integers
GO
CREATE FUNCTION dbo.Integers (
@MaxValue INT
)
RETURNS @Integers TABLE (IntValue INT NOT NULL)
AS
/* =============================================================== */
/* == Returns the [0, @MaxValue] range of integer values. == */
/* =============================================================== */
BEGIN
IF @MaxValue > 9999
BEGIN
RETURN;
END;
DECLARE @Digits TABLE
(
Digit INT NOT NULL PRIMARY KEY
)
INSERT INTO @Digits (Digit) VALUES (0)
INSERT INTO @Digits (Digit) VALUES (1)
INSERT INTO @Digits (Digit) VALUES (2)
INSERT INTO @Digits (Digit) VALUES (3)
INSERT INTO @Digits (Digit) VALUES (4)
INSERT INTO @Digits (Digit) VALUES (5)
INSERT INTO @Digits (Digit) VALUES (6)
INSERT INTO @Digits (Digit) VALUES (7)
INSERT INTO @Digits (Digit) VALUES (8)
INSERT INTO @Digits (Digit) VALUES (9)
INSERT INTO @Integers
SELECT Number
FROM
(SELECT
Thousands.Digit * 1000 +
Hundreds.Digit * 100 +
Tens.Digit * 10 +
Ones.Digit AS Number
FROM
@Digits Thousands
CROSS JOIN @Digits Hundreds
CROSS JOIN @Digits Tens
CROSS JOIN @Digits Ones
) Integers
WHERE Number <= @MaxValue
ORDER BY Number
RETURN;
END
GO
</pre>Note that number of integers in a function is limited to 10000. But that is what I need.<br />
<br />
Now I can generate identifiers in t-sql batches:<br />
<br />
<pre class="brush: sql">CREATE TABLE #TempId (Id INT NOT NULL)
SELECT * FROM hibernate_unique_key
INSERT INTO #TempId
EXEC dbo.GenerateHiLoIdRange 250
SELECT Id AS Id,
'PersonName' + CAST(ROW_NUMBER() OVER(ORDER BY Id) - 1 AS VARCHAR(255)) AS PersonName
FROM #TempId
DROP TABLE #TempId
</pre><br />
In my sandbox database I see such results:<br />
<div class="separator" style="clear: both; text-align: center;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnr73WnHAsjuYZMfrcFN1ENWXkQARjFkZeZdZVsC6AFS4Nz2BvsFtLWjbkAb0q9d3oJ9Xt1rhHu4AVUOiWlnxx9JeQDhd7d0aryKCbHwcu32jRxJjV1E0x9Q7u18nSQrJ8VY8DEDGgicgP/s1600-h/HiLoGeneratorResults.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhnr73WnHAsjuYZMfrcFN1ENWXkQARjFkZeZdZVsC6AFS4Nz2BvsFtLWjbkAb0q9d3oJ9Xt1rhHu4AVUOiWlnxx9JeQDhd7d0aryKCbHwcu32jRxJjV1E0x9Q7u18nSQrJ8VY8DEDGgicgP/s400/HiLoGeneratorResults.png" /></a><br />
</div>Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com1tag:blogger.com,1999:blog-8619358898227991199.post-82770151240535383402009-06-29T20:41:00.007+03:002009-06-29T21:03:45.681+03:00Firefox - пожирач пам'яті<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimOM2wtIHrQ_XhuS5mldEAfDuwSmOFdE2jXE3mvGqefIIGhSEalGanAzXHXG1fPpHHw4xidrIr8K20yBQXYZmbIUn2t1K6MjRKF8iE2ILdNkhjrhHb7yr-5Jqo9gn7sOSTxUqU7ZGnZwRr/s1600-h/firefox.png"><img style="cursor: pointer; width: 400px; height: 257px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEimOM2wtIHrQ_XhuS5mldEAfDuwSmOFdE2jXE3mvGqefIIGhSEalGanAzXHXG1fPpHHw4xidrIr8K20yBQXYZmbIUn2t1K6MjRKF8iE2ILdNkhjrhHb7yr-5Jqo9gn7sOSTxUqU7ZGnZwRr/s400/firefox.png" alt="" id="BLOGGER_PHOTO_ID_5352811162481116274" border="0" /></a><br /><br />Це через кілька хвилин після того як, здавалося б, закрив firefox. Куди стільки пам'яті можна жерти?Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-61147330263070270822009-06-28T15:13:00.005+03:002009-06-28T15:50:21.634+03:00Linq в .Net Framework 2.0<span style="font-weight: bold;">Проблема:</span> Клієнти живуть на Windows 2000, на яку можна поставити лише .Net Framework 2.0. Але в проекті хочеться використовувати Linq. Чи це можливо?<br /><span style="font-weight: bold;"><br />Рішення</span>: Можна! <a href="http://code.google.com/p/linqbridge/downloads/list">Завантажуємо</a> чудову бібліотеку <a href="http://www.albahari.com/nutshell/linqbridge.aspx">LinqBridge</a> і з радістю використовуємо Linq.<br /><br />Що саме містить ця бібліотека:<br /><ul><li>Власну реалізацію всіх Linq операторів для Enumerable класу із Framework 3.5, що саме і дозволяє використовувати Linq to Objects в .Net Framework 2.0.<br /></li><li>Реалізує generic делегати <span style="font-weight: bold;">Func</span> та <span style="font-weight: bold;">Action</span>.<br /></li><li>Реалізує <span style="font-weight: bold;">ExtensionAttribute</span>, який дозволяє використовувати extension methotds в .Net Framework 2.0.</li></ul>Однак це не стосується <span style="font-weight: bold;">Linq to Sql </span>та <span style="font-weight: bold;">Linq to Xml</span>, які ця бібліотека не реалізує.<br /><br />Детально про LinqBridge, і про те чому це можливо <a href="http://www.albahari.com/nutshell/linqbridge.aspx">читайте в статті автор</a>а, якому я дуже вдячний за чудову роботу.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-1767323999867764802009-02-15T23:35:00.003+02:002009-10-13T23:23:18.112+03:00Харпер Лі. Убить пересмешника.В романі <a href="http://ru.wikipedia.org/wiki/%D0%9B%D0%B8,_%D0%A5%D0%B0%D1%80%D0%BF%D0%B5%D1%80">Харпер Лі</a>, очима маленької дівчинки, показано життя Америки на початку минулого століття.<br /><br />Читається дуже легко, чимось нагадало "Пригоди Тома Сойєра" Марка Твена.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-35718376350567574132009-02-15T09:34:00.004+02:002009-10-13T23:23:18.112+03:00Вернор Виндж. Пламя над бездной.В романі автор пропонує дуже цікавий погляд на будову всесвіту. Він поділяється на зони - від центра до краю. В центрі - безодня, в якій розум неможливий. Чим ближче до краю, тим технологічнішими можуть бути цивілізації. В Краї, швидкість світла не є обмеженням. За краєм цивілізації стають Силами (я так і не зрозумів що це таке).<br /><br />Розпочинається книга, тим, що представники людської цивілізації, десь в Краї, знайшли та розворушили якийсь архаїчний архів, і цим розбудили Зло, яке собі спокійно дрімало мільйони років. Корабель, в якому, як виявилось була протидія, встиг втекти і потрапив на планету в повільній зоні.<br /><br />Надалі, однією з основних сюжетних ліній є опис цивілізації собакоподібних зграй, які населяють ту планету, де приземлився космічний корабель з утікачами. Ці собаки можуть бути розумними лише коли об'єднані в зграю. І вся зграя - це одна особистість. <br /><br />Щоб активувати протидію Злу, споряджена експедиція. Пригоди та стосунки членів екіпажу: жінки, чоловіка, який певним чином пов'язаний із Силою, та двох розумних рослин на візках складають іншу сюжетну лінію.<br /><br />І все це переплітається повідомленнями із груп новин пародійної всесвітньої мережі "мільйонів неправд".<br /><br />Мені роман сподобався своєю незвичністю. Тепер собак шкода, як же їм, бідним "синглетам" живеться.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-46119672129479311942009-02-02T23:10:00.003+02:002009-10-13T23:24:26.844+03:00Герберт Уэллс. Машина времени.<a href="http://lib.aldebaran.ru/author/uyells_gerbert/uyells_gerbert_mashina_vremeni/">Ця книга</a> давно була в мене в readlist'і. Нарешті дійшли руки її прочитати. Перед тим як її завантажити, прочитав кілька дуже схвальних відгуків. І мабуть тому від книги очікував більше. Але прочитав із задоволенням, швидко. Перед сном і з ранку в метро.<br />Головною темою книги є питання соціальної нерівності, яке автор показав в незвичному ракурсі.<br /><br />PS. Я думаю, що людству до 8000 року не дотягнути.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-69122157160383412952009-02-01T07:35:00.005+02:002009-10-13T23:24:26.845+03:00Стивен Хокинг и Леонард Млодинов. Кратчайшая история времени.<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://amphora.ru/book.php?id=832"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 214px; height: 320px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgWFVqqusYVgtUVPqcm33mkxGwkSUOmnmb-WEUlLkiK5nBg5F58LV9iUCeOEzgoE61rZBUS1Ngitvym502CUy8qlWR9w_B8lu8JpTT_GMx-LRvzHd4UV-2rGADVn44pihzc9jMP-1H6pa5F/s320/A+Briefer+History+of+Time.jpg" alt="" id="BLOGGER_PHOTO_ID_5297699481748537986" border="0" /></a> Прочитав <a href="http://amphora.ru/book.php?id=832">чергову книгу</a> найвідомішого (принаймні для мене) фізика сучасності.<br /><br />В цій книзі автор намагається, іще доступніше ніж в своїх попередніх книгах, пояснити будову всесвіту, з точки зору сучасної фізики.<br /><br />І дійсно складні речі в книзі пояснені дуже доступно і з цікавими прикладами. Прочитав із великим задоволенням.<br /><br />P.S. Але така хитрість автора зі мною не спрацювала. Все рівно нічого не розумію.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-28591591405346596742008-10-30T07:17:00.007+02:002009-02-15T09:33:53.956+02:00Unit testing #2. NUnit constraint-based AssertsПочинаючи з версії 2.4 в NUnit реалізовано новий стиль assert'ів. Ця модель використовує один метод Assert.That(args) для всіх тверджень. Логіка виконання кожного твердження перенесена в другий параметр. Ось декілька прикладів:<br /><pre class="csharp">Assert.That(10.0/3.0, Is.EqualTo(3.33).Within(0.01f));<br />Assert.That(2*2, Is.Not.EqualTo(5));<br />Assert.That(new object(), Is.Not.Null);<br />Assert.That(string.Empty, Is.Empty);<br />Assert.That("abc", Has.Length(3));<br />Assert.That(new string[] {"a", "b", "c"}, Is.All.Not.Null);<br /></pre>Також можна комбінувати constraints.<br /><pre class="csharp">Assert.That(3, Is.LessThan( 5 ) | Is.GreaterThan( 10 ));<br />Assert.That("s", !Is.Null));</pre>Для того щоб працювати з новим стилем потрібно використати:<br /><pre class="csharp">using NUnit.Framework.SyntaxHelpers;<br /></pre>Цей стиль мені подобається більше ніж <a href="http://www.nunit.org/index.php?p=classicModel&r=2.4.8">класичний</a>.<br /><br />Детальніше - на сторінці <a href="http://www.nunit.org/index.php?p=constraintModel&r=2.4.8">Constraint-Based Assert Model </a>документації NUnit .Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-29024178384028130742008-10-29T08:07:00.010+02:002008-10-29T09:23:06.409+02:00Unit testing #1<a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgM5b4eCPAepeQGRe9Ryxmj-_3Linvmh4EFWC2JrEeaiAFh3SWFWpChyphenhyphentnQQmbkncUQTHjJcc2nS1PbUfTZRe3k2ctvv60Ou_yIUH_hyphenhyphen8pdYs7TZQKzRa1POlkkffAKy5ytFAmz1PD5fQIn/s1600-h/utc2.jpg"><img style="margin: 0pt 10px 10px 0pt; float: left; cursor: pointer; width: 190px; height: 228px;" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgM5b4eCPAepeQGRe9Ryxmj-_3Linvmh4EFWC2JrEeaiAFh3SWFWpChyphenhyphentnQQmbkncUQTHjJcc2nS1PbUfTZRe3k2ctvv60Ou_yIUH_hyphenhyphen8pdYs7TZQKzRa1POlkkffAKy5ytFAmz1PD5fQIn/s320/utc2.jpg" alt="" id="BLOGGER_PHOTO_ID_5262458498435631602" border="0" /></a><br />Я знову вирішив щось написати. Справа в тому, що я почав читати книгу <a href="http://www.pragprog.com/titles/utc2/pragmatic-unit-testing-in-c-with-nunit">"Pragmatic Unit Testing in C# with NUnit"</a> by Andy Hunt and Dave Thomas with Matt Hargett. І ось, вночі, коли я прочитав перші сторінки, я подумав - "Круто, прочитав лише кілька сторінок, а вже виніс для себе чотири нових речі про які не знав раніше. А наступного дня, коли на роботі розказував, що книжка цікава, що я дізнався чотири нових штучки, ніяк не міг згадати, а про яку ж все-таки четверту річ я дізнався. От три пам'ятаю а четверту ні. Отже треба ці знання записувати. Тут їм і буде місце.<br /><br /><span style="font-weight: bold;"><br /><br />Тіп #1. "Ommit the </span><span style="font-style: italic; font-weight: bold;">message</span><span style="font-weight: bold;"> string for reporting unless you really need to".</span><br /><br />Ось класичний синтаксис Assert в NUnit:<br /><pre>Assert.AreEqual(expected, actual [, string message])</pre>Автори рекомендують не включати текстовий рядок для пояснення тесту, крім випадків коли це дійсно необхідно. Краще коли ім'я тесту самостійно пояснює призначення самого тесту, коли використовуються доречні методи Assert'а, або тест розбивається на декілька методів, щоб залишатися сконцентрованими на конкретних тестових випадках.<br /><br />Що ж в цій практиці революційного і чому вона нова для мене? Не знаю. Просто я чомусь часто писав ці текстові пояснення в NUnit Assert'ах. Мабуть це був вплив StyleCop, який змушує завжди писати пояснення для методу System.Diagnostics.Deиug.Assert. А тепер не буду, звісно крім випадків, коли це дійсно необхідно :).Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-74416155258136112008-07-02T23:00:00.003+03:002009-10-13T23:26:39.123+03:00Двоколісний другНарешті купив мотик. Yamaha YBR 125. Або просто "йобр", чи "юбка". Уже встиг впасти :). Фотки потім.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com2tag:blogger.com,1999:blog-8619358898227991199.post-35945262203788221452008-06-02T22:41:00.003+03:002009-10-13T23:28:58.322+03:00Інвестиційний портфельНатрапив на сайт <a href="http://www.investportfel.com">Мой инвестиционный портфель</a>, який дозволяє контролювати та аналізувати свої інвестиційні вклади. Я для таких цілей використовував spreadsheets в Google docs. Мушу визнати що користуватися новим ресурсом значно простіше.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-60551037436512452882008-04-11T00:05:00.003+03:002009-10-13T23:26:55.944+03:00Як перезавантажити IPodЩо робити коли завис IPod? Перезавантажити. Як це зробити описано <a href="http://docs.info.apple.com/article.html?artnum=61705-ru">тут</a>.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0tag:blogger.com,1999:blog-8619358898227991199.post-25967951361240781872008-03-31T22:53:00.002+03:002009-10-13T23:23:18.113+03:00Николай Гаврилович Чернышевский: Что делать?<blockquote>Кто не изучил человека в самом себе, никогда не достигнет глубокого знания людей.</blockquote>Сильний твір! Розумний і непростий. Чомусь не можу дібрати слів, щоб описати про що саме ця книга. Мабуть про стосунки людей, про місце особи в суспільстві, про особистість... Не знаю, якось складно передати враження від роману в двох словах. Та мабуть цього й не варто робити...<br />Комусь твір може здатись неактуальним для нашого часу. Так я з кимось не погоджусь. Одним словом, раджу читати — книга точно не залишить Вас байдужим.Ivan Korneliukhttp://www.blogger.com/profile/12117983842614711809noreply@blogger.com0