mardi 25 août 2015

How does this class work or how can you use it?

I fell over a class which looks like this:

public final class DatabaseType{

    public static final DatabaseType TYPE_LIMITED_TEXT = new DatabaseType();
    public static final DatabaseType TYPE_UNLIMITED_TEXT = new DatabaseType();
    public static final DatabaseType TYPE_DATE = new DatabaseType();
    public static final DatabaseType TYPE_DECIMAL = new DatabaseType();

    private DatabaseType(){
    }

    public String toString(){
        return "DatabaseType";
    }
}

I need to set the type but I want to understand what's happening here and I have no clue how this class works. Whatever variable I use it will always return an empty DatabaseType, with no information. So I wonder how you can get use of such a class. Maybe there is a name for this type of class?

lundi 24 août 2015

Singleton in DependencyInjection

I am struggling with concept of singletons in dependency injection. I am not sure whether classes should be implemented in way to support singleton / per instance instancing for classes intended to be used as singletons or whether they should rely on proper settings of instancing by programmer.

Following class will work as expected if it will be marked as singleton in Dependency container

...
builder.RegisterType<ApplicationSettings>().AsSelf().SingleInstance();
...


/// <summary>
/// This allows to create many ApplicationSettings instances which each of them will have its collection of settings. 
/// Thus we cannot guarantee that one of class has complete settings
/// </summary>
public class ApplicationSettings
{
    private readonly object _locker = new object();
    private readonly Dictionary<string, object> _settings;
    private readonly ILog _log;

    public ApplicationSettings(ILog log)
    {
        _log = log;
        _settings = LoadSettings();
        Thread.Sleep(3000); //inner hardwork, e.g. cashing of something
    }

    public object GetSettings(string key)
    {
        lock (_locker)
        {
            return _settings.ContainsKey(key) ? _settings[key] : null;
        }
    }

    public void SetSettings(string key, object value)
    {
        lock (_locker)
        {
            _settings.Remove(key);
            _settings.Add(key, value);
        }
    }

    public void Remove(string key)
    {
        lock (_locker)
        {
            _settings.Remove(key);
        }
    }

    public void Save()
    {
        Thread.Sleep(5000); //Saving somewhere
    }

    private Dictionary<string, object> LoadSettings()
    {
        Thread.Sleep(5000); //Long loading from somewhere
        return new Dictionary<string, object>();
    }
}

All classes which will need to use ApplicationSettings class will share one instance and thus Settings will contain all information when saving to somewhere. On the other hand if programmer do not mark class as SingleInstance there will be an issue when saving because if it will be implemented as replacing whole collection in storage place not all settings will be saved. Thus correct functionality strongly depends on programmer knowledge of class and using it as singleton.

In second example I am using static field for Settings which allows me to use class as singleton or as instancing per instance without effecting core functionality (I mean that not all settings will be saved if more instances of ApplicationSettings2 will be used)

/// <summary>
/// This allows to create many ApplicationSettings2 instances which each of them will share same collection of settings. 
/// </summary>
public class ApplicationSettings2
{
    private static readonly object Locker = new object();
    private static readonly Dictionary<string, object> Settings;
    private readonly ILog _log;

    static ApplicationSettings2()
    {
        Settings = LoadSettings();
        Thread.Sleep(3000); //inner hardwork, e.g. cashing of something
    }


    public ApplicationSettings2(ILog log)
    {
        _log = log;
    }

    public object GetSettings(string key)
    {
        lock (Locker)
        {
            return Settings.ContainsKey(key) ? Settings[key] : null;
        }
    }

    public void SetSettings(string key, object value)
    {
        lock (Locker)
        {
            Settings.Remove(key);
            Settings.Add(key, value);
        }
    }

    public void Remove(string key)
    {
        lock (Locker)
        {
            Settings.Remove(key);
        }
    }

    public void Save()
    {
        Thread.Sleep(5000); //Saving somewhere
    }

