Posts

AOP through Unity interceptors - Compression

Today we are going to compress our data. We will still use AOP through Unity. The scenario is the following: we have a class generating a huge set of data (in our example, data structure is string). we don't want it to be kept in memory, because we will use it later, or not often. So one solution could be to compress this data & uncompress it once we will really need it. public interface IBigDataRepo     {         string GetData();     }  public class BigDataRepo : IBigDataRepo     {         public string GetData()         {             return new string('X', 100000);         }     } We are going to use is this way: static void Main(string[] args)         {             using (var container = new UnityContainer())             {                 container.AddNewExtension<Interception>();                 // Register                 container.RegisterType<IBigDataRepo, BigDataRepo>(                     new Interceptor<InterfaceInterceptor>(),       

AOP through Unity interceptors - Performance monitoring

Today we continue the serie on AOP with Performance monitoring. The principle is very simple: we want to monitor the time a method took to execute. Here is the code of the class. public class PerformanceMonitoringInterceptionBehavior : IInterceptionBehavior     {         public IEnumerable<Type> GetRequiredInterfaces()         {             return Type.EmptyTypes;         }         public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)         {             Stopwatch sw = Stopwatch.StartNew();             var result = getNext()(input, getNext);             sw.Stop();              WriteLog(String.Format("Method {0} took {1} ms", input.MethodBase, sw.ElapsedMilliseconds));             return result;         }         public bool WillExecute         {             get { return true; }         }         private void WriteLog(string message, string args = null)         {             var utcNow = DateTime.UtcNow;            

AOP through Unity interceptors - Caching

Image
Let's continue our serie on AOP with another popular usage of interception: caching. Let's use the previously created project and add this class: public class CachingInterceptionBehavior : IInterceptionBehavior     {         private static IDictionary<string, MemoryCache> Cache = new Dictionary<string, MemoryCache>();         private static IList<string> SyncLock = new List<string>();         private static object SyncRoot = new object();         public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)         {             if (input.MethodBase.GetCustomAttribute<IgnoreCacheAttribute>() != null)             {                 return getNext()(input, getNext);             }             var arguments = JsonConvert.SerializeObject(input.Arguments);             string key = string.Concat(input.MethodBase.DeclaringType.FullName, "|", input.MethodBase.ToString(), "|", arguments); if (IsInCa

AOP through Unity interceptors - Logging

The first thing we think when talking about interception is logging. Let's see an implementation of a Logger. But before introducing the Logger implementation, let us scaffold a console sample project. public interface IFoo { FooItem GetItem(string name); } public interface IStore { StoreItem Get(int id); IEnumerable<StoreItem> GetAll(); void Save(StoreItem item); void ThrowException(); } public class FooImpl : IFoo { public FooItem GetItem(string name) { return new FooItem { Name = name }; } } public class StoreImpl : IStore { public StoreItem Get(int id) { Thread.Sleep(1000); return new StoreItem { Id = id, Name = "Fake name" }; } public IEnumerables<StoreItem> GetAll() { Thread.Sleep(2000); return new List&ltStoreItem>()

[Xamarin Forms] Button text alignment issue in Android

Image
Recently, I had to deal with custom Buttons in my Android application. So I designed custom renderers to render it. Unfortunately, I encountered a strange behavior with the text alignment of my buttons. This happens when the button changes of state : - Originally the button's text is centered - When you click it, the text moves to the left See images below:   Before / after the button click After investigating, I found a solution to my problem. In my Android custom button renderer, I had to overwrite a specific method:  ChildDrawableStateChanged public class ExtButtonRenderer : ViewRenderer<Extbutton, global::android.widget.button> { ... public override void ChildDrawableStateChanged(Android.Views.View child) { base.ChildDrawableStateChanged(child); if (Control != null) Control.Text = Control.Text; } ... } That's all ! Related links: Custom renderers: http://developer.xamarin.com/guides

[Fast&Furious guide] Get ready to build a Xamarin iOS app

Image
In order to build, test and deploy an iOS application using the Xamarin SDK, you will need to setup your environement first. As you know, it's necessary to have a Mac computer that will be at least your build host... This post is just a quick guide to help you to setup your environment ! So let's go ! Setup your environnement (2 possibilities) The hardware needed: A- First choice: A MAC (MacBook Pro for instance) with a Virtual Machine (VmWare / Parallels) running Windows and Visual Studio with Xamarin. B- Second choice A MAC (will be used only as a build host) A PC (with Windows / Visual Studio / Xamarin SDK) on the same network Requirements: Minimum MAC OS versions : OS X Mountain Lion, iOS Xamarin SDK, XCode IDE Minimum Windows versions : Windows 7, Visual Studio 2010, latest Xamarin tools C-  You will also need an iOS developer account You need to have an iOS developer account in order to: - test / deploy your app on a device - publish your app

AOP through Unity interceptors - Introduction

Image
Hello, Today we will start a serie of blog posts concerning Aspect Oriented Programming (formely AOP ). This technic is used to have a better separation of concerns in the application you write. Let's take a basic example. When you are writing the implementation of a class that is specialized on a task, you could write someting like this: public class Calculator { public decimal Add(decimal num1, decimal num2) { Console.WriteLine("Entering Add Method"); var result = num1 + num2; Console.WriteLine("Exiting Add Method"); return result; } } The issue with this code is that the logging is part of the method. What will happen if you have a huge quantity of code? You will probably duplicate the logging code across te different class & methods you will write. Moreover, the Add method we wrote is not specialized on a single task: it has two different purpose: it effectly performs an adds operation and it lo

[Xamarin Forms] Embed custom fonts in your apps

Image
Recently I had some difficulties to embed custom fonts in my applications. Specially with the iOS application that was crashing with no message (while it was working for the Android version). In fact, it was a font issue. So in this post I will show you some tips to embed quickly custom fonts in both your Android & iOS apps. 1- Select your fonts The first step is to get some cool fonts you want to use. You'll maybe use (free) fonts from various websites like: www.1001freefonts.com www.dafont.com 2- Set font files properties In your solution, include your fonts files: iOS:  Create a subfolder (in the the 'Resources' directory) where to embed the fonts Build Action = BundleResource & "Copy Always" Android : Create a subfolder in the 'Assets' directory where to embed the fonts: Build Action = "AndroidAsset" & "Copy Always" 3- iOS application: edit your plist file With iOS you need to specify the e