SQLSaturday #229 #Event

Hello everybody,

On next 21 and 22 of June 2013 in  Hilton Dublin, Charlemont Place, Dublin 2,  the SQL Saturday #229 will be back to the Republic of Ireland’s capital: http://www.sqlsaturday.com/229/

I’m happy to comment that I’m preparing a presentation for that event titled “Data Architectural Patterns in C#”, more information here: http://www.sqlsaturday.com/viewsession.aspx?sat=229&sessionid=14846

The session topics are described as follows:
(1) Data architectural patterns that can be implemented in a modern Software Architecture for favoring non-functional requirements such as resource management (concurrency, even distribution) and resource demand (incremental caching, hash partitions).
(2) Design techniques (like Resource Pooling, Do Not Wait / Fire and Forget , caching components) and principles (like Eventual consistency, CAP theorem, Fine grained vs coased grained, Statelessness, Idempotency and fallacy of zero latency) to improve the application performance in the data access layer.

The session is for Software Developers/Designers/Architects and Data Architects interested in exploring the available software architectural patterns and techniques.

Hopefully see you there!

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer

Guidelines for designing layers and packages – Part II

This column is the (unplanned) second part of the previous post: “Guidelines for designing layers and packages”.

As a software architect you should ask some questions (and come up with a serious evaluation of some alternatives) like the following ones:

  • What are the operational (common) services you (own and) provide out of the box to all projects (i.e.: authentication, provisioning, SOMETHING to consistently read/write parameters –into DB or config files-, a tenant load balancer –for multi-tenant SAAS environments-,…).

If more than one developer in your team uses AppSettingsReader (and you AS technical lead haven’t come up with a wrapper class around it, consider yourself a bad SW Designer –therefore a terrible SW Architect-)

  • Which layer(s) should handle the errors?
  • If your solution contains Web Services, where will you place them? (e.g.: in the same application directory that other application pages)
  • Where will you place the Factories? (i.e.: in the business rules layer).
  • Where will you place the common abstractions? I mean, if you did any technique based on common abstractions (dependency inversion, strategy pattern, IoC, …).
  • How to handle the code (from upper layers) which does not use the single Facade entry point of the next layer below?

As a software designer you should ask:

  • What’s your policy for finalizing unmanaged resources? (e.g.: enclosing them in using blocks? Close + Dispose?)
  • What are the definitions for common constants? (e.g.: -1 for not found? Date time formats?)
  • How many classes should a package contain? How many packages should a layer contain? How many layers should a module contain?

A software layer usually contains one package, BUT is not always the case (prepare yourself to recognize them).

  • How is going to be the internal DLL distribution process? (An email to ALL does not count).
  • How will technology/middleware specific object be consumed? I mean, if you’re using a distributed cache, will all of you projects have references to that specific DLL (I call them dirty objects)?

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer

Introducing: Naming Interface Delegation

Depending on abstractions [1], decoupling [2] dependencies and segregating interfaces [3] are highly desirable design attributes to make systems that are easy maintain and extend over time. However if the fan-in [4] (S. Counsell, A. Mubarak, and R. M Hierons: An Evolutionary Study of Fan-in and Fan-out Metrics in OSS)  coupling metrics of the resulting shared interfaces are low then the design leads to code bloat [5] as a side effect [6].

A common cause of low fan-in is the different types of arguments that a highly cohesive set of algorithms can handle. The following code helps to illustrate the previous situation by defining an IList.List function which is implemented by a DAO class.

public interface IListable
{
 object List(int recordId);
 }

public class DAO : IListable
 {

public object List(int recordId)
 {
 //Code to query a data repository using the given Id
 }
 }

The purpose of the IList interface is providing a centralized List abstraction of repository records. The DAO class is a sample class to show the IList implementation. When different data types are needed (for instance: a record description of “string” type) then data type dependent interfaces becomes code bloat. A common approach for addressing the previous issue consist in upcasting [7] the input parameters to object types and providing metadata to perform the downcasting right.