    private static Dictionary<string, object> LoadSettings()
    {
        Thread.Sleep(5000);
        return new Dictionary<string, object>();
    }
}

Both approach of class usage

...
builder.RegisterType<ApplicationSettings>().AsSelf().SingleInstance();
...

or

...
builder.RegisterType<ApplicationSettings>().AsSelf();
...

will lead to same expected functionality. Only difference is that not singleton instancing mode will lead to slower functionality (there is a sleep in ctor), but in the end of the day changing instancing mode does not break anything.

My questions:

  • Who is responsible for defining which class should be used as singleton in dependency container configuration?
  • Where should be this information about instancing stored?
  • Should I implement classes which are intending to be used as singleton to be able to work even in environment where it is instancing per instance?
  • If I am adding 3rd party DLL's class to my dependency container configuration, who is responsible to inform me how should I instancing this class? (How programmer can know which kind of instancing should he use? Should be this information mandatory part of documentation, or should programmer just use "try/use" approach?)
  • Should be "singleton" used only as way how to make system faster but should be singletons to be able to work even if they are instanced many times? (in dependency container configuration SingleInstance keyword is missing)

Writing a parser for a binary message format

I need to develop a parser for a binary message exchange format i.e., a message parser which parses a binary message into an java object representation. I would like to ask what useful patterns could be used to implement a parser in a most flexible way. Could anybody describe this in a nutshell or provide resources to read?

How to implement feature tracking?

We are about to begin the development of an application in Laravel 5.1 which requires tracking of every feature present in the application and consequent granting of user access on the fly.

Say for eg - Photos is a feature in the application. The owner can upload, view, delete, edit pictures. However a manager can have the permission to only upload and view pictures (the other options though available, will result in a pop up explaining the restriction to carry out the action).

Architecture wise, we have decided to have a database table listing features and an id associated with them. There will be another table mapping user to feature.

However the part we are struggling really is to decide how to track which feature is the user trying to access.

Our possible options till now are -

  1. Aspect Oriented Programming. To have join points defined and check user permission before method execution.
  2. Storing the feature id in the session and using the same on every call to the server to check if the user can access using the mapping table

Which of the above two is a better approach ? Any other better approach possible ?

In C#, if A implements IX and B inherits from A , does it necessarily follow that B implements IX?

In C#, if A implements IX and B inherits from A , does it necessarily follow that B implements IX? if yes is it because of LSP ? are there any differences between :

1.

Interface IX;
Class A : IX;
Class B : A;

and

2.

Interface IX;
Class A : IX;
Class B : A, IX;

?

Is there a terminology associated with B implementing IX though the chain of inheritance or B implementing IX directly?

PS: I am Assuming in both cases the interface is implemented implicitly, not explicitly. Any Patterns that utilize on 1 and would not work if they were implemented as in 2?

how would you sanitize '$' and '/' in string (java)

Having difficulty sanitizing the string to allow $ and / to stay within the string. Using the Pattern class and I'm not sure if I should place the

import java.util.regex.Pattern;

public String customiseText(String bodyText, List<Object> objectList) {
    Map<String, String> replaceKeyMap = extractMapFromList(objectList);
    String escapedString = Pattern.quote(bodyText); //
    // iterate over the mapkey and return the body text replace
    for (final String key : replaceKeyMap.keySet()) {
        String replacementKey = "(?i)" + key; // not case sensitive and empty string matcher
        String replacementValue = replaceKeyMap.get(key);
        if (replacementValue == null) {
            replacementValue = "";
        }
        escapedString = escapedString.replaceAll(replacementKey, replacementValue);
    }

    return escapedString;
}

How do I print this pattern?

I'm new to programming (C++) and having trouble with this problem.

I'm supossed to print out a diamond pattern, where a user enters the number which corresponds to the length of the diagonals. For example, if a user enters a number 7, it should look like this:

http://ift.tt/1Nx3mES