Sunday, April 29, 2018

How will you create your own custom html helper method.

We can create custom HTML helpers in two ways,
Using static methods
Using extension methods

using static method


public static class CustomHelper
{
        public static MvcHtmlString Image(string source,string altTxt,string width,string height){
            //TagBuilder creates a new tag with the tag name specified
            var ImageTag = new TagBuilder("img");
            //MergeAttribute Adds attribute to the tag
            ImageTag.MergeAttribute("src", source);
            ImageTag.MergeAttribute("alt", altTxt);
            ImageTag.MergeAttribute("width", width);
            ImageTag.MergeAttribute("height", height);
            //Return an HTML encoded string with SelfClosing TagRenderMode
            return MvcHtmlString.Create(ImageTag.ToString(TagRenderMode.SelfClosing));
        }
}

Using Extension Methods


public static MvcHtmlString Image(this HtmlHelper htmlhelper, string source, string altTxt, string width, string height)
{
            var ImageTag = new TagBuilder("img");
            ImageTag.MergeAttribute("src", source);
            ImageTag.MergeAttribute("alt", altTxt);
            ImageTag.MergeAttribute("width", width);
            ImageTag.MergeAttribute("height", height);
}

Difference between TextBox and TextBoxFor

  • @Html.TextBox() is loosely typed method whereas @Html.TextBoxFor() is a strongly typed (generic) extension method.
  • TextBox() requires property name as string parameter where as TextBoxFor() requires lambda expression as a parameter.
  • TextBox doesn't give you compile time error if you have specified wrong property name. It will throw run time exception.
  • TextBoxFor is generic method so it will give you compile time error if you have specified wrong property name or property name changes. (Provided view is not compile at run time. )

Saturday, April 28, 2018

Difference Between MVC , MVP and MVVM

MVC – Model View Controller

Let’s look at MVC first. You’ll notice a few things about the diagram:

The input is directed at the Controller first, not the view. That input might be coming from a user interacting with a page, but it could also be from simply entering a specific url into a browser. In either case, its a Controller that is interfaced with to kick off some functionality.

There is a many-to-one relationship between the Controller and the View. That’s because a single controller may select different views to be rendered based on the operation being executed.

Note the one way arrow from Controller to View. This is because the View doesn’t have any knowledge of or reference to the controller.

The Controller does pass back the Model, so there is knowledge between the View and the expected Model being passed into it, but not the Controller serving it up.

MVP – Model View Presenter

Now let’s look at the MVP pattern. It looks very similar to MVC, except for some key distinctions:

The input begins with the View, not the Presenter.

There is a one-to-one mapping between the View and the associated Presenter.

The View holds a reference to the Presenter. The Presenter is also reacting to events being triggered from the View, so its aware of the View its associated with.

The Presenter updates the View based on the requested actions it performs on the Model, but the View is not Model aware.

MVVM – Model View View Model

So with the MVC and MVP patterns in front of us, let’s look at the MVVM pattern and see what differences it holds:

The input begins with the View, not the View Model.

While the View holds a reference to the View Model, the View Model has no information about the View. This is why its possible to have a one-to-many mapping between various Views and one View Model…even across technologies. For example, a WPF View and a Silverlight View *could* share the same View Model. However, my own feeling is that this is a bad practice and creates Franken-ViewModels that have too many responsibilities. It’s better to keep it as a one-to-one mapping instead.

You’ll also notice that the View has no idea about the Model in the MVVM pattern. This is because, as far as the View knows, its “Model” IS the View Model (hence its name). Because of how data-binding and other features like commanding work in WPF and Silverlight, there is rich communication between the View and View Model, isolating the View from having to know anything about what’s really happening behind the scenes.

Source http://geekswithblogs.net/dlussier/archive/2009/11/21/136454.aspx


Friday, April 27, 2018

Data Loss Prevention

Data Loss Prevention is a strategy for making sure that end users do not send sensitive or critical
information outside the corporate network.

For example if an employee tried to forward business email to outside the corporate domain or upload
a file to consumer cloud service like DropBox, the employee would be denied permission.

