tags:

views:

40

answers:

1

Hello, My app has quite a lot of entities with relationships. I have a scenario in which the entity should join all the contacts records (One to One). another scenario where I need only limited record without all the joins.

For example, a "service call". This entity contains a number of one-to-one relationships: supplier, customer .. I do not always need access to all information (supplier, customer). Sometimes only the specific information.

So I thought the case where I have only the limited record retrieval I will write a separate function without the joins ..

So I found myself almost duplicates all of my retrieval functions. One, with all the joins (GetServiceCalls()). another without joins (GetServiceCallsLimited()).

This way looks me as wrong. Are there more effective ways?

-I know LazyList but it is one-to-many relationships..

Here is some code ex:

This is the basic function

    public IQueryable<Product> GetBasicProducts()
    {
        return from p in _db.Products
               select new Product
               {
                   ProductID = p.ProductID,
                   IsActive = p.IsActive,
                   Comment = p.Comment,
                   ShortDescription = p.ShortDescription,
                   Sponsorship = p.Sponsorship,
                   Link = p.Link,
                   Makat = p.Makat,
                   Price = p.Price,
                   Cost = p.Cost,
                   Name = p.ProductName,
                   Description = p.Description,
                   StockAmount = p.StockAmount,
                   CategoryID = p.CategoryID,
                   ManufacturerID = p.ManufacturerID,
                   ModifiedDate = p.ModifiedDate,
                   WeightUnitMeasureCode = p.WeightUnitMeasureCode,
                   SizeUnitMeasureCode = p.SizeUnitMeasureCode
               };
    }

That is the full relationships version

    public IQueryable<Product> GetProducts()
    {
        return from p in _db.Products
               join category in GetCategoriesNoChilds() on p.CategoryID equals        category.CategoryID
               join manufacturer in GetManufacturersNoChilds() on p.ManufacturerID equals manufacturer.ManufacturerID
               into manufacturers
               from manufacturer in manufacturers.DefaultIfEmpty()
               join weightUnit in this.GetUnitMeasures() on p.WeightUnitMeasureCode equals weightUnit.UnitMeasureCode
               into weightUnit from wUnit in weightUnit.DefaultIfEmpty()
               join sizeUnit in this.GetUnitMeasures() on p.SizeUnitMeasureCode equals sizeUnit.UnitMeasureCode
               into sizeUnit
               from sUnit in sizeUnit.DefaultIfEmpty()
               select new Product
               {
                   ProductID = p.ProductID,
                   IsActive = p.IsActive,
                   Comment = p.Comment,
                   ShortDescription = p.ShortDescription,
                   Sponsorship = p.Sponsorship,
                   Link = p.Link,
                   Makat = p.Makat,
                   Price = p.Price,
                   Cost = p.Cost,
                   Name = p.ProductName,
                   Description = p.Description,
                   StockAmount = p.StockAmount,
                   ModifiedDate = p.ModifiedDate,
                   Category = category,
                   CategoryID = p.CategoryID,
                   ManufacturerID = p.ManufacturerID,
                   Manufacturer = manufacturer,
                   Weight = p.Weight,
                   Size = p.Size,
                   WeightUnitMeasure = wUnit,
                   SizeUnitMeasure = sUnit,
                   WeightUnitMeasureCode = p.WeightUnitMeasureCode,
                   SizeUnitMeasureCode = p.SizeUnitMeasureCode
               };
    }

The Q is there is some way to simplifed this? (Above i talked about service call and the code is about products. sory about that. but it is same idea)

Thanks

A: 

You should split up your methods in smaller parts so you can reuse them within other methods. This will assist reuse of your methods.

Tony
Do you have an example? It's a little tricky to create modular retrieving data. Suppose you have an service call object that contains an customer object. Assuming I understand your answer you offer one function pull out only the details of the call without the customer's information. based on this function build another function only to connect the customer information. Therein lies the problem! How can pinpoint only update the client object within the call obj? This requires a new mapping for the call object.
ari
In your one function return the information that it gathered and then use that in the call obj.
Tony
@Tony. I just edit the question to clear up the q. Can you please take a look? thanks
ari