These days i read most books on my ipad. The problem is that iTunes does not seem to add pdf files when i choose ‘Add Folder’ to the library. So here is a small application that adds them one by one (way too lazy/unmotivated to do this by hand).
Tag Archives: C
Add missing books to iTunes
Remove all access rules from a directory
A while ago i needed to write some code that removes all (existing/inherited) access rules from a given directory. It was pretty frustrating to notice that all my attempts seemed to fail (RemoveAccessRule, PurgeAccessRule, …)
Finally i found that SetAccessRuleProtection was the method that i needed to invoke.
const string Folder = @"c:\temp\secured"; var directory = new DirectoryInfo(Folder); var directorySecurity = directory.GetAccessControl(); directorySecurity.SetAccessRuleProtection(true,false); directory.SetAccessControl(directorySecurity);
There you go
SqlConnectionStringBuilder sets the Pooling property to true by default
Here is something that surprised me: SqlConnectionStringBuilder sets the Pooling property to true by default.
TryGetResult
I think this entry has been in the pipeline for a couple of years now and today i have decided to finally post it
I got frustrated with the annoying out parameter in TryGet methods so i decided to use a different signature using TryGetResult:
public class TryGetResult<T>
{
public TryGetResult()
{
Success = false;
}
public TryGetResult(T result)
{
Success = true;
Result = result;
}
public bool Success { get; private set; }
public T Result { get; private set; }
}
And now your TryGet methods can have the following signature:
public TryGetResult<Person> TryGetPersonByName(string name)
{
// person is not available
if(name.IsInvalidPersonName()) return new TryGetResult();
// return the person
return new TryGetResult(new Person(name));
}
Using User-Defined Table Type with Identity column in ADO.NET
A while ago i wanted to use a User-Defined Table Type to pass in a set of records. Nothing special about this except that the first column of the UDTT was an Identity column:
CREATE TYPE [Star].[example] AS TABLE( [Ordinal] [int] IDENTITY(1,1) NOT NULL, [Name] [nvarchar](200) NOT NULL, )
After finding a lot of posts saying that this is not supported a colleague of mine, Stephen Horsfield, found a way to do it as following:
var sqlMetaData = new[]
{
new SqlMetaData("Ordinal", SqlDbType.Int, true, false, SortOrder.Unspecified, -1),
new SqlMetaData("Name", SqlDbType.NVarChar, 200)
};
sqlRecords = new HashSet<SqlDataRecord>(usersToInclude.Select(user =>
{
var record = new SqlDataRecord(sqlMetaData);
record.SetString(1, user.Name);
return record;
}));
Get/SetVariable value from SSIS VariableDispenser
Here is some code that allows you to Get/Set a variable (object) value from/on the VariableDispenser in an SSIS package:
public static T GetVariable<T>(this VariableDispenser variableDispenser, string scopedVariableName)
{
Variables variables = null;
try
{
variableDispenser.LockForRead(scopedVariableName);
variableDispenser.GetVariables(ref variables);
return (T)variables[0].Value;
}
finally
{
if (variables != null) variables.Unlock();
}
}
public static void SetVariable<T>(this VariableDispenser variableDispenser, string scopedVariableName, T value)
{
Variables variables = null;
try
{
variableDispenser.LockForWrite(scopedVariableName);
variableDispenser.GetVariables(ref variables);
variables[0].Value = value;
}
finally
{
if (variables != null) variables.Unlock();
}
}
Exploring messaging at lower levels…
Yesterday a colleague of mine, Neil Robbins, asked me how a piece of code would look like if I apply the Hollywood principle on it (Don’t call us, we’ll cal you).
Let me start with setting the scene: The purpose of the code is to provide items via provider and to consume those items via a consumer.
Here is how my oldskool function signatures would look like:
class ItemProviderFactory {
public ItemProvider Create() { .. }
}
class ItemProvider {
public IEnumerable<Item> Provide() { .. }
}
class ItemConsumerFactory {
public ItemConsumer Create() { .. }
}
class ItemConsumer {
public void Consume(IEnumerable<Item> items) { .. }
}
My Hollywood style function signatures look like the following:
class ItemProviderFactory {
public void WithItemProvider(Action<ItemProvider> action) { .. }
}
class ItemProvider {
public void Provide(Action<IEnumerable<Item>> action) { .. }
}
class ItemConsumerFactory {
public void WithItemConsumer(Action<ItemConsumer> action) { .. }
}
class ItemConsumer {
public void Consume(IEnumerable<Item> items) { .. }
}
And now I am able compare the code that glues everything together:
void OldStyle(OldStyle.ItemProviderFactory itemProviderFactory, OldStyle.ItemConsumerFactory itemConsumerFactory) {
var provider = itemProviderFactory.Create();
var items = provider.Provide();
var consumer = itemConsumerFactory.Create();
consumer.Consume(items);
}
void HollywoodStyle(HollywoodStyle.ItemProviderFactory itemProviderFactory, HollywoodStyle.ItemConsumerFactory itemConsumerFactory) {
itemProviderFactory.With(provider =>
provider.Provide(items =>
itemConsumerFactory.With(consumer
=> consumer.Consume(items))));
}
Let me refactor this Hollywood code a bit:
// An itemconsumer consumes items as following: Action<HollywoodStyle.ItemConsumer, IEnumerable<Item>> consumerAction = (consumer, items) => consumer.Consume(items); // As soon as I have items, I want a consumer to consume them: Action<IEnumerable<Item>> itemsAction = (items) => itemConsumerFactory.With(consumer => consumerAction(consumer, items)); // I can get items as following: Action<HollywoodStyle.ItemProvider> providerAction = (provider) => provider.Provide(itemsAction); // I can get an ItemProvider as following: itemProviderFactory.With(providerAction);
I think that most colleagues are quite thankful that i’m not a Hollywood star
A TetriNet Spectator bot that generates game statistics
Get the TetriNet Spectator bot that generates game statistics: statsbot.zip.