How To implement?



  • It can also discover and identify Intellectual Property (IP), and even be trained to learn the difference between your IP and the IP of your business partners. It can alert you when someone tries to copy or share PI or IP. It can block or encrypt attempts to email, IM, blog, copy, or print this sensitive data. 
  • While choosing a DLP product, organizations should check whether the DLP product supports the data formats in which data is stored in their environment.
  • After choosing a DLP product, DLP implementation should start with a minimal base to handle false positives and the base should be increasing with more identification of critical or sensitive data.
  • Identify potential places where PCI information might leak. For most organizations it is recommended to inspect the following channels:
  • Email – Consider all out bound email traffic including attachments.
  • Web traffic – Gmail, and other web mail providers, Facebook and other social media sites should be monitored
  • Other protocols – In particular unencrypted communications should not be crossing the organizational firewall without first identifying the information
  • Data storage – Identify and categorize the information on all storage under control of the organization, including file servers, file shares, SAN, SharePoint servers, user home directories, workstations and laptops in order to determine the assets requiring review and inspection.
  • USB, DVD – Consider workstations that allow USB m ass storage or DVD burning and any devices that can be physically disconnected and carried away.
  • Scan data stores for PCI information. Once assets have been determined, identify any potential regulated or sensitive information on that information asset.
  • Apply controls. Repeat these steps until a satisfactory level of understanding is developed in the form of a map to the protected information and appropriate controls are in place and understood by the stakeholders and system users.

Thursday, April 26, 2018

Dependencies Vs DevDependencies angular 2+

The difference between these two, is that devDependencies are modules which are only required during development, while dependencies are modules which are also required at runtime.

To save a dependency as a devDependency on installation we need to do an npm install --save-dev, instead of just an npm install --save. A nice shorthand for installing a devDependency that I like to use is npm i -D. The shorthand for saving a regular dependency is -S instead of -D.

Some good examples of dependencies which would be required at runtime include React, Redux, Express, and Axios.

Some good examples of when to install devDependencies would be Nodemon, Babel, ESLint, and testing frameworks like Chai, Mocha, Enzyme, etc…

Wednesday, April 25, 2018

CTE Vs Temp Table


#CTE
  • Are unindexable (but can use existing indexes on referenced objects)
  • Cannot have constraints
  • Are essentially disposable VIEWs
  • Persist only until the next query is run
  • Can be recursive
  • Do not have dedicated stats (rely on stats on the underlying objects)

#Temp Tables...

  • Are real materialized tables that exist in tempdb
  • Can be indexed
  • Can have constraints
  • Persist for the life of the current CONNECTION
  • Can be referenced by other queries or subprocedures
  • Have dedicated stats generated by the engine.

As far as when to use each, they have very different use cases. If you will have a very large result set, or need to refer to it more than once, put it in a #temp table. If it needs to be recursive, is disposable, or is just to simplify something logically, a CTE is preferred.

Also, a CTE should never be used for performance. You will almost never speed things up by using a CTE, because, again, it's just a disposable view. You can do some neat things with them but speeding up a query isn't really one of them.

CTE SQL Server

CTE is a temporary result set. It is not stored in memory. It can be used within a SELECT, INSERT, UPDATE, or DELETE statement or in View.It also
can be used in Merge statement. It exist only in same scope.

Advantage

  • CTE improves the code readability.
  • CTE provides recursive programming.
  • CTE makes code maintainability easier.
  • If you need to reference/join the same data set multiple times you can do so by defining a CTE. Therefore, it can be a form of code re-use. An example of self referencing is recursion: Recursive Queries Using CTE
  • Substitute for a view when you do not have to store the definition in metadata.
  • Enable grouping by a column that is derived from a scalar subselect, or a function that is either not deterministic or has external access.
  • It can be used instead of temp table or table variables in the stored procedures in the circumstances.
  • CTE’s can also recursively refer to the same table using a union or union all, and this works great for searching an adjacency pairs pattern hierarch
  • The query can be divided into separate, simple, logical building blocks which can be then used to build more complex CTEs until final result set is generated.
  • CTE can be defined in functions, stored procedures, triggers or even views.
  • After a CTE is defined, it can be used as a Table or a View and can SELECT, INSERT, UPDATE or DELETE Data

Syntax

