Search This Blog

Tuesday, June 7, 2016

Deep clone example

Deep-cloning example:

 public class RechivHafrasha : IEqualityComparer<RechivHafrasha>
    {
        public decimal Schum { get; set; }
        public decimal? Shiur { get; set; }
 
        //public int? Status { get; private set; }
        public int Sug { get; set; }
 
        public decimal Sachar { get; private set; }
        public string MisparMezaheReshuma { get; set; }
 
        public string MisparMezaheReshumaKodem { get; set; }
 
        public RechivHafrasha()
        {
        }
 
        public RechivHafrasha(decimal schumHafrasha, decimal? shiurHafrasha, /*int? status,*/ int sugHafrasha, decimal sachar, string misparMezaheReshuma, string misparMezaheReshumaKodem)
        {
            Schum = schumHafrasha;
            Shiur = shiurHafrasha;
            //Status = status;
            Sug = sugHafrasha;
            Sachar = sachar;
            MisparMezaheReshuma = misparMezaheReshuma;
            MisparMezaheReshumaKodem = misparMezaheReshumaKodem;
        }
 
        /*
        * לצורך השוואת בין רשומות מאוחדות, כדי לא לשלוח כפילויות לפירעון
        */
 
        public string ID
        {
            get
            {
                return string.Format("{0}{1}{2}{3}{4}{5}",
                    this.Schum,
                    this.Shiur,
                    this.Sug,
                    this.Sachar,
                    this.MisparMezaheReshuma,
                    this.MisparMezaheReshumaKodem);
            }
        }
 
        public bool Equals(RechivHafrasha x, RechivHafrasha y)
        {
            return x.ID.Equals(y.ID);
        }
 
        public int GetHashCode(RechivHafrasha obj)
        {
            return obj.ID.GetHashCode();
        }
 
        public RechivHafrasha ShallowCopy()
        {
            var shallowCopy = this.MemberwiseClone() as RechivHafrasha;
            return shallowCopy;
        }
 
        public override string ToString()
        {
            return string.Format("[{0},{1},{2},{3},{4},{5}]", Schum, Shiur, Sug, Sachar, MisparMezaheReshuma, MisparMezaheReshumaKodem);
        }
    }
 
 
 
 
            List<RechivHafrasha> list = new List<RechivHafrasha>
            {
                new RechivHafrasha(10, 0.5m, 1, 1000, "7d2d272d7268326", "2d2862g2g2d"),
                new RechivHafrasha(100, 0.7m, 2, 900, "7d2d272d7268326", "2d2862g2g2d"),
                new RechivHafrasha(1000, 0.2m, 5, 80, "7d2d272d7268326", "2d2862g2g2d"),
                new RechivHafrasha(10000, 0.41m, 4, 500, "7d2d272d7268326", "2d2862g2g2d"),
                new RechivHafrasha(100000, 0.11m, 1, 15, "7d2d272d7268326", "2d2862g2g2d")
            };
 
            List<RechivHafrasha> c1 = list.ToList();
            List<RechivHafrasha> c2 = new List<RechivHafrasha>(list);
            List<RechivHafrasha> c3 = list.ConvertAll<RechivHafrasha>(z => z.ShallowCopy());
 
            list[1].Schum = 0;
 
            Console.WriteLine(string.Join(",\n", list));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c1));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c2));
            Console.WriteLine();
            Console.WriteLine(string.Join(",\n", c3)); 
Output:

Sunday, May 22, 2016

Improving performance in working with EF



I stumbled across a SO post about performance, where the main hindrance was Entity Framwork/DB operations.

Here's a summary of the suggested approaches for performance improvement:

  •  Using Dapper or raw ADO.NET components.
  • Disabling EF change auto detection and validation on save flags.
  • Using BulkInsert
  • Instead of adding new entities inside a loop, collect all the entity objects to be added into a collection - and one you are finished issue a single [entity].AddRange(collection) to push all the new entities into the EF object (and afterwards performing a SaveChanges). It is stated that this is performance-wiser.
I myself was familiar with the 2nd and 3rd suggestions. Should give Dapper and AddRange a try.

Some performance comparison tests for the above can be found in the web, here is a nicely presented one.

See also Performance considerations for EF 4, 5 and 6.

Monday, April 4, 2016

Web Development Fundamental Concepts

I found today concise yet very good descriptions of fundamental concepts in web development, by the Mozilla Developer Network.
A must for any web developer (I stumbled across this way too late, I admit).

https://developer.mozilla.org/en-US/Learn/Common_questions

You will find great articles clarifying what is the internet, a web server, web pages, web sites and more.

Update:
Web Fundamentals from Google.

Monday, February 22, 2016

C# Regex Positive Lookbehind

An example of positive lookbehind (Matches a group before the main expression without including it in the result):

            var str = @"System.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.ApplicationException: MQException is caught. Reason code: 2033, completion code: 2. ---> CompCode: 2, Reason: 2033
                       --- End of inner exception stack trace ---
                       at Multiconn.ZZZ.Web.Services.Port.EndProcess(IAsyncResult asyncResult)
                       at Multiconn.ZZZ.Demo.K.M.E(IAsyncResult asyncResult, WsEsParams1Response& result1, MyType result3)
                       --- End of inner exception stack trace ---";

            Regex r = new Regex(@"(?<=Reason code: )\d+");

            foreach (Match m in r.Matches(str))
                Console.WriteLine(m.Value);

Sunday, February 21, 2016

Impefect Content-Type http response headers

Have you heard about perfect and imperfect http headers ?

Well, I just did.
Take a look here, a topic that developed from here.

Monday, December 28, 2015

Make sure you are testing/playing with regular expressions in an environment that follows the same regular expression behavior of the programming language you are using

Upon posting this question, I learn that it is mandatory, whenever testing/playing with regexes, to make sure you are using an environment that follows/matches the Regular Expression behavior of the language you are programming with.

Otherwise things will not make sense.

Check this for online sandboxes for regex testing, per programming language.