Skip to content

Instantly share code, notes, and snippets.

View sapiens's full-sized avatar

Mihai Mogosanu sapiens

View GitHub Profile
_db.RetryOnTransientErrorAsync(cancel
, db => db.WithSql(
q=>q.From<AssetsWithContextViewRow>()
.Where(d=>d.ProjectId==input.ProjectId && d.Name.Contains(input.Word))
.Limit(30)
.SelectAll(useAsterisk:true)
.MapTo<AssetInfoWithContext>()
)
.GetRowsAsync());
@sapiens
sapiens / dbus.cs
Created April 12, 2016 22:03
dbus example
//config
ServiceBus.ConfigureWith(new AutofacBusConfigurator(_boot.Container))
.ForMonolith(bus =>
{
bus.PersistAuditsWith(new InMemoryAuditStorage())
.WithoutErrorsQueues()
.ConfigureSagas(s=>s.WithoutSagas())
;
bus.WithSqlServerStorages(_boot.DbConnection,
d => d.EnableProcessorStorage().EnableReserveIdStorage())
@sapiens
sapiens / sfu3-in-action.cs
Last active January 12, 2016 21:24
SqlFu3 in action
//selecting related assets from a view then processing each row
//into a RelatedItem which gets added to a result
//generated sql is
//select [Id],[Type],[Name],[ContextId] from [AssetUsagesView]
//where ((([AssetId] = @0) and ([ProjectId] = @1)) and [Category] in (@3,@4))
using (var db = await _db.CreateAsync(cancel))
{
await db.QueryAndProcessAsync(
q =>
// this is a POCO , used as a source to create a table. In SqlFu 3, POCOs are **sources** or **destinations** of data,
// there is no object to table mapping like in an ORM
public class AssetsRow
{
public Guid Id { get; set; }
public Guid ProjectId { get; set; }
public Guid? ContextId { get; set; }
public string Name { get; set; }
public string Description { get; set; }
@sapiens
sapiens / cqrsfun.cs
Created September 23, 2015 19:38
read model updater
public class ReadModelUpdater:
ISubscribeTo<CommandCreated>,ISubscribeTo<CommandDeleted>,ISubscribeTo<CommandRenamed>
,IExecute<UpdateCommandDetails>,IExecute<UpdateCommandBehaviour>,IExecute<UpdateCommandConstraints>,IExecute<UpdateCommandModel>
,IExecute<AddTrigger>,IExecute<RemoveTrigger>
,IExecute<AddQueryToCommand>,IExecute<RemoveQueryFromCommand>
,ISubscribeTo<CommandOutcomeAdded>,ISubscribeTo<CommandOutcomeDeleted>,ISubscribeTo<CommandOutcomeRenamed>
,IExecute<AddEventToOutcome>,IExecute<RemoveEventFromOutcome>
@sapiens
sapiens / sqlfu3.cs
Last active September 18, 2015 20:07
SqlFu3 example
SqlFuManager.Configure(c =>
{
c.AddProfile(SqlServer2012Provider.Instance, Setup.Connex);
});
_getDb = SqlFuManager.GetDbFactory();
_getDb.Do(db =>
@sapiens
sapiens / lotsofasync.cs
Created July 12, 2015 17:28
lots of async
public Task<OutputValue<Guid>> HandleAsync(GetOrganizationId input, CancellationToken cancel)
{
return GetDataAsync("org" + input.Name, async () =>
{
return await _db.DoAsync(async d =>
{
OutputValue<Guid> result = null;
var res = await d.GetValueAsync<Guid?>("select Id from Orgs where Organization=@0", input.Name);
if (res.HasValue)
{
public class MyConventions:AspNetMvcConventionsModule
{
public void override Configure(AspNetMvcConventions convetions)
{
conventions.Always
.ControllersLookLike(type=> type.Name.EndsWith("Endpoint"))
.ActionsLookLike(methodInfo=> methodInfo.IsPublic);
//or
public ActionResult Post(AddPageModel model)
{
if (model.IsEmpty)
{
goto redirect;
}
var createPageData = new CreatePageData(model,this.GetCurrentTenantId());
if (model.Cmd == AddDocumentModel.CmdDraft)
@sapiens
sapiens / select template based on model data.cs
Created May 7, 2014 18:09
select template based on model data
//view engine convention which select a different template for the same type, depending on model value
// when Format is html we want DocumentContent.html.chstml
// when Format is markdown we want DocumentContent.markdown.chstml
//etc
public class DocumentContentTemplates : IFindViewConvention
{
public bool Match(ControllerContext context, string viewName)
{
return viewName.Contains("DocumentContent");
}