Select Name,Age, employeeID    
From employee    
Where employeeID in    
(   
   Select employeeID from salary where salary >=1000 /******Sub Query******/   
)

Here the Select statement must be very next to the CTE. The name is mandatory and the argument is an optional. This can be used to give the alias to the retrieve field of the CTE.


The WITH keyword not only begins a CTE, it also adds a hint to a table reference. This is why the statement before a CTE must be terminated with a semicolon. 

We can use another CTE within a CTE but the query using the CTE must be the first query appearing after the CTE.


With salaryCTE(EmployeeID)  

  
AS  
  
(Select employeeID from salary where salary >=1000)  
  
, EmpDetailsCTE( Name, EmployeeID ,salary)  
  
AS  
  
(  
  
Select Name,Age, employeeID  
  
From employee Emp Join salaryCTE sa  
  
on Emp. employeeID = sa. EmployeeID)  

Types of CTE’s

Common Table Expressions can be placed into two broad categories:  Recursive CTE’s and Non-Recursive CTE’s


Disadvantage of CTE


  • Firstly, CTEs cannot be nested like Subqueries.
  • Secondly, CTEs cannot reference the main query; they are self-contained like the simple Subqueries. They may reference to any of the CTEs defined before it or even to itself.
  • Are unindexable (but can use existing indexes on referenced objects)
  • Cannot have constraints
  • Persist only until the next query is run
When to Use CTE


There are two reasons I see to use cte's.
To use a calculated value in the where clause. This seems a little cleaner to me than a derived table.
Suppose there are two tables - Questions and Answers joined together by Questions.ID = Answers.Question_Id (and quiz id)
WITH CTE AS
(
    Select Question_Text,
           (SELECT Count(*) FROM Answers A WHERE A.Question_ID = Q.ID) AS Number_Of_Answers
    FROM Questions Q
)
SELECT * FROM CTE
WHERE Number_Of_Answers > 0
Here's another example where I want to get a list of questions and answers. I want the Answers to be grouped with the questions in the results.
WITH cte AS
(
    SELECT [Quiz_ID] 
      ,[ID] AS Question_Id
      ,null AS Answer_Id
          ,[Question_Text]
          ,null AS Answer
          ,1 AS Is_Question
    FROM [Questions]

    UNION ALL

    SELECT Q.[Quiz_ID]
      ,[Question_ID]
      ,A.[ID] AS  Answer_Id
      ,Q.Question_Text
          ,[Answer]
          ,0 AS Is_Question
        FROM [Answers] A INNER JOIN [Questions] Q ON Q.Quiz_ID = A.Quiz_ID AND Q.Id = A.Question_Id
)
SELECT 
    Quiz_Id,
    Question_Id,
    Is_Question,
    (CASE WHEN Answer IS NULL THEN Question_Text ELSE Answer END) as Name
FROM cte    
GROUP BY Quiz_Id, Question_Id, Answer_id, Question_Text, Answer, Is_Question 
order by Quiz_Id, Question_Id, Is_Question Desc, Name

Tuesday, April 24, 2018

THREE-VALUED LOGIC

-- this is true
SELECT 1 WHERE 1 = 1
Output : 1

-- this is false
SELECT 1 WHERE 1 = 0
Ouput :No Output

-- this is unknown - it is usually expected to be false, but that only shows 
-- misunderstanding of nulls. It's not false it's only treated as false in the filter
SELECT 1 WHERE 1 = NULL
Ouput :No Output

-- this is also unknown - but logicaly it would seem it would be true
-- but unknown compared to unknown equals uknown and it is treated as false in the filter
SELECT 1 WHERE NULL = NULL
Ouput :No Output

Join Vs Exist Vs In

The Exists keyword evaluates true or false, but the IN keyword will compare all values in the corresponding subquery column.  If you are using the IN operator, the SQL engine will scan all records fetched from the inner query. On the other hand, if we are using EXISTS, the SQL engine will stop the scanning process as soon as it found a match.

The EXISTS subquery is used when we want to display all rows where we have a matching column in both tables.  In most cases, this type of subquery can be re-written with a standard join to improve performance.

select
   book_key
from
   book
where 
   exists (select book_key from sales);

