Contents tagged with C#

  • One more summary of Moq library

    Examples of Mock usages

    Mock - is a simple and lightweight isolation framework, which is built on the basis of anonymous methods and expression trees. To create them it uses code generation, thus allowing to mock interfaces, virtual methods (and even protected methods) and does not allow to mock non-virtual and static methods.

    NOTE

    At market exists only two frameworks that allow to mock anything. These are TypeMockIsolator and Microsoft Fakes, available in Visual Studio 2012 and higher. These frameworks, unlike Mock ( which uses code generation) use CLR Profiling API, allowing to mock any method even for static, virtual and private methods. IMHO they are good for testing legacy code … more

  • C# and continue operator in cycles

    Hello everybody,

    today I want to write a notice about word continue in C#. 

    So, how it look like:

    for( int i = 0; i < 10; i++ )

    {

         if(i == 5)

         {

              continue;

         }

        //do some other staff

    }

    In this code, //do some other staff will be executed only 9 times, because for fifth it is written as continue. Continue can be compared to situation, when you bust some boxes, but then you see, that some box doesn't feet to your needs. Then you'll say to C# compiler, skip the box, and continue to bust another boxes. more

  • CQRS in in C# most trivial possible example

    Hello everybody,

    today I want to make compendium of CQRS for very simple case.

    So, at one of the projects I seen following way of implementing code:

    public class WriteModel

    {

    //common properties

    public int SomeEntity {get;set; } // ID of some entity

    }

    public class ReadModel : WriteModel

    {

    public string AdditionalProperty {get;set; }

    }

    and that way of implementing it was proudly named CQRS. To be honest I wasn't very agreeable on that part, so I decided to hear opinions of other people, not only mine understanding of what I read in book.

    And I'd like to save one of the comments not only at stackoverflow, but also at my web site.

    So, what's wrong with … more

  • Trivial implementation of File uploader

    Hello everybody,

    today I want to share with you fragment of code of html extension.

    Imagine that you edit your cshtml file, and see following code:

    @Html.EditorFor(model => model.SomeField

    Then you need to add some file upload and notice, that there is not file upload! 

    How can you fix this situation?

    I propose to use HtmlExtension code. For example like this:

    public static MvcHtmlString FileUpload(this HtmlHelper htmlHelper, string cssClass, object htmlAttrs = null) {

    var tb = new TagBuilder("input");

    tb.MergeAttribute("type", "file");

    tb.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttrs));

    if (!string.IsNullOrWhiteSpace(cssClass)) {

    tb. … more

  • Usage of Factor in R

    Hello everybody,

    today I want to make post about usage of factor in R.

    So, first of all, what is factor at all? 

    I was surprised to see that factor is very overloaded term and can have a lot of meanings in many areas.  So, for what factors can be used in R?

    First of all they can be used for storing categorical or nominal values. For example it can be convenient for saving gender, blood group, car type, etc. 

    So, let's say you wrote something like this: 

    cars.classes <- c("Coupe", "Cabriolet", "Sport", "Hatchback")

    For now cars.classes is not a factor, but just character vector. 

    If you execute this code in RStudio, and then execute in RStudio following line:

    cars.classes … more

  • Coercion in programming

    Hello everybody,

    just one short notice. Sometime in programming you can read word coercion. 

    You may be wondering what it means. 

    If to put simply, it is converting of one type into the other. 

    In C# coercion can be of two types: 

    implicit and explicit. 

    double a,b;

    a = 25; // implicit coercion

    b = (double) 7; //explicit coercion ( casting)

    more

  • The Adapter Pattern

    Hello everybody,

    today I want to make post about one of design patterns which is called Adapter. There are many articles over the web about this topic, but I hope to bring some clarity in this area.

    Before I proceed any further, let me describe situation, when you can need it. One of the cases for consideration can be option, if you have your own class, let's name it A. And then let's say that there is some other class B, which has some needed function for you. For example like this:

    How you can deal with this? One of the common ways which many people do, is to use class be directly in your code. For example like this:

    Aggregate class B from class A in some kind of function, and do … more

  • Moq and unit test

    Hello everybody,

    today I want to write few words about Moq and unit testing. 

    First of all you can have a question, why do I need Moq at all?

    Moq is especially useful for cases if we don't have desire to code interface, then code implementation of interface, and then code fake implementation. As usually those simple implementation are relatively simple so somebody ( as Moq team ) can organize code generation for your simple interface implementations.

    So, unit test is something that should follow 3 A:

    Arrange

    Act

    Assert

    General schema of Unit test implementation can be like this:

    [Test]

    public void SomeTest()

    {

    //Arrange

    var … more

  • Override Equals method of value types

    Hello everybody.

    Today I want to give some demo.

    using System;

    using System.Diagnostics;

    namespace StackOverflowQuest

    {

    class Program

    {

    struct StructTest

    {

    public string TestString { get; set; }

    //public override bool Equals(object obj)

    //{

    // var ct = (StructTest)obj;

    // return ct.TestString == this.TestString;

    //}

    }

    class ClassTest

    {

    public string TestString { get; set; }

    public override bool Equals(object obj)

    {

    var ct = obj as ClassTest;

    if (ct == null) … more

  • Search for duplicates

    Hello everybody,

    Here is just shorp version of application which searches for duplicates in folder.

    In  downloadable archive you can find my implementation of searcher with Task, ContinueWith and some other new features of C# 4.5 more