add

Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Thursday, February 5, 2026

 S.O.L.I.D. Principles 





The SOLID principles are a set of five foundational object-oriented design guidelines that help developers build software systems that are maintainable, scalable, and resilient to change

Introduced and popularized by Robert C. Martin, SOLID emphasizes designing classes and modules with clear responsibilities, low coupling, and high cohesion

Rather than focusing on syntax or specific technologies, these principles guide how software components should interact and evolve over time. By applying SOLID, developers reduce the risk of fragile code, make systems easier to understand and test, and ensure that future enhancements can be introduced with minimal impact on existing functionality. 

In essence, SOLID is not about writing more code—it is about writing better-structured code that stands the test of time.

Day 1: Introduction to SOLID Principles: Day 1 PDF


Visit Instagram: Fundamentally Clean 


Tuesday, November 1, 2011

Convert DATETIME TO TIMESPAN IN C#

In the previous post, I mentioned how to convert DATETIME to TIMESPAN. Now if the requirement is to convert DATETIME TO TIMESPAN, we can do that in one line: DateTime dt="XXX"; TimeSpan TS = new TimeSpan(dt.Now.Ticks); This will convert the DateTime to TimeSPan. Varun

Convert TimeSpan To DateTime in c# + One line Code

We usually fall into the situation where conversion of Timespan to DateTime is required.

Here is how it can be done easily just by casting:

Suppose we have two variables DateTime and Timespan and Timespan one with some value
DateTime dt;
TimeSpan ts="XXX";

We can covnert 'ts' to 'dt' like this:

dt= Convert.ToDateTime(ts.ToString());

Thats it!!!

Hope you have liked the solution.

Varun

Thursday, June 11, 2009

How to get Data from Active Directory using C#

Today, I am going to discuss how to fetch data from active directory and how to store it into a clas..

We can access LDAP or AD by using two class: DirectoryEntry and DirectorySearcher

Step 1:

Add reference to System.DirectoryServices amd import it in the code by entering using System.DirectoryServices;

Step 2:

We need the AD Location, AD Userid and AD Password in order to read data from it.

We store these values in web.config:
<appSettings>
<add key="GaadLocation" value="LDAP://XXXXX:XXX"/>
<add key="GaadUserName" value="XXXXXX"/>
<add key="GaadPassword" value="XXXXXX"/>
</appSettings>


Step 3:

In order to store the data fetched from AD, we created a class and defined some properties.

public class ADContact
{
public string UniqueId {get; set; }
public string LastName { get; set; }
public string DepartmentNumber { get; set; }
public string EmployeeType { get; set; }
public string TelephoneNumber { get; set; }
public string FirstName { get; set; }
public string UserStatus { get; set; }
public string Mail { get; set; }
public string Mobile { get; set; }
public string Title {get; set; }
}


Step 4: Create a method that will return a datatable so that we can iterate through the result.

public List GetADContacts(string SirName, string GivenName, string UniqueId)
{}

Step 5:

Within the method, create a SearchResultCollection object , a datatable, and list of AD properties to access:

SearchResultCollection results = null;
List GaadData = new List();

string ADLocation, ADUserId, ADUserPassword = string.Empty;
#region List of AD Properties
string[] properties = new string[]
{
"modifyTimestamp",
"uid",
"sn",
"departmentNumber",
"facsimileTelephoneNumber",
"givenName",
"mail"
};
#endregion


Step 6:
Declare a try-catch block and within try- write the following code:

try {

string strfilter = string.Empty;


//Get AD Settings from web.config
ADLocation = ConfigurationManager.AppSettings["ADLocation"];
ADUserId = ConfigurationManager.AppSettings["ADUserName"];
ADUserPassword = ConfigurationManager.AppSettings["ADPassword"];


//Create an instance of DirectoryEntry by passing the setting valies)
DirectoryEntry root = new DirectoryEntry(ADLocation, ADUserId, ADUserPassword, AuthenticationTypes.FastBind);

//Create the filter string : we need to search the Ad for people matching the search criteria. Here we are passing three parameters: SirName, GivenName, Uid. If any field is blank, we are putting * there. that is to get all values

strfilter = String.Format(
"(&(&(sn={0})(givenname={1})(uid={2})))",
String.IsNullOrEmpty(SirName) ? "*" : SirName + "*",
String.IsNullOrEmpty(GivenName) ? "*" : GivenName + "*",
String.IsNullOrEmpty(UniqueId) ? "*" : UniqueId + "*");

if (strfilter != "")
{
DirectorySearcher searcher = new DirectorySearcher(root, strfilter, properties);
searcher.Asynchronous = true;
searcher.SearchScope = SearchScope.Subtree;
results = searcher.FindAll();
}
foreach (SearchResult result in results)
{
ADContact contact = new ADContact();

if (result.Properties["uid"].Count > 0)
contact.UniqueId= Convert.ToString(result.Properties["uid"][0]);

if (result.Properties["title"].Count > 0)
contact.Title = Convert.ToString(result.Properties["title"][0]);

if (result.Properties["sn"].Count > 0)
contact.LastName = Convert.ToString(result.Properties["sn"][0]);

if (result.Properties["givenName"].Count > 0)
contact.FirstName = Convert.ToString(result.Properties["givenName"][0]);

if (result.Properties["facsimileTelephoneNumber"].Count > 0)
contact.TelephoneNumber = Convert.ToString(result.Properties["facsimileTelephoneNumber"][0]);

if (result.Properties["mobile"].Count > 0)
contact.Mobile = Convert.ToString(result.Properties["mobile"][0]);

if (result.Properties["mail"].Count > 0)
contact.Mail = Convert.ToString(result.Properties["mail"][0]);

ADData.Add(contact);
}
return ADData;
}
catch (Exception ex)
{
throw ex;
}