Header

Tuesday, October 25, 2011

Visualforce Component IDs in JQuery

I bet most of you are using css classes in your vf components in order to select them using JQuery. I believe this the easiest workaround but not really that efficient especially if you have a number of vf components in your page and most of them should be using similar css classes. You are not able to use JQuery with the vf component's ID since it adds additional data to the ID you specified. The additional data denotes the component's position in the vf page structure. There is an easy way to refer to your vf components IDs without doing much code, however, just make sure that you don't have duplicate IDs or else this approach will fail. You may refer to your specific visualforce component by using this JQuery expression: $('[id$=YourVFComponentID]'). This will search all the IDs of the page components which contains the specified text after the id$.

Here's an example:

<apex:page>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>

<script>
    $(function()
    {
        $('[id$=itDate]').datepicker();  
    });
</script>

    <apex:form >
        <apex:pageblock>
            <apex:pageBlockSection columns="2">
                <apex:pageBlockSectionItem >
                    <apex:outputlabel value="Date:"/>
                    <apex:inputText id="itDate"/>
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
        </apex:pageblock>
    </apex:form>

</apex:page>     

Thursday, October 20, 2011

Entity Framework Tidbit 3

At the beginning of my previous project, I was sending entity framework objects over the web service. After some time, I realized that it wasn't a good implementation since you had to reference the entity framework objects in the web service. In addition, the performance isn't great but good enough considering the convenience it brings to us developers. To address these issues, I decided to use business objects with my current implementation. This will remove the entity framework object references in the web services because I will sending business objects instead. The entity framework will only be present in my repository class which contains all the methods to the data layer of the application. More so, new data contracts are needed for every business object I decide to send in the web service. Here's some sample code to illustrate this implementation:

Business Object

namespace EntityDTO
{
    [DataContract(Namespace = "http://sb.services.betsson.com")]
    public class Customer
    {
        [DataMember]
        public int CustId {get; set;}
       
        [DataMember]
        public string CustName {get; set;}
    }
}

Repository

namespace EntityRepository
{
    public class CustomerRepo
    {
        private SBCustomerEntities _db;    

        public CustomerRepo()
        {
            _db = new SBCustomerEntities();
            _db.ContextOptions.LazyLoadingEnabled = false;
        }
       
        public List<Customer> GetAllCustomers()
        {
            List<Customer> lCustomers = new List<Customer>();
       
            var customers = from c in _db.tbl_customer select c;
           
            lCustomers.addRange(from customer in customers select new Customer{CustId=customer.customerID, CustName=customer.customerName});
           
            return lCustomers;
        }
    }
}

Web Service Interface

namespace Services
{
    public interface ISampleService
    {
        [OperationContract]
        List<Customer> GetCustomers();           
    }
}


Web Service Implementation

namespace Services
{
    public sealed class SampleService : ISampleService
    {
        List<Customer> GetCustomers()
        {
            CustomerRepo customerRepo = new CustomerRepo();
            var customers = customerRepo.GetAllCustomers();
           
            return customers;
        }
    }
}

Friday, October 7, 2011

Entity Framework Tidbit 2

I'll be discussing about custom associations in entity framework. You will need this when you want to create a link between two tables which are related but no relation constraints defined. Usually, this happens for tables whose relationship to one another is not really that significant to the system. The relationship is only established on a per need basis only. To create this relationships in entity framework, you need to add a custom association between the tables.  I consider it custom since, when you add tables in the entity model which have relation constraints, the association between the tables is automatically created. Here's the steps on how to do this:

1. Right click at the table and select new association.


2. Uncheck the "Add foreign key properties to.."


3. Click on the line created linking the two tables you have associated and go to properties. Then, set the referential constraints for the two tables.








After that, you have created a custom association between two tables! :)

Saturday, September 3, 2011

Entity Framework Tidbit 1

I'm not going to discuss on how to use the entity framework since there are a number of tutorials available in the web. I'll just share a realization I had while trying to do a sample application using the entity framework. In most examples I've read, I noticed that the entity objects are exposed in all projects where they are used in the solution. For simple applications, this kind of implementation is somehow acceptable. However, in enterprise-level applications, this won't do since there are a lot of issues that come into play such as security, performance and etc. Since my current is somehow an enterprise application, I was instructed to find a workaround so that I won't be exposing the entity objects to the web service projects. I read a number of articles before I came across the solution for this problem. I found out that referencing the auto-generated entity object classes in another project and adding a reference to it in the web service project is the solution for the issue. With this, you won't have to reference the entity model project in the web service project but you are still able to use the data from the entity object which were persisted to the entity classes. I'll be starting to apply the entity framework to the actual application I'm working on next week. Hopefully, everything will go smoothly and I'll find something interesting to share which would help those who are new in using the entity framework.

