RSS

Monthly Archives: April 2012

Entity Framework Validation Exception

One of the beauty of using Entity Framework is that it will throw out validation exception based on the database required columns and data type. This means, we don’t need to set any validation rules in application level. Entity Framework will check the rules in database and throw exception based on these rules.

When validation exception is thrown, we can access Entity Framework exception from DbEntityValidationException class, as the following:

try
{
    db.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var entityValidationError in e.EntityValidationErrors)
    {
        Console.WriteLine("Entity Type \"{0}\", Entity State \"{1}\", Errors:", entityValidationError.Entry.Entity.GetType().Name, entityValidationError.Entry.State);
        foreach (var validationError in eve.ValidationErrors)
        {
            Console.WriteLine("Property Name: \"{0}\", Error Message: \"{1}\"", validationError.PropertyName, validationError.ErrorMessage);
        }
    }
}

Or, instead of using try { } catch { }, we can also call GetValidationErrors method to get list of validation errors.
GetValidationErrors method is inherited from DbContext class. So, if you are using code generation, you will need to use DbContext Generator in order to access this method.

var validationErrors = db.GetValidationErrors();

Code is tested in Entity Framework 4.3.1

Source: MSDN

 
1 Comment

Posted by on April 27, 2012 in General

 

Tags: , ,

WCF System Built-In Bindings

Name Description
<webHttpBinding> Interoperable RESTful communicaton via HTTP.
<basicHttpBinding> Interoperable SOAP communication via HTTP. Only basic protocols that conform to WS-I basic profile. Default message encoding is text/xml.
<wsHttpBinding> Interoperable SOAP communication via HTTP. Full range support of SOAP + WS*-protocols. Suitable for non-duplex services.
<netTcpBinding> Cross machine communication via TCP. Only for .Net to .Net communication.
<netNamedPipeBinding> Same machine communication via IPC. Only for .Net to .Net communication.
<netMsmqBinding> WCF communication via MSMQ. Only for .Net to .Net communication.
<netPeerTcpBinding> Cross machine communication via P2P. Only for .Net to .Net communication.

Full WCF built-in binding can be read here.

 
Leave a comment

Posted by on April 25, 2012 in General

 

Tags:

What is Unobtrusive JavaScript?

Basically: a way of writing JavaScript that:

  • Separate JavaScript code from markup language (HTML).
  • Avoid JavaScript programming that behaves inconsistently in different user agents.
  • Progressive enhancement / degrade gracefully to support user agents that may not support JavaScript functionality.

There is a lot more to unobtrusive JavaScript. All can be read here.

 
Leave a comment

Posted by on April 25, 2012 in General

 

Tags: ,

Database Audit with Change Data Capture (CDC) in SQL Server 2012

There are two feature in SQL Server 2008 (and above) to track data changes, Change Tracking (CT) and Change Data Capture (CDC).

Tracked Data: DML changes, DML type, columns, historical data (only in CDC).

Difference: CT doesn’t know the old and new value of the data being changed.

DML Operations:

  • 1 = delete
  • 2 = insert
  • 3 = update (before image)
  • 4 = update (after image)

Objects: The following are objects (system tables, stored procedures – sp, or functions – fn) used in CDC (Change Data Capture)

Object Description
sys.sp_cdc_enable_db Stored procedure to disable CDC in a specified database.
sys.sp_cdc_disable_db Stored procedure to disable CDC in a specified database.
sys.sp_cdc_enable_table Stored procedure to enable CDC in a specified table.
sys.sp_cdc_disable_table Stored procedure to disable CDC in a specified table.
cdc.fn_cdc_get_all_changes_<capture_instance> Function to return all changes that occurred for the specified interval.
cdc.fn_cdc_get_net_changes_<capture_instance> Function to return one change per modified source table row.
sys.fn_cdc_get_max_lsn Function to return the maximum log sequence number (LSN) from the start_lsn column in the cdc.lsn_time_mapping system table.
sys.fn_cdc_get_min_lsn Function to returns the start_lsn column value for the specified capture instance from the cdc.change_tables system table.
cdc.lsn_time_mapping System table to store one row for each transaction having rows in a change table. This table contains commit time for each data changes.
cdc.change_tables System table to store one row for each change table in the database.
sys.dm_tran_database_transactions System table to store information about transactions at the database level.

Source: MSDN

 
1 Comment

Posted by on April 23, 2012 in General

 

Tags:

Dependency Injection Structure

Highly coupled dependency

public class VerySimpleStockTraderImpl : IAutomatedStockTrader
{
    private IStockAnalysisService analysisService = new StockAnalysisServiceImpl();
    private IOnlineBrokerageService brokerageService = new NewYorkStockExchangeBrokerageServiceImpl();