This approach can be seen in the Microsoft’s DbParameter [8] and IDataParameter [9] implementations; those implementations provide a Value object property and a DbType meta-data property. This approach represents an issue because data must be casted and potentially boxed/unboxed [10]. Boxing (automatic or manual) is an expensive operation because a new wrapper must be allocated and constructed. UnBoxing is also expensive computationally.

A second well known approach is based on using Generics [11] or abstract algorithms with template [12] arguments’ types. The main issue of this kind of parametric polymorphism [13] is that function signatures are defined by the consumers of the interface, so the actual specific implementation has to cast to a specific type as described in the following RepositoryDAO.Get function.


public interface IGetable
 {
 object Get<T>(T contextData);
 }

public class RepositoryDAO : IGetable
 {
 public object Get<T>(T contextData)
 {
 string obj = Convert.ToString(contextData);
 //Do something with the value
 }
 }

Abstractions should not care about different types because their purpose is generalizing a behavior regardless of the underlying details. To keep the number of segregated integrated under a manageable number to make it easy to understand, maintain and extend a potential solution should provide static concrete arguments’ data types and inferred dynamic binding to avoid casting and code bloat. The previous new concept is expressed in the following pseudo code:


public interface IRead
 {
 object Read(dynamic T contextData);
 }

public class DAOReader:IRead
 {
 public object Read(string contextData)
 {
 //Do something with the concrete argument
 }
 }

public class Consumer
 {
 public void Test()
 {
 IRead obj=new DAOReader();
 obj.Read("Hello World!");
 }
 }

The previous potential requirements could be summarized as “Naming Interface Delegation” because the actual interface type definition is delegated to the consumer during early binding; plus they favor the Open Close design principle [14].

Note that pseudocode is for illustration purposes only because no formal research has been carried out at the moment. Also note that Naming Interface Delegtaion seem similar to Weak Typing and dynamic dispatch because their syntax is similar but the semantics are different. In the Weak Typing case syntax means that a few validation rules for compiling are in place while dynamic dispatch [15] is specific to dynamic selection of the callee/target function based on the caller’s data types. Because types are inferred during the dynamic binding by the interface, the variadic [16] functionality is not desirable.

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer

Introducing: Orthogonally Attachable Interfaces

This column describes an issue arising when implementing two opposite desired design attributes (the Interface Segregation Design Principle and the orthogonally concept) and a draft proposal of how addressing it.

Under the Object Oriented Programming paradigm every system concept should be represented as an object with fields and procedures. In turn, it’s a common software development practice splitting related procedures into smaller and specific interfaces to make the program easier to understand and maintain.

Let’s consider the following DAO[1] with two simple operations: List and Delete.

public class TableDAO
 {
 public object List(object contextData)
 {
 //Code to list data from a data repository
 return null;
 }

 public void Delete(object contextData)
 {
 //Code to delete data from a data repository
 }
 }

In order to meet the Interface-segregation principle stating that [2] “CLIENTS SHOULD NOT BE FORCED TO DEPEND UPON INTERFACES THAT THEY DO NOT USE” we should provide the specific List and Delete interfaces as described in the following code.


public interface IList
 {
 object List(object contextData);
 }

public interface IDelete
 {
 void Delete(object contextData);
 }

public class TableDAO: IList, IDelete
 {
 public object List(object contextData)
 {
 //Code to list data from a data repository
 return null;
 }

 public void Delete(object contextData)
 {
 //Code to delete data from a data repository
 }
 }

A potential consumer of the TableDAO services might want to invert the dependencies (as stated in the complementary Dependency Inversion Principle[3]) to consume separately the specific interfaces as illustrated with the following Client class.


public class Client
 {
 public void TestIList()
 {
 IList daoObj = new TableDAO();
 daoObj.List(null);
 }

public void TestIDelete()
 {
 IDelete daoObj = new TableDAO();
 daoObj.Delete(null);
 }

}

The issue arises when you want to access both services (List and Delete) within the same block in a computationally efficient and engineer friendly manner. Consider the following three solutions for the previous scenario.


