patterncsharpMinor
Attribute driven behaviour in C# methods
Viewed 0 times
methodsbehaviourdrivenattribute
Problem
We want to create a
One requirement we have is that a method can either:
The latter requirement is so that settings can be changed at runtime for specific methods if necessary (e.g. extending a timeout) without having to recompile the system and without affecting all
Option 1 - pass config keys via method param on the create methods
My only issue with this is that I don't really like the fact that there are different strings peppered throughout the different
TransactionScope factory class that we can use as a central point for instantiating TransactionScopes with varying configurations throughout our app.One requirement we have is that a method can either:
- Instantiate a plain
TransactionScopewhose settings are driven by the defaults in the App.Config
- Instantiate a
TransactionScopepassing some config key, which will pull specific settings from some other source
The latter requirement is so that settings can be changed at runtime for specific methods if necessary (e.g. extending a timeout) without having to recompile the system and without affecting all
TransactionScopes.Option 1 - pass config keys via method param on the create methods
public static class TransactionScopeFactory
{
public static TransactionScope Create()
{
return new TransactionScope();
}
public static TransactionScope Create(string configKey)
{
var source = GetConfigSettings(configKey);
if(source != null)
{
var options = new TransactionOptions
{
//IsolationLevel = From Config Source
//Timeout = From Config Source
};
return new TransactionScope(TransactionScopeOption.Required, options);
}
return Create();
}
}
public class Frob
{
public void DoStuff()
{
using (var scope = TransactionScopeFactory.Create()) //Default
{ /*Do Stuff*/ }
}
public void DoFoo()
{
using (var scope = TransactionScopeFactory.Create("DoFoo"))
{ /*Do Foo*/ }
}
public void DoBar()
{
using (var scope = TransactionScopeFactory.Create("DoBar"))
{ /*Do Bar*/ }
}
}My only issue with this is that I don't really like the fact that there are different strings peppered throughout the different
Create() methods throughout the app. Even Solution
Eoin, nice job with the second attempt. However, I would stick to your guns on the first style with a few deviations. Either A) pass in enumerations into the create method or B) create an override method with the specific name.
I would highly recommend direction B. This prevents magic strings all over the place and reflection while promoting readability.
Example:
I would highly recommend direction B. This prevents magic strings all over the place and reflection while promoting readability.
Example:
private static TransactionScopeFactory Create(string key){/* primary create */}
public static TransactionScopeFactory Start(){return Create("default_key");}
public static TransactionScopeFactory StartFoo(){return Create("foo");}
public static TransactionScopeFactory StartBar(){return Create("bar");}Code Snippets
private static TransactionScopeFactory Create(string key){/* primary create */}
public static TransactionScopeFactory Start(){return Create("default_key");}
public static TransactionScopeFactory StartFoo(){return Create("foo");}
public static TransactionScopeFactory StartBar(){return Create("bar");}Context
StackExchange Code Review Q#10464, answer score: 2
Revisions (0)
No revisions yet.