Monday, August 22, 2011

Entity Framework Introduction


I'm starting my new project today and I was informed by our technical architect that we will use the Microsoft Entity Framework. I was so happy when I learned that since I was so depressed, frustrated, and stressed working with Datasets on my previous project. I had almost a month-long of hell dealing with Datasets. I just needed to update fields in several tables but I had to do a lot of stuff to make it happen. Not only that, I encountered a number of ambiguous errors which I can't figure out the causes. I ended up not doing the update on the other tables and creating a new stored procedure. I'm sorry that I keep on ranting about this, I guess I'm not really over with that experience yet.

Anyway, I'm going to discuss a bit about the Entity Framework. The entity framework is Object-Relational Mapping (ORM) tool created by Microsoft to compete with third parties such as NHibernate which have been popular  for the past few years. What is a Object-Relational Mapping? As a software developer, I view it as a method of linking the object classes I created to their corresponding database tables. Usually, I do this through code. Like I execute a SQL Select statement then loop through the result and map them to its approriate property in the object class. As a result, I get a list of objects populated with database data. With the use of an ORM tool, my life is a lot simpler since I don't need to create code to retrieve and manipulate data from the database. I just need to call the appropriate method for the database operation I need to implement, the tool abstracts how this happens. It's really powerful and it can increase one's productivity significantly. However, I believe it has its own share of imperfections which is common in every technology available right now. I'm excited to start developing using this. Hopefully, in a couple of days, I will post an update on how to use the Entity framework and a few insights on its pros and cons.

Sunday, August 14, 2011

Visualforce Page Redirection

I needed a button in a standard page of a custom object to do some logic when pressed. Initially, I found out that this could be done through a Global Apex Class. So, I started developing using this approach but I had a hard time passing some parameters to the class from the page. I managed to find some workarounds to address the issue, however, it seemed that I was doing a lot just to make it work. With this, I decided to find another alternative to do the functionality. I realized that I could redirect to a Visualforce page using the button and add method in the controller to do the logic. This time, my problem is that the page redirection does not work after the method is executed in the constructor. After some researching on the cause of this, it turned out that the constructor does not know what to do with the pagereference object it got from the method. As a result, no page redirection happens. To address this issue, you need to call the method in the action parameter of the visualforce page instead of the constructor.

Here's an example:

<apex:page controller="SampleController" action="{!sampleLogicMethodWithRedirection}">


Friday, August 12, 2011

First Stored Procedure

Today, I created my first stored procedure. I'm have mixed emotions about it. I was never a fan of stored procedures throughout my career, I didn't like the idea that business logic will be housed in the database layer. Here are some of my reasons for that:

a). It is hard to debug SP code. I was working with a requirement wherein I need to add an new field to be updated in a table. This task required me to make some modifications to a backend call which calls an SP to update data in the database. Initially, I thought I would just need to add a new field in the SP and update its mapping in the backend. Alas, it didn't work! I encountered a casting error with the SP's parameter against the dataset. I investigated the cause of the error using a variety of methods but I just get the same error message which does not help at all. This was so frustrating since I just wanted to update a single field and how hard could that be?!?! I really, really wanted to create a new implementation on updating the field but I couldn't justify why I need to do so. After two days of investigation, the team decided to create new method to update the field but sad to say it still uses an SP.

b). Hard to maintain in different environments (PT, QA and Prod). It is a pain that you need to update all instances of the SP in different environments every time you have changes. Unlike if you do the business logic in code, you just need to update it in a single place and deploy to different environments. With this, it is less likely you will break something in the other environments compared  to doing an update in all affected environments.

c). Makes the application slower. This is because of the additional layer it needs to go through before the result is obtained by the application. Usually, this is evident when the result from the database layer processing needs to be processed in the application's backend.

However, in spite of these, I guess I'm glad that I created a new SP since it signified that the sprint I was working on for almost a month is almost over! I can't wait to work on something different.