public void TestBoth()
 {
 //Solution # 1
 TableDAO daoObj = new TableDAO();
 daoObj.List(null);
 daoObj.Delete(null);

//Solution # 2
 IList daoObj1 = new TableDAO();
 daoObj1.List(null);
 IDelete daoObj2 = new TableDAO();
 daoObj2.Delete(null);

//Solution # 3
 IList daoObjn = new TableDAO();
 daoObjn.List(null);
 ((IDelete)daoObjn).Delete(null);

}

The three solutions are flawed because they sacrifice at least one of the gained benefits. The solution number 1 removes an abstraction layer. The solution number two wastes memory space and processor time and the solution number three forces the developer to do casting thus loosing static type checking [4] and fluency [5]. None of the sacrificed attributes should be accepted, which leads Software Engineers to find alternatives solutions.

A rational triage could be including the List signature into the Delete interface so consumers of the List interface would see the List function while the Delete consumers would see the List and Delete functions, which solves the need of using both of the functions within the same block at the expense of consumers wanting to use the Delete operation only. This approach is contradictory to the Single Responsibility[6] and DRY [7] design principles but it’s tolerable depending on the project size.

The project size is important because in small systems is tolerable having two different classes implementing the two different interfaces despite they belong to the same concept. For instance: a small project with 7 DAOs implementing List and Delete operations would require 14 classes. This approach promotes scattering [8], low cohesion and reduces maintainability because it would increment the quantity of code that needs to be understood to make even a small change.