    public void executeTrades()
    {
        for (String stockSymbol : brokerageService.getStockSymbols())
        {
             double askPrice = brokerageService.getAskingPrice(stockSymbol);
             double estimatedValue = analysisService.getEstimatedValue(stockSymbol);
             if (askPrice < estimatedValue)
             {
                  brokerageService.putBuyOrder(stockSymbol, 100, askPrice);
             }
        }
    }
}

public class MyApplication
{
    public static void main(String[] args)
    {
        IAutomatedStockTrader stockTrader = new VerySimpleStockTraderImpl();
        stockTrader.executeTrades();
    }
}

Manually injected dependency

public class VerySimpleStockTraderImpl : IAutomatedStockTrader
{
    private IStockAnalysisService analysisService;
    private IOnlineBrokerageService brokerageService;

    public VerySimpleStockTraderImpl(IStockAnalysisService analysisService, IOnlineBrokerageService brokerageService)
    {
        this.analysisService = analysisService;
        this.brokerageService = brokerageService;
    }

    public void executeTrades()
    {
        ....
    }
}

public class MyApplication
{
    public static void main(String[] args)
    {
        IStockAnalysisService analysisService = new StockAnalysisServiceImpl();
        IOnlineBrokerageService brokerageService = new NewYorkStockExchangeBrokerageServiceImpl();

        IAutomatedStockTrader stockTrader = new VerySimpleStockTraderImpl(analysisService, brokerageService);
        stockTrader.executeTrades();
    }
}

Automatically injected dependency

public class VerySimpleStockTraderImpl : IAutomatedStockTrader
{
    private IStockAnalysisService analysisService;
    private IOnlineBrokerageService brokerageService;

    public VerySimpleStockTraderImpl(IStockAnalysisService analysisService, IOnlineBrokerageService brokerageService)
    {
        this.analysisService = analysisService;
        this.brokerageService = brokerageService;
    }

    public void executeTrades()
    {
        ....
    }
}

public class MyApplication
{
    public static void main(String[] args)
    {
        IAutomatedStockTrader stockTrader = (IAutomatedStockTrader)DependencyManager.create(typeof(IAutomatedStockTrader));
        stockTrader.executeTrades();
    }
}

Source: Wikipedia

 
Leave a comment

Posted by on April 13, 2012 in References

 

Tags: ,

Service Oriented Architecture (SOA) Benefits

Many need justification to go with service oriented architecture. Below are just few SOA benefits, both business and technical:

  • Reducing integration expense
    Integration point could be time-consuming and expensive because of its maintenance. By building SOA and allowing others to access your services, you will shift integration to client’s side, omitting the needs for you to maintenance integration for your clients. Your clients, in return, has benefits of customizing integration point to their needs. Win-win situation.
  • Reducing hardware cost
    Because the nature of SOA is ad-hoc calls, which means services are called on-demand, it will consume resources only when necessary. This essentially reduce hardware costs.
  • Data exposure
    Data exposure could be good way if managed properly. Many companies have succeed through exposing their data. Case and point, Amazon.com and Facebook. By exposing and allowing others to use your data, you are creating dependency toward your platform. This basically means you can’t fail, because when you fail, others will follow in which it leads others to support your business nature.
  • One central source
    SOA when build properly could be one central source of all your data. This reduce needs of going different places to gather data because SOA is handling that and providing in the form of services for you. One central source is always good for it will save your time almost instantly.
  • Cross platform
    SOA doesn’t need specific platform to call, it’s cross-platform. Because SOA is built on top of today’s Internet standard, all platforms are allowed to access using these standards. No need to reinvent the wheel and one can focus on developing business model.
 
Leave a comment

Posted by on April 13, 2012 in General

 

Tags:

JavaScript Patterns Structure

Prototype Pattern

var Calculator = function(eq) {
    // Variables defined here.
    this.eqCtl = document.getElementById(eq);
};

Calculator.prototype = {
    // Functions defined here.
    add: function (x, y) {
        var val = x + y;
        this.eqCtl.innerHTML = val;
    }
}

Module Pattern

var Calculator = function(eq) {
    // Private variables.
    // Private functions.
    var eqCtl = document.getElementById(eq);

    return {
        // Public members
        add: function(x, y) {
            var val = x + y;
            eqCtl.innerHTML = val;
        }
    };
};

Revealing Prototype Pattern

var Calculator = function(eq) {
    // Variables defined here.
    this.eqCtl = document.getElementById(eq);
};

Calculator.prototype = function() {
    // Functions defined here.
    var add = function (x, y) {
        var val = x + y;
        this.eqCtl.innerHTML = val;
    };

    return {
        // Public members defined here.
        add : add
    };
}();

Revealing Module Pattern

var calculator = function(eq) {
    // Private variables.
    // Private functions.
    var eqCtl = document.getElementById(eq),
        doAdd = function(x, y) {
            var val = x + y;
            eqCtl.innerHTML = val;
        };

    return {
        // Public members
        add: doAdd
    };
}('eqCtl');
 
Leave a comment

Posted by on April 12, 2012 in References

 

Tags: ,

 
%d bloggers like this: