简体   繁体   English

在foreach循环中继续执行后,C#处理对象的实例

[英]C# handle instance of an object after continue in foreach loop

Let's assume I have an instance of this class. 假设我有一个此类的实例。

public class MyClass
{
    public string LocationCode;
    public string PickUpCode
}

There is another class that takes a List<MyClass> as input and saves to the DB. 还有另一个将List<MyClass>作为输入并保存到DB的类。

Now I need to apply some business rules: 现在,我需要应用一些业务规则:

For example if LocationCode is null this item in the List<MyClass> must be skipped and the foreach loop must continue to the next item in the list. 例如,如果LocationCodenull ,则必须跳过List<MyClass>中的此项,并且foreach循环必须continue到列表中的下一项。

I've written the following code and the items with null LocationCode are indeed skipped but the var instance = new SomeClass(); 我已经编写了以下代码,并且确实跳过了具有null LocationCode的项,但是var instance = new SomeClass(); somehow remains in memory so when the loop reaches an valid item and proceeds to save it in the DB, it also saves all the previously skipped instances of var instance = new SomeClass(); 以某种方式保留在内存中,因此当循环到达有效项并继续将其保存在数据库中时,它还将保存所有先前跳过的var instance = new SomeClass(); . Which means I have null entries in the DB. 这意味着我在数据库中有空条目。

I'm using NHibernate and Evict doesn't seam to be doing the trick. 我正在使用NHibernate,Evict并没有接缝。 Any suggestions? 有什么建议么?

public void Save(List<MyClass> listOfItems)
{
    using (UnitOfWork.Start())
    {
        var repository = new Repository();

        try
        {

            foreach (var item in listOfItems.Select(i => i.Item).Where(item => item != null))
            {
                var instance = new SomeClass();         

                if (pickUpCode != null)
                {
                    instance.PickUpCode = pickUpCode;
                }
                else
                {               
                    instance.PickUpCode = null;
                }

                if (locationCode != null)
                {
                    instance.StartLocation = locationCode
                }
                else
                {
                    UnitOfWork.CurrentSession.Evict(instance);
                    continue;
                }

                repository.SaveSomeClass(instance);
            }
        }
        catch (Exception ex)
        {
            _log.Error(" Unhandled error", ex);
        }
    }
}

** Because someone asked, here's some code on UnitOfWork.Start() **因为有人问,这是一些关于UnitOfWork.Start()代码

public static class UnitOfWork
    {        
        public static IUnitOfWork Start();
    }

public interface IUnitOfWork : IDisposable
    {
        bool IsInActiveTransaction { get; }
        IUnitOfWorkFactory SessionFactory { get; }

        IGenericTransaction BeginTransaction();
        IGenericTransaction BeginTransaction(IsolationLevel isolationLevel);
        void Flush();
        void TransactionalFlush();
        void TransactionalFlush(IsolationLevel isolationLevel);
    }

Why don't you fail first and avoid all of this? 您为什么不首先失败并避免所有这些?

Example being: 示例为:

foreach (var item in listOfItems.Select(i => i.Item).Where(item => item != null))
        {

            if (item.LocationCode == null){
              continue;
            }

            var instance = new SomeClass();         

            if (pickUpCode != null)
            {
                instance.PickUpCode = pickUpCode;
            }
            else
            {               
                instance.PickUpCode = null;
            }

            // if we reach here, location code is definitley not null, no need for the check            
            instance.StartLocation = locationCode



            repository.SaveSomeClass(instance);
        }

Alternatively, you could add the check to you LINQ where clause 或者,您可以将支票添加到LINQ where子句中

foreach (var item in listOfItems.where(item=> item != null && item.LocationCode != null)

Without more code on how UnitofWork.Start works its hard to suggest. 没有关于UnitofWork.Start如何工作的更多代码,很难提出建议。 But, It's worth trying by implementing IDisposable on SomeClass. 但是,值得在SomeClass上实现IDisposable。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM