---
title: Warehouses and inventories examples
slug: commerce-connect-15/docs/warehouses-and-inventories-examples
docTags: 
createdAt: 2026-07-30T20:34:40.000Z
---

## Inventories

The following examples show how to work with inventories.

### Get an instance of IInventoryService

Use `IInventoryService.GetCacheSkippingInstance(IEnumerable<string>)` to get an instance of `IInventoryService` that skips the cache on reads.

```csharp
// Gets an instance of IInventoryService that will skip the cache on reads. 
public IInventoryService GetCacheSkippingInstance() 
  {  
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>(); 
    return inventoryService.GetCacheSkippingInstance(); 
  }
```

### Get a single inventory

Use `IInventoryService.Get(string, string)` to get a single inventory record for a catalog entry and a warehouse.

```csharp
// Gets a single inventory record. 
public InventoryRecord GetAnInventory() 
  { 
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>(); 
    return inventoryService.Get(); 
  }
```

### List inventories

Use `IInventoryService.List()` to list information for all inventories.

```csharp
// List all inventory records. 
public IEnumerable<InventoryRecord> ListAllInventories() 
  { 
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();  
    return inventoryService.List(); 
  }
```

Use `IInventoryService.QueryByEntry(IEnumerable<string>)` to list all inventories by entry code.

```csharp
// List all inventory records by entry code.
public IEnumerable<InventoryRecord> QueryInventoriesByEntry(IEnumerable<string> catalogEntryCodes)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByEntry(catalogEntryCodes);
  }
```

Use `IInventoryService.QueryByEntry(IEnumerable<string>, int, int, out int)` to list all inventories by entry code in a specified range.

```csharp
// List all inventory records by entry code in a specified range.
public IEnumerable<InventoryRecord> QueryInventoriesInRangeByEntry(IEnumerable<string> catalogEntryCodes, int offset, int count, out int totalCount)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByEntry(catalogEntryCodes, offset, count, out totalCount);
  }
```

Use `IInventoryService.QueryByWarehouse(IEnumerable<string>)` to list all inventories by warehouse code.

```csharp
// List all inventory records by warehouse codes.
public IEnumerable<InventoryRecord> QueryInventoriesByWarehouse(IEnumerable<string> warehouseCodes)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByWarehouse(warehouseCodes);
  }
```

Use `IInventoryService.QueryByWarehouse(IEnumerable<string>, int, int, out int)` to list all inventories by warehouse code in a specified range.

```csharp
// List all inventory records by warehouse code in specified range.
public IEnumerable<InventoryRecord> QueryInventoriesInRangeByWarehouse(IEnumerable<string> warehouseCodes, int offset, int count, out int totalCount)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByWarehouse(warehouseCodes, offset, count, out totalCount);
  }
```

Use `IInventoryService.QueryByPartialKey(IEnumerable<InventoryKey>)` to list all inventories matching an item in an inventory key.

```csharp
// List all inventory records matching an item in inventory key.
public IEnumerable<InventoryRecord> QueryInventoriesByPartialKey(IEnumerable<InventoryKey> partialKeys)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByPartialKey(partialKeys);
  }
```

Use `IInventoryService.QueryByPartialKey(IEnumerable<InventoryKey>, int, int, out int)` to list all inventories matching an item in an inventory key in a specified range.

```csharp
// List all inventory records matching an item in inventory key in specified range.
public IEnumerable<InventoryRecord> QueryInventoriesInRangeByPartialKey(IEnumerable<InventoryKey> partialKeys, int offset, int count, out int totalCount)
{
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.QueryByPartialKey(partialKeys, offset, count, out totalCount);
}
```

### Request a transactional inventory operation

Use `IInventoryService.Request(InventoryRequest request)` to request a transactional inventory operation.

```csharp
// Requests a transactional inventory operation.
public InventoryResponse RequestInventory(InventoryRequest request)
  {
    var inventoryService = ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Request(request);
  }
```

### Update inventories

Use `IInventoryService.Save(IEnumerable<InventoryRecord>)` to save specified inventory records. The method matches records on `InventoryRecord.CatalogEntryCode` or `InventoryRecord.WarehouseCode`, updating existing records and inserting new ones.

```csharp
// Saves the specified inventory records.
public void SaveInventories(IEnumerable<InventoryRecord> records)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Save(records);
  }
```

Use `IInventoryService.Insert(IEnumerable<InventoryRecord>)` to insert the specified inventory records. An exception is thrown if any records exist with matching `InventoryRecord.CatalogEntryCode` and `InventoryRecord.WarehouseCode` values.

```csharp
// Inserts the specified inventory records.
public void InsertInventories(IEnumerable<InventoryRecord> records)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Insert(records);
  }
```

Use `IInventoryService.Update(IEnumerable<InventoryRecord>)` to update the specified inventory records. Records are matched to existing records on `InventoryRecord.CatalogEntryCode` and `InventoryRecord.WarehouseCode` values. The method throws an exception for any records that do not match.

```csharp
// Updates the specified inventory records.
public void UpdateInventories(IEnumerable<InventoryRecord> records)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Update(records);
  }
```

Use `IInventoryService.Delete(IEnumerable<InventoryKey>)` to delete all specified inventory data.

```csharp
// Deletes all specified inventory data.
public void DeleteInventories(IEnumerable<InventoryKey> inventoryKeys)
  {
    if (inventoryKeys == null || inventoryKeys.Contains(null))
      {
        throw new ArgumentNullException(nameof(inventoryKeys));
      }
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Delete(inventoryKeys);
  }
```

Use `IInventoryService.Adjust(IEnumerable<InventoryChange>)` to increment or decrement matching values in the inventory provider. Elements of changes are matched to existing data by item and warehouse code, and each quantity in the `InventoryService.InventoryChange` is added to the corresponding quantity in the inventory provider.

```csharp
// Increments or decrements matching values in the inventory provider.
public void AdjustInventory(IEnumerable<InventoryChange> changes)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.Adjust(changes);
  }
```

Use `IInventoryService.DeleteByEntry(IEnumerable<string>)` to delete all inventory data for specified catalog entries.

```csharp
// Deletes all inventory data for the specified catalog entries.
public void DeleteInventoriesByEntry(IEnumerable<string> catalogEntryCodes)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.DeleteByEntry(catalogEntryCodes);
  }
```

Use `IInventoryService.DeleteByWarehouse(IEnumerable<string>)` to delete all inventory data for specified warehouses.

```csharp
// Deletes all inventory data for the specified warehouses.
public void DeleteInventoriesByWarehouse(IEnumerable<string> warehouseCode)
  {
    var inventoryService= ServiceLocator.Current.GetInstance<IInventoryService>();
    return inventoryService.DeleteByWarehouse(warehouseCode);
  }
```

## Warehouses

The following examples show how to list, get, save, and delete warehouses.

### List warehouses

Use `IWarehouseRepository.List()` to list information for all warehouses.

```csharp
// Get list Warehouse
public IEnumerable<IWarehouse> ListAllWarehouses()
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.List();
}
```

### Get a warehouse

Use `IWarehouseRepository.Get()` to get a single warehouse. Get a warehouse by `ID` or `Code`.

```csharp
// Get a specific Warehouse by ID
public IWarehouse GetWarehouse(int warehouseId)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.Get(warehouseId);
  }
```

```csharp
// Get a specific Warehouse by Code
public IWarehouse GetWarehouse(string warehouseCode)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    return warehouseRepository.Get(warehouseCode);
  }
```

### Save a warehouse

Use `IWarehouseRepository.Save(IWarehouse)` to add/edit and save a warehouse.

:::hint{type="info"}
**Note**
The `IWarehouse` object returned from `Get` and `List` methods above `IWarehouseRepository` is read-only. To edit it, you must make a writable clone object then update it, as is.
:::

```csharp
public void CreateNewWarehouse()
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    var warehouse = new Warehouse
    {
        Code = "NY",
        Name = "New York store",
        IsActive = true,
        IsPrimary = false,
        IsFulfillmentCenter = false,
        IsPickupLocation = true,
        IsDeliveryLocation = true,
        ContactInformation = new WarehouseContactInformation
        {
            FirstName = "First Name",
            LastName = "Last Name",
            Line1 = "Address Line 1",
            Line2 = "Address Line 2",
            City = "City",
            State = "State",
            CountryCode = "Country Code",
            PostalCode = "Postal Code",
            RegionCode = "Region Code",
            DaytimePhoneNumber = "Daytime Phone Number",
            EveningPhoneNumber = "Evening Phone Number",
            FaxNumber = "Fax Number",
            Email = "Email"
        }
    };
    warehouseRepository.Save(warehouse);
}

public void UpdateWarehouse(string warehouseCode)
{
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    var warehouse = warehouseRepository.Get(warehouseCode); // It's a read-only object.
    var writableCloneWarehouse = new Warehouse(warehouse); // Create a writable clone before updating.
    writableCloneWarehouse.IsPickupLocation = true;
    warehouseRepository.Service.Save(writableCloneWarehouse);
}
```

:::hint{type="info"}
**Note**
You can improve the `UpdateWarehouse` section of the above code like this:
:::

```csharp
public void UpdateWarehouse(string warehouseCode)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    var warehouse = warehouseRepository.Get(warehouseCode); // It's a read-only object
    var writableCloneWarehouse = warehouse.CreateWritableClone();
    writableCloneWarehouse.IsPickupLocation = true;
    warehouseRepository.Service.Save(writableCloneWarehouse);
  }
```

### Delete a warehouse

Use `IWarehouseRepository.Delete(int)` to delete a warehouse.

```csharp
public void DeleteWarehouse(int warehouseId)
  {
    var warehouseRepository = ServiceLocator.Current.GetInstance<IWarehouseRepository>();
    warehouseRepository.Delete(warehouseId);
  }
```
