So I'm working on a legacy project and quite intent to re-factor the heck out of it. One of my goals was to make the code testable and to do that I had to rip out the dependency on the database/DataSet implementation.
To do this I created a data provider class and then implemented an iterator block and used the yield statement.
public override IEnumerable MyDataObjects
{
get
{
using (var connection = new SqlConnection(ConnectionString))
{
connection.Open();
using (var command = new SqlCommand{CommandText = "...", Connection = connection })
{
using (var reader = command.ExecuteReader())
{
while (reader != null && reader.Read())
{
yield return new MyDataObject
{
//extract the column values
};
}
}
}
}
}
}
Now the consumer of the data doesn't know where the data is coming from and that's just fine!
foreach(var myData in provider.MyDataObjects)
{
//do stuff with the object
}
Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
Tuesday, March 23, 2010
Isolation from database
Labels:
.Net,
C#,
IEnumerable,
sql,
SqlConnection,
SqlDataReader,
yield return
Wednesday, November 4, 2009
Get Facebook user's permission
In using the Facebook developer toolkit it was necessary to ask the user for extended permission to publish to their wall server side.
The FbPermissions class is initialized with the required permissions. Calling the GetPermissionFromUser will display the facebook permissions dialog.
If all permissions were granted the dialog will not be invoked next time. This prevents a momentary flash of the permissions dialog if you're using AJAX to make server requests.
<script type="text/javascript">
function FbPermissions(options){
this.requiredPermissions = options["RequiredPermissions"];
}
FbPermissions.prototype.callback = function(e, successCallback, failureCallback){
if(e === false){
if(failureCallback != null)
failureCallback();
}
else if(e == null || e === ""){
if(failureCallback != null)
failureCallback();
}
else{
if(successCallback != null)
successCallback();
//Simple length check should be sufficient
if(this.requiredPermissions.split(',').length == e.split(',').length)
this.requiredPermissions = "";
}
}
FbPermissions.prototype.GetPermissionFromUser = function(successCallback, failureCallback)
{
var me = this;
if(this.requiredPermissions != null && this.requiredPermissions.length != 0)
FB.Connect.showPermissionDialog(this.requiredPermissions, function(e){me.callback(e, successCallback, failureCallback);});
else
successCallback();
}
Using the class is simple. Just initialize the FbPermissions with the required permissions using a server side script block. Then invoke the GetPermissionFromUser function with a success and failure callback method.
var permissions = new FbPermissions({RequiredPermissions : '<%= Model.RequiredFacebookPermissions %>'});
function PostToWall(message) {
permissions.GetPermissionFromUser(
function(){/*Call your facebook application here*/},
function(){alert('You must grant permission');})
}
</script>
<a href="#" onclick="PostToWall('hi'); return false;">click me</a>
The FbPermissions class is initialized with the required permissions. Calling the GetPermissionFromUser will display the facebook permissions dialog.
If all permissions were granted the dialog will not be invoked next time. This prevents a momentary flash of the permissions dialog if you're using AJAX to make server requests.
<script type="text/javascript">
function FbPermissions(options){
this.requiredPermissions = options["RequiredPermissions"];
}
FbPermissions.prototype.callback = function(e, successCallback, failureCallback){
if(e === false){
if(failureCallback != null)
failureCallback();
}
else if(e == null || e === ""){
if(failureCallback != null)
failureCallback();
}
else{
if(successCallback != null)
successCallback();
//Simple length check should be sufficient
if(this.requiredPermissions.split(',').length == e.split(',').length)
this.requiredPermissions = "";
}
}
FbPermissions.prototype.GetPermissionFromUser = function(successCallback, failureCallback)
{
var me = this;
if(this.requiredPermissions != null && this.requiredPermissions.length != 0)
FB.Connect.showPermissionDialog(this.requiredPermissions, function(e){me.callback(e, successCallback, failureCallback);});
else
successCallback();
}
Usage
Using the class is simple. Just initialize the FbPermissions with the required permissions using a server side script block. Then invoke the GetPermissionFromUser function with a success and failure callback method.
var permissions = new FbPermissions({RequiredPermissions : '<%= Model.RequiredFacebookPermissions %>'});
function PostToWall(message) {
permissions.GetPermissionFromUser(
function(){/*Call your facebook application here*/},
function(){alert('You must grant permission');})
}
</script>
<a href="#" onclick="PostToWall('hi'); return false;">click me</a>
Labels:
.Net,
asp.mvc,
C#,
developer toolkit,
facebook,
permissions,
showPermissionDialog
Wednesday, September 9, 2009
Using XmlSerializer without rendering document declaration or namespace
Using xmlSerializer to serialize an object will by default add the xml namespace and document declaration. <strong>Example</strong>:
<?xml version="1.0" encoding="Windows-1252"?>
<node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" uID="12345">
...
</node>
But if all you want is to write out the xml fragment like:
<node uid="12345">
...
</node>
You need to configure the serializer before you use it. The following code will not render the document declaration or namespace.
public void SerializeWithNoDeclarationNoNamespace<T>(TextWriter writer, T theObject)
{
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineHandling = NewLineHandling.Replace,
NewLineChars = Environment.NewLine,
OmitXmlDeclaration = true
};
using (var xmlWriter = XmlWriter.Create(writer, settings))
{
var xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add("", "");
new XmlSerializer(typeof(T)).Serialize(xmlWriter, theObject, xmlnsEmpty);
}
writer.WriteLine("");
}
<?xml version="1.0" encoding="Windows-1252"?>
<node xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" uID="12345">
...
</node>
But if all you want is to write out the xml fragment like:
<node uid="12345">
...
</node>
You need to configure the serializer before you use it. The following code will not render the document declaration or namespace.
public void SerializeWithNoDeclarationNoNamespace<T>(TextWriter writer, T theObject)
{
var settings = new XmlWriterSettings
{
Indent = true,
IndentChars = " ",
NewLineHandling = NewLineHandling.Replace,
NewLineChars = Environment.NewLine,
OmitXmlDeclaration = true
};
using (var xmlWriter = XmlWriter.Create(writer, settings))
{
var xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add("", "");
new XmlSerializer(typeof(T)).Serialize(xmlWriter, theObject, xmlnsEmpty);
}
writer.WriteLine("");
}
Subscribe to:
Posts (Atom)