public static string GetConnectionString(ProfileMaster profile)
{
//return string.Format("data source={0};initial catalog={1};user id={2};password={3} multipleactiveresultsets=True;App=EntityFramework", profile.DatabaseIP, profile.DatabaseName, profile.UserName, profile.Password);
string providerName = "System.Data.SqlClient";
string serverName = profile.DatabaseIP;
string databaseName = profile.DatabaseName;
// Initialize the connection string builder for the
// underlying provider.
SqlConnectionStringBuilder sqlBuilder =
new SqlConnectionStringBuilder();
// Set the properties for the data source.
sqlBuilder.DataSource = serverName;
sqlBuilder.InitialCatalog = databaseName;
sqlBuilder.IntegratedSecurity = true;
// Build the SqlConnection connection string.
string providerString = sqlBuilder.ToString();
// Initialize the EntityConnectionStringBuilder.
EntityConnectionStringBuilder entityBuilder =
new EntityConnectionStringBuilder();
//Set the provider name.
entityBuilder.Provider = providerName;
// Set the provider-specific connection string.
entityBuilder.ProviderConnectionString = providerString;
// Set the Metadata location.
entityBuilder.Metadata = @"res://*/Models.MoviesDB.csdl|
res://*/Models.MoviesDB.ssdl|
res://*/Models.MoviesDB.msl";
return entityBuilder.ToString();
}
Showing posts with label Design Patterns. Show all posts
Showing posts with label Design Patterns. Show all posts
Saturday, February 22, 2014
set entity framework connection string programmatically
Saturday, September 15, 2012
Singleton Session
public class SingletonSession
{
// private constructor
private SingletonSession()
{
}
// Gets the current session.
public static SingletonSession Current
{
get
{
SingletonSession session =
(SingletonSession)HttpContext.Current.Session["__SingletonSession__"];
if (session == null)
{
session = new SingletonSession();
HttpContext.Current.Session["__SingletonSession__"] = session;
}
return session;
}
}
// **** add your session properties here, e.g like this:
public Guid UserId { get; set; }
public string UserName { get; set; }
public string AccountNo { get; set; }
public MembershipUser User { get; set; }
}
}
{
// private constructor
private SingletonSession()
{
}
// Gets the current session.
public static SingletonSession Current
{
get
{
SingletonSession session =
(SingletonSession)HttpContext.Current.Session["__SingletonSession__"];
if (session == null)
{
session = new SingletonSession();
HttpContext.Current.Session["__SingletonSession__"] = session;
}
return session;
}
}
// **** add your session properties here, e.g like this:
public Guid UserId { get; set; }
public string UserName { get; set; }
public string AccountNo { get; set; }
public MembershipUser User { get; set; }
}
}
Labels:
C#.NET,
Design Patterns,
Singleton
Sunday, October 16, 2011
Design Patterns which are used in .NET Framework base class library
As we know that .net is an OOPs based language so we can easily implement design patterns in our projects but some times it comes to mind that, which all design patters microsoft .NET Base Class Library is using internally.
NOTE: This question also popular in interviews.
Lets walk through few Design patterns used in .NET BCL:
Observer Pattern:
This observer design pattern is used for delegates and events.
Iterator Pattern:
Iterator design pattern used in foreach in C# and For Each in Visual Basic .NET
Decorator Pattern:
System.IO.Stream :Any useful executable program involves either reading input, writing output, or both. Regardless of the source of the data being read or written, it can be treated abstractly as a sequence of bytes. .NET uses the System.IO.Stream class to represent this abstraction.
CryptoStream :System.Security.Cryptography.CryptoStream to encrypt and decrypt Streams on the fly, without the rest of the application needing to know anything more than the fact that it is a Stream.
Adapter Pattern:
By allowing managed classes and COM components to interact despite their interface differences, RCWs are an example of the Adapter pattern. The Adapter pattern lets you adapt one interface to another. COM doesn't understand the System.String class, so the RCW adapts it to something that it can understand. Even though you can't change how a legacy component works, you can still interact with it. Adapters are frequently used like this.
Factory Pattern:
System.Convert: System.Convert class contains a host of static methods that work like factory design pattern.
System.Net.WebRequest:
This class is used to request and receive a response from a resource on the Internet.FTP, HTTP, and file system requests are supported by default. To create a request, call the Create method and pass in a URI. The Create method itself determines the appropriate protocol for the request and returns the appropriate subclass of WebRequest: HttpWebRequest, FtpWebRequest, or FileWebRequest. The caller doesn't need to know the specifics of each protocol, only how to invoke the factory and work with the WebRequest that gets returned. If the URI changes from an HTTP address to an FTP address, the code won't have to change at all.
Strategy Pattern:
Both Array and ArrayList provide the capability to sort the objects contained in the collection via the Sort method. Strategy Design Pattern is used with Array and Arraylist to sort them using different strategy without changing any client code using IComparable interface
Composite Pattern in ASP.NET:
ASP.NET has lots of controls. A control may be a simple single item like a Literal, or it could be composed of a complex collection of child controls, like a DataGrid is. Regardless, calling the Render method on either of these controls should still perform the same intuitive function.
Because the domain of controls is so diverse, there are several intermediate derived classes like WebControl and BaseDataList that serve as base classes for other controls. Though these classes expose additional properties and methods, they still retain the child management functions and core operations inherited from Control. In fact, the use of the Composite pattern helps to hide their complexity, if desired. Regardless of whether a control is a Literal or a DataGrid, the use of Composite means you can just call Render and everything will sort itself out.
Template Method Pattern:
custom controls are example of Template Method Pattern.
Reference: http://msdn.microsoft.com/en-us/magazine/cc188707.aspx
Read More
NOTE: This question also popular in interviews.
Lets walk through few Design patterns used in .NET BCL:
Observer Pattern:
This observer design pattern is used for delegates and events.
Iterator Pattern:
Iterator design pattern used in foreach in C# and For Each in Visual Basic .NET
Decorator Pattern:
System.IO.Stream :Any useful executable program involves either reading input, writing output, or both. Regardless of the source of the data being read or written, it can be treated abstractly as a sequence of bytes. .NET uses the System.IO.Stream class to represent this abstraction.
CryptoStream :System.Security.Cryptography.CryptoStream to encrypt and decrypt Streams on the fly, without the rest of the application needing to know anything more than the fact that it is a Stream.
Adapter Pattern:
By allowing managed classes and COM components to interact despite their interface differences, RCWs are an example of the Adapter pattern. The Adapter pattern lets you adapt one interface to another. COM doesn't understand the System.String class, so the RCW adapts it to something that it can understand. Even though you can't change how a legacy component works, you can still interact with it. Adapters are frequently used like this.
Factory Pattern:
System.Convert: System.Convert class contains a host of static methods that work like factory design pattern.
System.Net.WebRequest:
This class is used to request and receive a response from a resource on the Internet.FTP, HTTP, and file system requests are supported by default. To create a request, call the Create method and pass in a URI. The Create method itself determines the appropriate protocol for the request and returns the appropriate subclass of WebRequest: HttpWebRequest, FtpWebRequest, or FileWebRequest. The caller doesn't need to know the specifics of each protocol, only how to invoke the factory and work with the WebRequest that gets returned. If the URI changes from an HTTP address to an FTP address, the code won't have to change at all.
Strategy Pattern:
Both Array and ArrayList provide the capability to sort the objects contained in the collection via the Sort method. Strategy Design Pattern is used with Array and Arraylist to sort them using different strategy without changing any client code using IComparable interface
Composite Pattern in ASP.NET:
ASP.NET has lots of controls. A control may be a simple single item like a Literal, or it could be composed of a complex collection of child controls, like a DataGrid is. Regardless, calling the Render method on either of these controls should still perform the same intuitive function.
Because the domain of controls is so diverse, there are several intermediate derived classes like WebControl and BaseDataList that serve as base classes for other controls. Though these classes expose additional properties and methods, they still retain the child management functions and core operations inherited from Control. In fact, the use of the Composite pattern helps to hide their complexity, if desired. Regardless of whether a control is a Literal or a DataGrid, the use of Composite means you can just call Render and everything will sort itself out.
Template Method Pattern:
custom controls are example of Template Method Pattern.
Reference: http://msdn.microsoft.com/en-us/magazine/cc188707.aspx
Compiled By: Rajesh Rolen
Thursday, June 16, 2011
What is difference between Private and Sealed Classes
Sealed Class:
"Sealed" keyword provide us facility to prevent a class to be inherited by other classes, so "Sealed" classes cannot be inherited by any other class.You can also use the sealed modifier on a method or property that overrides a virtual method or property in a base class. This enables you to allow classes to derive from your class and prevent them from overriding specific virtual methods or properties.
"Sealed" class can have constructor, and they can only be used by its object (non-static methods).
Private Class:
Private class cannot be used directly under any namespace, means we can only create private class as a nested class, to make it available only to class to whom its nested.the only/mostly use of Private class/structure is to create a user-defined type, which you wants to be accessible to that class only.
NOTE:Nested/inner class can be public also and that can be accessible to inherited class or by object where as private nested/inner class is only accessible to its outer class .
Compiled By: Rajesh Rolen
Friday, September 10, 2010
What is difference between Singleton pattern and static class
Both can be invoked without instantiation, both provide only with one "instance" and neither of them is threadsafe.
Usually both should be implemented to be thread-safe.
The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common IME), so you can pass around the singleton as if it were "just another" implementation.
Singletons preserve the conventional class approach, and don't require that you use the static keyword everywhere. They may be more demanding to implement at first, but will greatly simplify the architecture of your program. Unlike static classes, we can use singletons as parameters or objects.
Solution by:
Read More
Usually both should be implemented to be thread-safe.
The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common IME), so you can pass around the singleton as if it were "just another" implementation.
Advantages of singletons
Singletons preserve the conventional class approach, and don't require that you use the static keyword everywhere. They may be more demanding to implement at first, but will greatly simplify the architecture of your program. Unlike static classes, we can use singletons as parameters or objects.
Solution by:
Rajesh Rolen
Subscribe to:
Posts (Atom)