Tips and Tricks in a world of Mix

DRY don’t repeat yourself

YAGNI  – you aren’t gonna need it

KISS –  keep it short and simple

LoC – lines of code – Measuring software productivity by lines of code

SOLID

Initial Stands for Concept
S SRP [4] Single responsibility principle

class should have only a single responsibility (i.e. only one potential change in the software’s specification should be able to affect the specification of the class)

O OCP [5] Open/closed principle

“software entities … should be open for extension, but closed for modification.”

L LSP [6] Liskov substitution principle

“objects in a program should be replaceable with instances of their subtypes without altering the correctness of that program.” See also design by contract.

I ISP [7] Interface segregation principle

“many client-specific interfaces are better than one general-purpose interface.”[8]

D DIP [9] Dependency inversion principle

one should “Depend upon Abstractions. Do not depend upon concretions.”[8]

Very good source of courses used by Microsoft is now available for free for 6 month , including all it’s features.

All you need is an account and usage of the benefit.

1.Create Microsoft account

Capture
4.You’ll have the get the code and then you can activate the account of Pluralsight and use it.
Enjoy!
Capture2

When you need a test running during your process to insert data you may want to use task for async insertion.

Also you may run it parallel to your process from outside.

If you want to be able to stop you should implement Cancellation Token and put it inside the process as a stop condition for a while loop.

 


namespace AsyncTest
{
 public class InitTestPerSecond{
 public Task initData;
 public InitTestPerSecond()
 {
 initData = new Task()(
 ()=>
 {
 while(true)
 {
 MyDAL.InsertInTableEachSecond(DateTime.Now);
 Thread.Sleep(1000);
 }
 });
 initData.Start();
 }
}
 
namespace MyDAL
{
 public class MyDAL{
 public static void InsertInTableEachSecond(DateTime timestamp)
 {
 try{
 OracleConnection localConn = new OracleConnection(Utils.GetConnectionString());
 localConn.Open();
 int maxId;
 using (OracleCommand maxIdCommand = new OracleCommand("SELECT MAX(MYID) FROM SCHEMA_NAME.MYTABLE", localConn))
 {
 maxId = int.Parse(maxIdCommand.ExecuteScalar().ToString());
 }
 using (OracleCommand initMyData = localConn.CreateCommand())
 {
 
 initMyData = string.Format(" INSERT INTO SCHEMA_NAME.MYTABLE (MYID , COL1, DATE)
 VALUES 
 ({0}, 
 '{0} test ' ,
 {1})", ++maxId ,Utils.GetDateFormat(timeStamp) ));
 
 initMyData.ExecuteNonQuery();
 }
 localConn.Close();
 localConn.Dispose();
 }
 catch(Exception ex){
 throw ex;
 }
 }
 
 }
 
}
 
public class Utils{
 
 public static string GetDateFormat(DateTime date){
 return string.Format(@"TO_DATE('{0}', 'MM/DD/YYYY HH24:MI:SS')", date.ToString("MM/dd/YYYY HH:mm:ss"));
 }
 
 public static string GetConnectionString()
 {
 return ConnectionStringHandler.ConnectionString;
 }
} 
 
 
public static class ConnectionStringHandler
{
 public static string ConnectionString= String.Empty;
 static ConnectionStringHandler()
 {
 Decrypt();
 ConnectionString = ConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
 Encrypt();
 }
}


useful sql

Backup data

If NEW_TABLE already exists then …

insert into new_table select * from old_table
/

If you want to create NEW_TABLE based on the records in OLD_TABLE …

create table new_table as select * from old_table
/


In ORACLE

SELECT REPLACE(REPLACE(YourColumn,CHAR(13),NULL),CHAR(10),NULL)
FROM YourTable


FOR ONE CHAR :
SELECT replace(YourColumn,CHAR(13),'')
FROM YourTable

OR
update YourTable
set YourColumn =replace(YourColumn,CHAR(13),'')

IN .NET 
MyString.Replace("\r\n","")   

or..

MyString.Replace("\r\n",@"\n")

Potential Blacklist
Special control chars 0-31 by Unicode must be handled
White list 
[^a-zA-Zא-ת0-9._] -and punctuation chars by your choice
- this is a start of a regex pattern - [^] -everything that not in the pattern is not allowed.
also can do 
[^\w\s\p{IsHebrew}\p{P}] - not taking accesnt chars like ~^ , not finished , but a start

better handled by StringBuilder with loop through chars than Regex 
(more than 10 times better).just Whitelist of allowed chars and if in range 
like this 

if(c>=a && c<=z)...
  sb.append(c)
else
  sb.append(' '); //replacement char
 

 

For  usage of commands in cmd with ElasticSearch you should install curl.

Better put it in c:\ for convenience .

You should download ES   from here and install it.

You can check it in chrome – http://localhost:9200/

or

run it in cmd  c:\curl -X GET http://localhost:9200/

Run few commands to automatize the process and make it easier to work with :

1) running as a service on windows

c:\elasticsearch-{version}bin>service install
 
 
 
2)install plugins 
You better get the basics 
bin/plugin --install mobz/elasticsearch-head
bin/plugin --install lukas-vlcek/bigdesk
 
image

 

installing marvel will give you the tools that ES itself promotes

bin/plugin -i elasticsearch/marvel/latest

 

3)In VS2013 quick launch – type nuget – choose Package Manager

paste-

Install-Package NEST –Version  1.2.1

it will get you the .net api for elasticsearh. the version is optional . without it gets the latest  version

public static void Encrypt(){

           Configuration objConfiguration = null;

           if(HttpContext.Current!=null)        //if we are in web application of some sort

               objConfiguration = WebConfigurationManager.OpenWebConfiguration (“~”);

           else       ///if not web

                objConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

           ConfigurationSection objSection = “connectionStrings”;

           //if encrypted already don’t do it again

           if(objSection!=null && !objSection.SectionInformation.IsProtected){

                           objSection.SectionInformation.ProtectionSection(string.Empty);

                           objSection.SectionInformation.ForseSave = true;

                          objConfiguration.Save(ConfigurationSaveMode.Full);

           }     

}

 

public static void Encrypt(){

                     Configuration objConfiguration = null;

                       if(HttpContext.Current!=null) //if we are in web application of some sort

                                   objConfiguration = WebConfigurationManager.OpenWebConfiguration (“~”);

                       else ///if not web

                                    objConfiguration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

                      ConfigurationSection objSection = “connectionStrings”;

                           //if encrypted then decrypt

                            if(objSection!=null && !objSection.SectionInformation.IsProtected){

         objSection.SectionInformation.UnprotectSection();

  //objConfiguration.Save();  – you can save the change but it will reset all the global data of your application in iis (relevant to comment it for web applications,can stay on in others)

}

}

Tag Cloud

Follow

Get every new post delivered to your Inbox.