Thursday, January 29, 2009

SQL Server triggers used to prevent users to modify data

IF EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[InterzicereModificari]'))
DROP TRIGGER [InterzicereModificari]
GO


CREATE TRIGGER dbo.DenyWrite_ViramenteAntet
ON [dbo].[Viramente - antet]
AFTER INSERT,DELETE,UPDATE
AS
BEGIN
RAISERROR ('Nu mai aveti dreptul de a modifica din acest program', 16, 1);
END
GO

Friday, January 16, 2009

MemoryDictionaryCache reminder


public interface ICache
{
void Reset();
void Load();
string LoadMessage { get; set; }
}

public class MemoryDictionaryCache<T, Z> : ICache
{
public MemoryDictionaryCache(Func<Dictionary<T, Z>> loadCache, string loadMessage, Func<Dictionary<T,Z>, T, Z> retrieveValue)
{
_load = loadCache;
LoadMessage = loadMessage;
_retrieveValue = retrieveValue;
}

private Func<Dictionary<T, Z>> _load;
Func<Dictionary<T, Z>, T, Z> _retrieveValue;
public string LoadMessage { get; set; }

private Dictionary<T, Z> _cache = null;
private Dictionary<T, Z> Cache
{
get
{
if (_cache == null)
Load();
return _cache;
}
}

public void Reset()
{
_cache = null;
}

public void Load()
{
using (var waitWindow = new WaitWindow(LoadMessage ?? "Asteptati incarcarea datelor"))
{
_cache = _load();
}
}

public Z GetValue(T forKey)
{
return _retrieveValue(Cache, forKey);
}
}

private Dictionary<Type, ICache> _caches = new Dictionary<Type, ICache>();

private void Initialize()
{
_caches.Add(typeof(ContractID), new MemoryDictionaryCache<ContractID, ContractEntity>(
()=>new Dictionary<ContractID, ContractEntity>(),
"Incarc contractele",
(dic, contractID)=>dic.GetValue(contractID)));
}

public ContractEntity GetContractul(ContractID contractID)
{
return ((MemoryDictionaryCache<ContractID, ContractEntity>)_caches[typeof(ContractID)]).GetValue(contractID);
}