Skip to content

Instantly share code, notes, and snippets.

@aruss
Last active December 19, 2015 05:49
Show Gist options
  • Save aruss/5907357 to your computer and use it in GitHub Desktop.
Save aruss/5907357 to your computer and use it in GitHub Desktop.
Kooboo UUIDGenerator with IoC
public interface IUUIDGenerator
{
string Generate(ContentBase content);
}
public class GuidUIIDGenerator : IUUIDGenerator
{
[DllImport("rpcrt4.dll", SetLastError = true)]
private static extern int UuidCreateSequential(out Guid guid);
const int RPC_S_OK = 0;
public static Guid NewSequentialGuid()
{
Guid guid;
int result = UuidCreateSequential(out guid);
if (result == RPC_S_OK)
return guid;
else
return Guid.NewGuid();
}
public string Generate(ContentBase content)
{
return GuidUIIDGenerator.NewSequentialGuid().ToString();
}
}
public class UUIDGenerator
{
static UUIDGenerator()
{
DefaultGenerator = EngineContext.Current.ContainerManager.Resolve<IUUIDGenerator>();
}
public static IUUIDGenerator DefaultGenerator { get; set; }
public virtual string Generate(ContentBase content)
{
return UUIDGenerator.DefaultGenerator.Generate(content);
}
}
@kooboo-jifeng
Copy link

Yes, it is better. Actually you can inherit from UUIDGenerator

public class MyUUIDGenerator : UUIDGenerator
{
 public override string Generator(ContentBase content)
{
  return Guid.NewGuid().ToString(); //.....
}
}

//Replace the DefaultGenerator when the application in starting

UUIDGenerator.DefaultGenerator  = new MyUUIDGenerator();

// It is the expected extend method in the old version of Kooboo CMS which without IoC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment