Chris McKelt

Remembering Thoughts

 

Recent comments

Archive

Authors

Categories

None


Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2012

Visual Studio Icons

For free icons that come with Visual Studio check out

 

C:\Program Files\Microsoft Visual Studio 9.0\Common7\VS2008ImageLibrary\1033\

 

VS2008ImageLibrary.zip


Posted by chris on Monday, February 22, 2010 9:45 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Stream bytes to files

                using (var stream =
                    Assembly.GetAssembly(typeof(StubPolicy)).GetManifestResourceStream(
                        "Documents.TestHelpers.Files.test.msg"))
                {
                    const int bufferLength = 256;
                    var buffer = new Byte[bufferLength];
                    if (stream != null)
                    {
                        int bytesRead = stream.Read(buffer, 0, bufferLength);

                        using (var fs = new FileStream(filename, FileMode.CreateNew, FileAccess.Write))
                        {
                            // Write out the input stream
                            while (bytesRead > 0)
                            {
                                fs.Write(buffer, 0, bytesRead);
                                bytesRead = stream.Read(buffer, 0, bufferLength);
                            }
                            fs.Close();
                        }
                        stream.Close();
                    }
                }

Posted by chris on Wednesday, February 10, 2010 6:48 AM
Permalink | Comments (5) | Post RSSRSS comment feed

ISpecification

using System;
using System.Collections.Generic;

namespace Matlock.Core.Specification
{
public interface ISpecification<T>
{
bool IsSatisfiedBy(T candidate);
ISpecification<T> And(ISpecification<T> other);
ISpecification<T> Or(ISpecification<T> other);
ISpecification<T> XOr(ISpecification<T> other);
ISpecification<T> AndAllOf(IEnumerable<ISpecification<T>> specifications);
T Target { get; set; }
void GetResults(ResultsVisitor visitor);
IEnumerable<Type> WhatWasAssessed();
Risks.IMatlockCommand GetCommand();
}
}

 

 

using System;
using System.Collections.Generic;
using Matlock.Core.Commands;
using Matlock.Core.Risks;
using Matlock.Core.Specification.Common;

namespace Matlock.Core.Specification
{
    public abstract class AbstractSpecification<T> : ISpecification<T>
    {
        protected SpecificationResult result;
        protected IList<Type> ruleList = new List<Type>();
        protected bool SoftCheck { get; set; }

        public T Candidate { get; set; }
        public T Target { get; set; }
        public bool IsReplay {protected get; set;}

        public ISpecification<T> And(ISpecification<T> other)
        {
            return new AndSpecification<T>(this, other);
        }

        public ISpecification<T> Or(ISpecification<T> other)
        {
            return new OrSpecification<T>(this, other);
        }

        public ISpecification<T> XOr(ISpecification<T> other)
        {
            return new XORSpecification<T>(this, other);
        }

        public ISpecification<T> AndAllOf(IEnumerable<ISpecification<T>> specifications)
        {
            return new AndAllOfSpecification<T>(this, specifications);    
        }

        public virtual bool Evaluate(T candidate)
        {
            throw new NotImplementedException("You need to provide a predicate or override IsSatisfiedBy");
        }

        public virtual IMatlockCommand GetCommand()
        {
            return new NoCommand();
        }
        
        public virtual void GetResults(ResultsVisitor visitor)
        {
            if (result != null && !string.IsNullOrEmpty(result.Message))
            {
                visitor.Add(result);
            }
        }

        public virtual IEnumerable<Type> WhatWasAssessed()
        {
            return ruleList;
        }

        public virtual string MessageFormat()
        {
            throw new NotImplementedException("You need to provide a result a message or override IsSatisfiedBy");
        }

        public virtual bool IsSatisfiedBy(T candidate)
        {
            if (IsReplay)
            {
                return true;
            }
            
            Type ruleType = GetType(); 
            ruleList.Add(ruleType);

            Candidate = candidate;

            bool satisfied = Evaluate(candidate);
            if (!satisfied || (SoftCheck && !string.IsNullOrEmpty(result.Message)))
            {
                result = new SpecificationResult
                             {
                                Satisfied = SoftCheck, 
                                Message = string.Format(MessageFormat(), candidate, Target)
                             };
            }

            return satisfied;
        }

        public AbstractSpecification<T> SuccessOrRhs(ISpecification<T> specification)
        {
            return new SuccessOrRhsSpecification<T>(specification);
        }
    }
}

Posted by chris on Wednesday, February 03, 2010 7:24 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Castle Windsor – WCF Endpoint Configuration

          const int maxSize = 52428800;

            var binding = new BasicHttpBinding();
            binding.Security.Mode = BasicHttpSecurityMode.TransportCredentialOnly;
            binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
            binding.Security.Transport.ProxyCredentialType = HttpProxyCredentialType.None;
            binding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;
            binding.Security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
            binding.MaxReceivedMessageSize = 1000000;
            binding.CloseTimeout = new TimeSpan(0, 1, 0);
            binding.OpenTimeout = new TimeSpan(0,1,0);
            binding.ReceiveTimeout = new TimeSpan(0,10,0);
            binding.SendTimeout = new TimeSpan(0,1,0);
            binding.AllowCookies = false;
            binding.BypassProxyOnLocal = false;
            binding.HostNameComparisonMode = HostNameComparisonMode.StrongWildcard;
            binding.MaxBufferSize = maxSize;
            binding.MaxBufferPoolSize = maxSize;
            binding.MaxReceivedMessageSize = maxSize;
            binding.MessageEncoding = WSMessageEncoding.Mtom;
            binding.TextEncoding = Encoding.UTF8;
            binding.TransferMode = TransferMode.Buffered;
            binding.UseDefaultWebProxy = true;

            binding.ReaderQuotas.MaxDepth = 32;
            binding.ReaderQuotas.MaxStringContentLength = maxSize;
            binding.ReaderQuotas.MaxArrayLength = maxSize;
            binding.ReaderQuotas.MaxBytesPerRead = maxSize;
            binding.ReaderQuotas.MaxNameTableCharCount = maxSize;

            container = new IocContainer(LifestyleType.Transient);
            container.AddFacility<WcfFacility>().Register(
                Component
                    .For<ISharePointFacadeService>()
                    .Named("DmsGateway")
                    .ActAs(
                        new DefaultClientModel()
                        {
                            Endpoint = WcfEndpoint
                                        .BoundTo(binding)
                                        .At("http://localhost/SharepointFacade/DMSService.svc/mex")
                        }));

Posted by chris on Monday, February 01, 2010 1:08 AM
Permalink | Comments (0) | Post RSSRSS comment feed