The EXISTS clause is much faster than IN when the subquery results is very large. Conversely, the IN clause is faster than EXISTS when the subquery results is very small.

Also, the IN clause can't compare anything with NULL values, but the EXISTS clause can compare everything with NULLs.

Source : http://www.dba-oracle.com/t_exists_clause_vs_in_clause.htm

Monday, April 23, 2018

Synechron

1. Join Vs In Vs Exist , make the priority performance wise
Ans Join Exist In
2. CTE advantage
And In memory representation of table, do not save in database
3. Temp table Vs Table Vairbale difference
Ans.  
4. Life Cycle Hook of Angular
5. What is sliding time expiration? How to kill cookie?
6. Func Vs Action
7. What is Java Script Closure?
8. Var Vs let 
9. Function Hosting
10. What is the role of Enumerator in IEnumerable?
Ans : 

IEnumerable interface represents an object that can be enumerated, like the List class here. It has one method:

public interface IEnumerable
{
    IEnumerator GetEnumerator();
}
The GetEnumerator method here returns an IEnumerator object, which can be used to iterate (or enumerate) the given object. Here is the declaration of the IEnumerator interface:


public interface IEnumerator
{
    bool MoveNext();
    object Current { get; }
    void Reset();

}

11. AOT compiler?
12. MVVM and MVC difference
13. Mock framework and AutoFac?
14. Task Runner?
Ans:

CSS
Compile Sass into CSS
Run Autoprefixer on the new CSS to catch any vendor prefixes we may have missed
Minify the prefixed CSS
Update our CSS banner with new timestamp information

Javascript
Check our Javascript for errors
Concatenate other included scripts into one file
Recheck for errors
Minify Scripts

Images
Optimize any jpg, gif, or png to make the file size smaller

Other Utilities
Watch files for changes and rerun tasks as needed

Run BrowserSync for testing in multiple browsers and devices at once

15. How to unit test http response in angular?
Ans : Using HttpClientTestingModule and  HttpTestingController

16. Dispose method?
17. Query Optimization technique?
18. Model Binder?
19. Http Helper in MVC?
Ans:

 Using the HTML Helper class, we can create HTML Controls progammaticrally. HTML Helpers are used in View to render HTML content. HTML Helpers (mostly) is a method that returns a string. It is not mandatory to use HTML Helper classes for building an ASP.NET MVC application. We can build an ASP.NET MVC application without using them, but HTML Helpers helps in the rapid development of a view. HTML Helpers are more lightweight as compared to ASP.NET Web Form controls as they do not use ViewState and do not have event models.

HTML Helpers are categorized into three types:

Inline HTML Helpers
Built-in HTML Helpers

Custom HTML Helpers


20. Http Handler?
21. Action Filter ? and How to create custom action filter?
22. Commnication between controllar in MVC?
23. Delete Vs Truncate
24. Union Vs Union All
25. How to share service in Multiple Component?
26. Extension method? Advantage of Extension method?
27. What is directive and how to create custom directive?
28. Routing in angular?
29. Give an example of Open Close Principle?
30 Give an example of Interface Sgregation principle?
31. What is TDD , How to write unit test if we have CRUD operation of a table?
32. How to get value from XML if we have employee information in xml in C#.
33. Write a regular expression for mobile number validation.
34. Store Procedure Vs Function.
35. What is .md file in angular?

36. Can we overload private constructor?
Ans: Yes

37. Can we overload static constructor?
Ans : No

38. If have not call dispose method explicitly then what will happen.
Ans: Will not call if not created object in using block.

39. What is async pipe in angular?
40. What is CTE?
Ans: 

Common Table Expression is used to store temporary results of single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement defined within the execution scope. Even though it lasts until the execution of query, it can be self referenced multiple times in the same query. CTE allows query to be single SELECT, INSERT, UPDATE, DELETE, or CREATE VIEW statement to improve readability. CTE be used to replace a view which stores the metadata.

41. What is the benefit of CTE?
Ans:

Can be used to create a recursive query.
Can be substituted for a view
Allow grouping by a column which might be derived from a scalar subset
Can reference itself multiple times

42. Write the syntax of CTE?

Followers

Link