Inject same interface in some class but different interface implementation


Class example

interface IValidator
{
    void Validate(object obj);
}

public class ValidatorA : IValidator
{
    public void Validate(object obj) { }
}

public class ValidatorB : IValidator
{
    public void Validate(object obj) { }
}

interface IClassA { }
interface IClassB { }

public class MyBaseClass
{
    protected IValidator validator;

    public void Validate()
    {
        validator.Validate(this);
    }
}

public class ClassA : MyBaseClass, IClassA
{
    //problem: validator should ValidatorA
    public ClassA(IValidator validator) { }
}

public class ClassB : MyBaseClass, IClassB
{
    //problem: validator should ValidatorB
    public ClassB(IValidator validator) { }
}

public class OtherClass
{
    public OtherClass(IClassA a, IClassB b) { }
}

//on Main
var oc = container.Resolve<OtherClass>();

DI Solutions

var container = new WindsorContainer();

container.Register(Component.For<IClassA>().ImplementedBy<ClassA>()
    .DependsOn(ServiceOverride.ForKey<IValidator>().Eq("ValidatorA")));
container.Register(Component.For<IClassB>().ImplementedBy<ClassB>()
    .DependsOn(ServiceOverride.ForKey<IValidator>().Eq("ValidatorB")));

container.Register(Component.For<IValidator>().ImplementedBy<ValidatorA>()
    .Named("ValidatorA"));
container.Register(Component.For<IValidator>().ImplementedBy<ValidatorB>()
    .Named("ValidatorB"));

container.Register(Component.For<OtherClass>().ImplementedBy<OtherClass>());

var oc = container.Resolve<OtherClass>();

Reference

First and Second Level Cache


Baik first level maupun second level itu sangat dianjurkan buat dipake diproduction.

First level cache selalu ada, gak bisa dimatiin. Itu gak berhubungan dengan koneksi ke db. First-level cache ini merujuk ke object yg ter-attach ke unit-of-work tertentu (nhibernate-session). Jadi selama lu load sebuah row dari session yang sama, lu bakal dibalikin reference ke object yang sama, karna ini adalah object yang ditrack oleh session tersebut selama unit-of-work, sampe entity tersebut dievict ato session tersebut didispose.
First level cache ini fungsinya bukan buat performance reason (walopun memang tetep improve performance), melainkan ini behavior yang dibutuhkan buat menjaga unit-of-work semantics, yang dah inherent dari cara kerja nhibernate, gak bisa dimatiin.

Second-level cache adalah across sessions. Ini sifatnya optional, tapi sangat penting dipake di production. Gunanya purely buat performance reason, ngurangin hit ke database, dengan memanfaatkan high-performance cache.
Yang gak boleh dipake adalah default providernya, yaitu menggunakan in-memory cache, yang bakal ngaco kalo dipake di production, karna cachenya gak terdistribute dengan benar antar instances di dalem cluster/web-farm.
Mesti diganti dengan production-grade cache provider, typically yg menggunakan DHT. Memcached misalnya.

~ hendryluk

JTDS


Interface jTDS Implementation
java.sql.Driver net.sourceforge.jtds.jdbc.Driver
javax.sql.DataSource net.sourceforge.jtds.jdbcx.JtdsDataSource
javax.sql.ConnectionPoolDataSource net.sourceforge.jtds.jdbcx.JtdsDataSource
javax.sql.XADataSource net.sourceforge.jtds.jdbcx.JtdsDataSource
javax.naming.spi.ObjectFactory net.sourceforge.jtds.jdbcx.JtdsObjectFactory

http://jtds.sourceforge.net/faq.html

Interface jTDS Implementation
java.sql.Driver net.sourceforge.jtds.jdbc.Driver
javax.sql.DataSource net.sourceforge.jtds.jdbcx.JtdsDataSource
javax.sql.ConnectionPoolDataSource net.sourceforge.jtds.jdbcx.JtdsDataSource
javax.sql.XADataSource net.sourceforge.jtds.jdbcx.JtdsDataSource
javax.naming.spi.ObjectFactory net.sourceforge.jtds.jdbcx.JtdsObjectFactory

configuring application application.xml


Application.xml is the global configuration for JavaEE.

if you use JavaEE version you can use:

<?xml version="1.0" encoding="UTF-8"?>
<application xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="5"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/application_5.xsd">

<module>
  <java>test-client.jar</java>
</module>

<module>
  <ejb>test-ejb.jar</ejb>
</module>

<module>
  <web>
    <web-uri>test.war</web-uri>
    <context-root>test</context-root>
  </web>
</module>

<library-directory>lib</library-directory>
</application>

If you use Java2EE you can use

<?xml version="1.0" encoding="UTF-8"?>
<application id="Application_ID" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
	<display-name>
	WarTestEAR</display-name>
	<module id="WebModule_1218106372121">
		<web>
			<web-uri>WarTest.war</web-uri>
			<context-root>WarTest</context-root>
		</web>
	</module>
</application>
<?xml version=”1.0″ encoding=”UTF-8″?>
<application id=”Application_ID” version=”1.4″ xmlns=”http://java.sun.com/xml/ns/j2ee&#8221; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd”&gt;
<display-name>
WarTestEAR</display-name>
<module id=”WebModule_1218106372121″>
<web>
<web-uri>WarTest.war</web-uri>
<context-root>WarTest</context-root>
</web>
</module>
</application>