Beispiel #1
0
        public void Add(ILsoCustomer item)
        {
            // create the customer row and update it using the passed in value
            var customer = new Customer();

            /* Note: Code generally relies on Linq to create the associated NewCustomerOption,
             *       but there are two cases that need to be handled
             * First, many customers exist that do not have a NewCustomerOption yet. That case is handled in Customer.cs
             * Second, a completely new customer. That case is handled below.
             */
            customer.NewCustomerOptions = new NewCustomerOption();
            var actualitem = (LsoCustomer)item; // We need access to the underlying implementation

            actualitem.CreatedFrom = customer;  // This empty customer will get updated in the factory below

            // set defaults if those fields are not already set
            new LsoCustomerNewDefaults().SetDefaults(actualitem);

            // perform copy to LINQ to SQL class
            LsoCustomerFactory.Update(actualitem);

            // linq to sql is ready to push to DB, but we need to test that first
            _Db.Customers.InsertOnSubmit(customer);

            // check to see if this object will fit into the DB columns
            try
            {
                _Db.TestSubmitChanges();
            }
            catch (InvalidOperationException ex)
            {
                // discard the changes
                _Db.DiscardInsertsAndDeletes();
                _Db.SubmitChanges();

                // rethrow the exception for the benefit of our caller);
                throw;
            }

            // If we made it to here, we can proceed safely
            // Get a new customer ID from the DB
            int?newCustId = 0;

            _Db.sp_GetCustID(ref newCustId);

            if (!newCustId.HasValue || newCustId == 0)
            {
                // discard the changes
                _Db.DiscardInsertsAndDeletes();
                _Db.SubmitChanges();
                throw new CannotCreateNewIdException("LsoCustomerRepository.Add: sp_GetCustID returned null or zero for the next customer ID");
            }

            // populate the customerID now that we have it and are clear to save in the DB
            item.CustID = newCustId.Value;
            actualitem.CreatedFrom.CustID = newCustId.Value;

            // flush DB
            _Db.SubmitChanges();
        }
Beispiel #2
0
 public void Update(ILsoCustomer item)
 {
     // TODO: make sure subsequent code additions do not change this
     // Although submitchanges turns in all changes, since we don't touch the
     // LINQ to SQL objects directly until we update them here, we should
     // effectively be updating one object at a time as the repository interface
     // specifies
     item.Update();
     _Db.SubmitChanges();
 }
        // TODO: This needs to probably be an entire class, the names in the DB are far too many
        // TODO: formats
        private static string GetSafeName(ILsoCustomer cust)
        {
            var retval = "";

            if (cust == null || String.IsNullOrEmpty(cust.CustContactName))
            {
                return(retval);
            }

            var trimmedName = cust.CustContactName.Trim();

            var split = trimmedName.Split(' ');

            if (split.Length > 1)
            {
                retval = split[0] + " " + split[1].Substring(0, Math.Min(1, split[1].Length));
            }
            else
            {
                retval = cust.CustContactName;
            }

            return(retval);
        }
 // Update the database
 static public void Update(ILsoCustomer item)
 {
     item.Update();
 }