How to use translate class in Adapter Class ?

Topics: General Discussion Forum, July and December Releases Forum
Feb 14, 2008 at 6:20 AM
Edited Feb 14, 2008 at 6:30 AM
Hi,

I have a translate class, and adpter class..
i wanna insert some datas into the database, like name,address,phone and status(what ever may be).
so i am getting all these from my client and from the client side its calling the service.

Here i have to translate these data contract into the bussiness entites.
how i have to code manually in the adpater class inorder to translate ?

I have a class called insert in my bussiness logic.
after inserting these datas, i have to return a value..

Nimesh
Feb 14, 2008 at 1:09 PM
Howdy Nimesh,
What happens if you right-click yourServiceImplementation? If your guidance packages are installed and setup correctly, you should get a menu option of Create Translator. This is a Wizard that will help you map your DC into your BE. It is a simple translation, you don't HAVE to use the wizard, but it helps. I keep the translator in the ServiceImplementation project, since that project already has the references to the BE and DC projects, so no additional references need to be made.
A sample:
Say you have a function to add a product:
public override AddProductReply AddProduct(AddProductRequest request)
You can simply call the business logic to do your work, passing in the translated object:
AddProductReply reply = new AddProductReply();
reply.NewProductID = ProductBL.ProductInsert(*Translator.ProductDCBE(request.productToAdd)*);_
return reply;

Really that is all there is to the service. The grunt work is done by the business layer and data access layer.

Hope that helps
Developer
Feb 14, 2008 at 2:54 PM
You can find these all many other topics very well illustrated in the ASMX and WCF Reference Implementation solutions included in WSSFv2 Dec2006 version here: https://www.codeplex.com/Release/ProjectReleases.aspx?ProjectName=servicefactory&ReleaseId=9600
Feb 25, 2008 at 8:49 AM
HI Cf,
Once again i would like to get clarification....
1--->
Data Contract can be the input parameters right....

Then wot will be the Message Contracts. Here in my project, I have a SC called Customer, with Register Customer, Delete Customer, Gett all Coustomer..like this. i have to use MC here, i created two MC,namely RegisterCustomerRequest, and RegisterCustomerResponse.

What all can be the Member's of the this MC's ? Suppose, Register Service Operation deals witht he registration of Customers.
If it register sucess then it must return a bool value(True/False).

2---->
Can you Please just show me an example how does the adapter class works....


Nimesh

Developer
Feb 25, 2008 at 1:18 PM
Here you have an example of an adapter class that uses an input translator (DC->BE) and then we call the BL class, and an output translator (BE->DC) that do the conversion back from the BL class to the DC and MC types.

public class CustomerFinderAdapter : ICustomerFinder
{
    public GlobalBank.GlobalBranchServices.DataTypes.FindCustomerResponse FindCustomer(GlobalBank.GlobalBranchServices.DataTypes.CustomerSearchCriteria searchCriteria)
    {
        // argument validation
        if (searchCriteria == null)
        {
            throw new ArgumentNullException("searchCriteria");
        }
 
        // Perform validation on fields
        ValidationContext context = new ValidationContext();
        ValidateCustomerSearchCriteria(context, searchCriteria);
        context.ThrowIfIsFailed();
 
        TranslateBetweenCustomerSearchCriteriaAndCustomerSearchCriteria inputTranslator =
            new TranslateBetweenCustomerSearchCriteriaAndCustomerSearchCriteria();
 
        TranslateBetweenListOfCustomersAndFindCustomerResponse outputTranslator =
            new TranslateBetweenListOfCustomersAndFindCustomerResponse();
 
        FindCustomerAction action = new FindCustomerAction();
 
        GlobalBank.GlobalBranchServices.BusinessEntities.CustomerSearchCriteria input =
            inputTranslator.TranslateDataContractCustomerCriteriaToBusinessCustomerCriteria(searchCriteria);
 
        List<GlobalBank.GlobalBranchServices.BusinessEntities.Customer> result = action.Execute(input);
 
        return outputTranslator.TranslateListOfCustomersToFindCustomerResponse(result);
    }

Now this is the input translator class:

public class TranslateBetweenCustomerSearchCriteriaAndCustomerSearchCriteria
{
    public BusinessCustomerCriteria TranslateDataContractCustomerCriteriaToBusinessCustomerCriteria(DataContractCustomerCriteria from)
    {
        if (from == null)
            throw new ArgumentNullException("from");
        
        BusinessCustomerCriteria to = new BusinessCustomerCriteria();
 
        AddIfNotNull(to, from.FirstName, CustomerSearchField.FirstName);
        AddIfNotNull(to, from.LastName, CustomerSearchField.LastName);
        AddIfNotNull(to, from.MiddleInitial, CustomerSearchField.MiddleInitial);
        AddIfNotNull(to, from.SSNumber, CustomerSearchField.SSNumber);
 
        return to;
    }
 