In order to address the described issue (coded in C# language) segregated interfaces should be combined to attach a set of low coupled and highly cohesive relations in medium to big projects. The following pseudo code illustrates how orthogonally attachable interfaces might be used to allow static type checking, fluency programming, efficient computation (no casting) and orthogonally.


orthogonal<IList, IDelete> ortObj = new TableDAO();
 ortObj.List();
 ortObj.Delete();

The previous code does not pretend to be an ultimate solution because no formal research has been carried out; it only illustrates how from a Software Engineer point of view the issue could be addressed with this new Orthogonally Attachable Interfaces concept; however, the final definition could include operators to define them (e.g. var daoObj = IList + IDelete).

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer

How To Open an Excel File in an ASP.Net App?

Hello there,

Recently somebody asked me the previous question an I wanted to share the following explanation rather than just pointing to the solution because the person tried “alternative” solutions like Process.Start or proprietary Excel automation objects:

To open server files (ie Excel files) on the client browser (to load them within a frame or to show the Save As prompt)  is necessary to write them down using Response.TransmitFile. Source: DotNetSpider.

I also was asked the following  (nor necessary related to C# code) questions (I published them because they can help someone else):

  • How to share the WiFi connection between two laptops? Short answer: Execute the following commands (Source: RedmonPie)

netsh wlan set hostednetwork mode=allow ssid=YOUR_SSID key=YOUR_PASS

netsh wlan start hostednetwork

  • How to reverse the caps lock? If you use a laptop for development  probably from time to time you inverse the caps lock or your mouse starts selecting the text just by moving over. Short answer: hit the two Shift keys.  Source: Help.com
  • How to solve the “The project type is not supported by this installation” error when loading an ASP.Net Web application with Visual Studio 2012 in a “clean” machine? Short Answer: An installation is missing, probably MVC3 (download it here). Source: MSDN and StackOverflow.
  • How to solve the  ”Could not load file or assembly “System.EnterpriseServices.wrapper.dll” or one of it’s dependencies.” error? Short answer: Add a reference to a valid Wrapper DLL (ie: C:\WINDOWS\WinSxS\x86_System.EnterpriseServices_b03f5f7f11d 50a3a_2.0.0.0_x-ww_7d5f3790\). Source: Andica.com.

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer

Designing “in” patterns vs. designing “into” patterns

Design patterns are fundamental part of the glue which sticks together the 20% percent of the design which makes 80% of the work. They’re the frontier line between Architects and Designers. They’re a common work area. But not for everyone is obvious that patterns describe a general solution for a given scenario. General solution is not intended to be the solution for your project. It’s just a general solution. It might work as is or it might not.

Let’s consider the Observer pattern. In articles and commercial products is suggested to use the pattern in real world scenarios as is. Implementing the patterns as is involves defining a subject interface, a concrete subject, an observer and a concrete observer. You should not be constraint by the conventions of a particular design pattern. The pattern offers you a solution for implementing notifications/actions based on observing changes of states. The pattern should be evaluated before implementing it. Don’t create classes or interfaces just because the pattern commands you so. Start with the functional requirements and then the non-functional requirements in a customer first style.

If you are a C# developer willing to implement the Observer pattern bear in mind that the pattern is not restrained for UI scenarios. The MVVM pattern and the C# ObservableCollection implementation make people think so, but the pattern goes far beyond UI scenarios. So, don’t get confused by commercial products implementing anti-patterns or by vendor specific built-in solutions that are intended for a subset of scenarios solved by the pattern.

First off, if you don’t need all the classes suggested by the pattern, don’t implement them. Then take advantage of your development platform without adding exotic elements in the process. The best implementation of the Observer pattern I have found so far uses events and delegates to decouple the Observer of the Subject. So you don’t have to keep track of the concrete Observers within the concrete Subject. In my opinion, holding a reference of the concrete observers within the concrete subject breaks encapsulation. I think the same about injecting a concrete observer as a dependency to the concrete subject.

I like the mentioned implementation because events and delegates follow the Hollywood principle “don’t call us, we’ll call you”, so you don’t need to explicitly call code on concrete observers and favor extensibility (see my previous post: Designing your API for extensibility), plus you can go further and add a mediator object to plug the subject with the observer. You could decouple two Facades working on common data by defining a subject interface exposing an event (similar to the INotifyPropertyChanged), then implement a concrete subject and hook it up with a concrete observer using a mediator.

If you haven’t noticed that the title was inspired in the following statement by Steve McConnell in his book Code Complete, You should grab one copy quickly, I read it for the first time several years ago and trust me, it will make you a better Designer:

Programmers who program “in” a language limit their thoughts to constructs that the language directly supports. If the language tools are primitive, the programmer’s thoughts are also primitive.

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer

Using the Amazon Elasticache in C#

There are several handy blogs about how to use the Amazon DynamoDb, Amazon EC2 and other services but I guess there are not enough information about how to use the Elasticache service. Indeed, the documentation only mentions that the service is Memcached compliant but it’s a pain for C# developers who are starting to use this service to figure out the way to use it.

As you probably might know, Amazon provides API’s for Elasticache operations (ie: create a cache cluster), but not for cache “transactions” (ie: add/remove an item from/to the cache). You can use your favorite Memcached client. I prefer the BeIT Memcached library.

After selecting a client library you can write the code to use it and bear in mind that you need to know the end point address provided by Amazon. To get it, log onto the Amazon Web Console and select the Elasticache option within the services menu. Then click on “Launch Cache Cluster” button and give it a meaningful name.

Wait a few seconds, click on your newly created cache and select the “Copy Node Enpoint(s)”, then you will get an URL like the following one:

AmazonElasticacheEndPoint

Finally define a cache object, grab the URL from the config file with some code like the next example and start using its Set/Get methods:


AppSettingsReader reader = new AppSettingsReader();
 string memCachedUrl = reader.GetValue("MemCachedUrl", string.Empty.GetType()) as string;
 MemcachedClient.Setup("MyCache", new string[] { memCachedUrl });
 cache = MemcachedClient.GetInstance("MyCache");
 cache.SendReceiveTimeout = 5000;
 cache.ConnectTimeout = 5000;
 cache.MinPoolSize = 1;
 cache.MaxPoolSize = 5;
 loggerObj = logger;

Cheers,

Javier Andrés Cáceres Alvis

Microsoft Most Valuable Professional – MVP
Intel Black Belt Software Developer
Seguir

Recibe cada nueva publicación en tu buzón de correo electrónico.