Implement Dependency Injection

Posts   
 
    
chungpn
User
Posts: 39
Joined: 23-Mar-2011
# Posted on: 11-May-2011 07:41:43   

I use this class to add DI to the Example_NorthwindCS1_110211 project:

namespace Northwind.DAL { [DependencyInjectionInfo(typeof (EntityBase), "ConcurrencyPredicateFactoryToUse", ContextType = DependencyInjectionContextType.Singleton)] [Serializable] public class GeneralConcurrencyPredicateFactory: IConcurrencyPredicateFactory { private static GeneralConcurrencyPredicateFactory concurrencyPredicateFactory;

public static GeneralConcurrencyPredicateFactory ConcurrencyPredicateFactory
{
  get
  {
    if (concurrencyPredicateFactory == null)
      concurrencyPredicateFactory = new GeneralConcurrencyPredicateFactory();
    return concurrencyPredicateFactory;
  }
}



public IPredicateExpression CreatePredicate(ConcurrencyPredicateType predicateTypeToCreate,
                                            object containingEntity)
{
  IPredicateExpression toReturn = new PredicateExpression();
  var entity = (IEntity)containingEntity;
  switch (predicateTypeToCreate)
  {
    case ConcurrencyPredicateType.Save:
      // only for updates
      foreach (EntityField field in entity.Fields)
        if (field.IsChanged)
          toReturn.Add(field == field.DbValue);

      break;
  }
  return toReturn;
}

} }

Then , I the EntityFactories.cs, I add the line of code: toReturn.ConcurrencyPredicateFactoryToUse = GeneralConcurrencyPredicateFactory.ConcurrencyPredicateFactory; like this: [Serializable] public partial class CustomerEntityFactory : EntityFactoryBase { /// <summary>CTor</summary> public CustomerEntityFactory() : base("CustomerEntity", Northwind.DAL.EntityType.CustomerEntity) { }

    /// <summary>Creates a new, empty CustomerEntity object.</summary>
    /// <returns>A new, empty CustomerEntity object.</returns>
    public override IEntity Create() {
        IEntity toReturn = new CustomerEntity();

        // __LLBLGENPRO_USER_CODE_REGION_START CreateNewCustomer

toReturn.ConcurrencyPredicateFactoryToUse = GeneralConcurrencyPredicateFactory.ConcurrencyPredicateFactory;

        // __LLBLGENPRO_USER_CODE_REGION_END

        return toReturn;
    }

When Use CustomerManager.cs to update a customer, evrything is OK, it mean the generated sql had the AND operator in the changed field, but when I use a new form to fetch a customer then update it, the generated sql not include the AND operator on changed field, the code like this

namespace Northwind.GUI { public partial class Form2 : Form { private CustomerEntity _currentCustomer; public Form2() { InitializeComponent();

        _currentCustomer = new CustomerEntity();
        _currentCustomer.FetchUsingPK("ALFKI");
        textBox1.DataBindings.Add("Text", _currentCustomer, "CompanyName");
        textBox2.DataBindings.Add("Text", _currentCustomer, "ContactName");

    }


    private void button1_Click(object sender, EventArgs e)
    {

        _currentCustomer.ContactName = textBox2.Text;
        _currentCustomer.CompanyName = textBox1.Text;
        _currentCustomer.Save();
    }
}

}

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-May-2011 10:01:00   

Do you use the CustomerEntityFactory in the customerManager.cs ?

If, yes then DI is not kicking in the WebForm. So my next question would be, how do you set DI in the application config file?

chungpn
User
Posts: 39
Joined: 23-Mar-2011
# Posted on: 11-May-2011 10:24:52   

No, I dont see any CustomerEntityFactory in the customerManager.cs and I dont' see any DI setting in the application config file

and by the way , can you show me why DI not working in webform, how to fix it to work? many thanh you. I download the example from the Additional downloads for LLBLGen Pro link: Northwind Example 1 (C#) http://www.llblgen.com/Pages/ListAdditionalDownloads.aspx

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 11-May-2011 10:58:24