    void AddIfNotNull(BusinessCustomerCriteria to, string input, CustomerSearchField output)
    {
        if (!string.IsNullOrEmpty(input))
        {
            to.Add(output, input);
        }
    }
}

And this is the BL class (action):

    public class FindCustomerAction
    {
        public List<Customer> Execute(CustomerSearchCriteria criteria)
        {
            CustomerRepository repository = new CustomerRepository("Global Bank CRM");
            return repository.FindCustomers(criteria);
        }
    }

That calls the CustomerRepository that handles the Data Access stuff where you have something like:

public class CustomerRepository
{
    private string databaseName;
    private CustomerFactory domainObjectFactory;
 
    public CustomerRepository(string databaseName)
    {
        this.databaseName = databaseName;
        domainObjectFactory = new CustomerFactory();
    }
 
    public List<Customer> FindCustomers(CustomerSearchCriteria criteria)
    {
        List<Customer> results = new List<Customer>();
        CustomerSelectionFactory selectionFactory = new CustomerSelectionFactory(criteria);
 
        Database db = DatabaseFactory.CreateDatabase(databaseName);
        using (DbCommand command = selectionFactory.GenerateSelector(db))
        {
            using (IDataReader rdr = db.ExecuteReader(command))
            {
                while (rdr.Read())
                {
                    results.Add(domainObjectFactory.Construct(rdr));
                }
            }
        }
        return results;
    }
    ...
Feb 26, 2008 at 8:27 AM
Edited Feb 26, 2008 at 11:00 AM


ccullum wrote:
Howdy Nimesh,
What happens if you right-click yourServiceImplementation? If your guidance packages are installed and setup correctly, you should get a menu option of Create Translator. This is a Wizard that will help you map your DC into your BE. It is a simple translation, you don't HAVE to use the wizard, but it helps. I keep the translator in the ServiceImplementation project, since that project already has the references to the BE and DC projects, so no additional references need to be made.
A sample:
Say you have a function to add a product:
public override AddProductReply AddProduct(AddProductRequest request)
You can simply call the business logic to do your work, passing in the translated object:
AddProductReply reply = new AddProductReply();
reply.NewProductID = ProductBL.ProductInsert(*Translator.ProductDCBE(request.productToAdd)*);_
return reply;

Really that is all there is to the service. The grunt work is done by the business layer and data access layer.

Hope that helps



HI,

Here the Add Method in the Repository class's return value is null, in that case is it possible to change that return value inside the repository class..

while i am usign the service translator receipe, i am not able to map the DC and BE.
i am having 4 members in the DC as well as BE.
Here i am able to add mapping between the 2 members, but the 2 members in the BE is showing the :

Date(Nullable `1)
status(Nullable `1)

do you know what is the problem with this ...?

Nimesh
Developer
Feb 27, 2008 at 12:23 PM
That is because you are using a Nullable<T> type where T is the type you specified as nullable. You can also define the same in your DC setting IsNullable property as true. Then simple map both properties and your're done.