Showing posts with label Patterns. Show all posts
Showing posts with label Patterns. Show all posts

Thursday, 17 October 2013

No Cache Attribute in MVC

    using System;
using System.Web;
using System.Web.Mvc;
using RRA.Core.Services.Utilities;

///
/// Disables browser caching.
///

[AttributeUsage(AttributeTargets.Method)]
public class NoCacheAttribute : ActionFilterAttribute
{
///
/// Called by the ASP.NET MVC framework before the action result executes.
///

/// The filter context.
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetETag((Guid.NewGuid()).ToString());
filterContext.HttpContext.Response.Cache.SetExpires(SystemDateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}


Tuesday, 23 July 2013

Unit of work pattern in WCF

See here for an example of how to implement a unit of work pattern in WCF (using Unity).

http://blogs.5dlabs.it/post/2012/05/23/Implementing-Repository-and-UnitOfWork-patterns-in-a-WCF-service-using-Unity.aspx

public class CustomServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
  {
return new CustomServiceHost(serviceType, baseAddresses);
  }
}

public class CustomServiceHost : ServiceHost
{
static IUnityContainer _container;
static CustomServiceHost()
  {
    _container = new UnityContainer();
    _container.RegisterType<ICompanyRepository, CompanyRepository>();
    _container.RegisterType<IUnitOfWork, DbUnitOfWork>();
    _container.RegisterType<SVCContext>(new ServiceInstanceLifeTimeManager());
  }
public CustomServiceHost(Type serviceType, params Uri[] : base(serviceType, baseAddresses)
  {
  }
protected override void ApplyConfiguration()
  {
base.ApplyConfiguration();
    Description.Behaviors.Add(_container.Resolve<UnityServiceBehavior>());
  }
}

public class UnityServiceBehavior : BehaviorExtensionElement , IServiceBehavior
{
IUnityContainer _container;
public UnityServiceBehavior(IUnityContainer container) : base()
  {
    _container = container;
  }
public void AddBindingParameters(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase,
Collection<ServiceEndpoint> endpoints,
BindingParameterCollection bindingParameters)
  {
  }
public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
  {
Type serviceType = serviceDescription.ServiceType;
IInstanceProvider instanceProvider = new UnityInstanceProvider(_container, serviceType);
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
    {
foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
      {
        endpointDispatcher.DispatchRuntime.InstanceProvider = instanceProvider;
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(
new UnitOfWorkMessageInspector(_container)
        );
      }
    }
  }
public void Validate(ServiceDescription serviceDescription,
ServiceHostBase serviceHostBase)
  {
  }
public override Type BehaviorType
  {
get { return this.GetType(); }
  }
protected override object CreateBehavior()
  {
return this;
  }
}

public class UnityInstanceProvider : IInstanceProvider
{
IUnityContainer _container;
Type _serviceType;
public UnityInstanceProvider(IUnityContainer container, Type serviceType)
  {
    _container = container;
    _serviceType = serviceType;
  }
public object GetInstance(InstanceContext instanceContext, Message message)
  {
return _container.Resolve(_serviceType);
  }
public object GetInstance(InstanceContext instanceContext)
  {
return this.GetInstance(instanceContext, null);
  }
public void ReleaseInstance(InstanceContext instanceContext, object instance)
  {
  }
}

 

 

public interface IUnitOfWork
{
void Commit();
}

public class DbUnitOfWork : IUnitOfWork
{
SVCContext _context;
public DbUnitOfWork(SVCContext context)
  {
    _context = context;
  }
public void Commit()
  {
    _context.SaveChanges();
  }
}

 

public class UnitOfWorkMessageInspector : IDispatchMessageInspector
{
IUnityContainer _container;
public UnitOfWorkMessageInspector(IUnityContainer container)
  {
    _container = container;
  }
public object AfterReceiveRequest(ref Message request, IClientChannel channel,
InstanceContext instanceContext)
  {
return _container.Resolve<IUnitOfWork>();
  }
public void BeforeSendReply(ref Message reply, object correlationState)
  {
    ((IUnitOfWork)correlationState).Commit();
  }
}

Thursday, 17 January 2013

Project Silk for all your HTML web application needs

A colleague of mine pointed out this white paper article that Microsoft have kindly put together, which appears to be a full overview on how to construct a web site in this day an age.

"Guidance for building cross-browser web applications with a focus on client-side interactivity. These applications take advantage of the latest web standards like HTML5, CSS3 and ECMAScript 5 along with modern web technologies such as jQuery, Internet Explorer 9, and ASP.NET MVC3"

http://silk.codeplex.com/

You can also download it all in pdf format from the following link

http://www.microsoft.com/en-us/download/details.aspx?id=27290

Monday, 22 October 2012

Design patterns

Other than the usual gang of four articles, http://www.dofactory.com/Patterns/Patterns.aspx, today I chanced across this page whilst reading the asp.net mvc best practices wiki, which had some more sample code for design-patterns....

http://wiki.asp.net/page.aspx/276/design-patterns/

How to find the last interactive logons in Windows using PowerShell

Use the following powershell script to find the last users to login to a box since a given date, in this case the 21st April 2022 at 12pm un...