public IEnumerable<ModuleData> ListModules()
{
foreach (XElement m in Source.Descendants("Module"))
{
yield return new ModuleData(m.Element("ModuleID").Value);
}
}
Run Code Online (Sandbox Code Playgroud)
最初上面的代码很棒,因为如果不需要,就不需要评估整个集合.
但是,一旦枚举了所有模块,在没有更改时重复查询XDocument会变得更加昂贵.
因此,作为绩效改进:
public IEnumerable<ModuleData> ListModules()
{
if (Modules == null)
{
Modules = new List<ModuleData>();
foreach (XElement m in Source.Descendants("Module"))
{
Modules.Add(new ModuleData(m.Element("ModuleID").Value, 1, 1));
}
}
return Modules;
}
Run Code Online (Sandbox Code Playgroud)
如果我反复使用整个列表,那就太好了,但不是那么好.
是否存在中间点,我可以在整个列表被迭代之前返回,然后缓存它并将缓存提供给后续请求?
对于相当模糊的标题表示道歉,但我想要实现的目标可能在代码中有更好的说明.
我有一个WCF客户端.当我调用方法时,我想将每个调用包装在一些错误处理代码中.因此,我没有直接公开方法,而是在客户端类上创建了以下辅助函数:
public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
{
try
{
return serviceMethod(decorator);
}
[...]
}
Run Code Online (Sandbox Code Playgroud)
客户端代码使用它如下:
service.HandleServiceCall(channel => channel.Ping("Hello"));
Run Code Online (Sandbox Code Playgroud)
对Ping的调用很好地包含在一些试图处理任何错误的逻辑中.
这很好用,除了我现在需要知道在服务上实际调用哪些方法.最初,我希望只是检查Func<IApplicationService, T>使用表达式树,但没有走得太远.
最后,我决定使用Decorator模式:
public T HandleServiceCall<T>(Func<IApplicationService, T> serviceMethod)
{
var decorator = new ServiceCallDecorator(client.ServiceChannel);
try
{
return serviceMethod(decorator);
}
[...]
finally
{
if (decorator.PingWasCalled)
{
Console.Writeline("I know that Ping was called")
}
}
}
Run Code Online (Sandbox Code Playgroud)
而装饰者本身:
private class ServiceCallDecorator : IApplicationService
{
private readonly IApplicationService service;
public ServiceCallDecorator(IApplicationService service)
{
this.service = service;
this.PingWasCalled = new Nullable<bool>();
}
public …Run Code Online (Sandbox Code Playgroud) 我已经搜索了档案,但找不到适用于linq到sql的解决方案.
如何在linq中创建自定义orderby到sql,以便它生成这样的SQL代码
ORDER BY
CASE SEASON
WHEN 'WINTER' THEN 1
WHEN 'SPRING' THEN 2
WHEN 'SUMMER' THEN 3
WHEN 'AUTUMN' THEN 4
END
Run Code Online (Sandbox Code Playgroud)
请注意,自定义比较器似乎没有编译和OrderByWeight,如本教程中所示(http://www.skindog.co.uk/2009/03/18/custom-sorting-order-in-linq-order-by-weighting/)似乎不存在
注意
我希望订单发生在sql server而不是c#中,因为这会给我不同的结果,因为我正在对结果进行分区.
<ListBox.ItemTemplate>
<DataTemplate>
<Grid x:Name="grid">
<Grid.Background>
<SolidColorBrush x:Name="backgroundBrush" Color="Transparent" Opacity="0.1"/>
</Grid.Background>
</Grid>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding IsExpanded}" Value="True">
<Setter TargetName="backgroundBrush" Property="Color" Value="Green" />
</DataTrigger>
<Trigger SourceName="grid" Property="IsMouseOver" Value="True">
<Trigger.EnterActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="backgroundBrush"
Storyboard.TargetProperty="Color"
To="White" Duration="0:0:1.5"/>
</Storyboard>
</BeginStoryboard>
</Trigger.EnterActions>
<Trigger.ExitActions>
<BeginStoryboard>
<Storyboard>
<ColorAnimation Storyboard.TargetName="backgroundBrush"
Storyboard.TargetProperty="Color"
AccelerationRatio="1" Duration="0:0:1.5" />
</Storyboard>
</BeginStoryboard>
</Trigger.ExitActions>
</Trigger>
</DataTemplate.Triggers>
</DataTemplate>
</ListBox.ItemTemplate>
Run Code Online (Sandbox Code Playgroud)
不编译错误'无法找到触发器目标'backgroundBrush'.'
如果我删除DataTrigger它将编译并工作.如果我更改DataTrigger以使用TargetName="grid" Property="Background"它将编译并工作(但没有所需的不透明度).
我哪里错了?
我有一个类库,它使用在自己的目录中找到的一些xml文件.
从其他项目引用此库时,如何确保库在其自己的目录中工作?
我尝试了Assembly.GetExecutingAssembly().位置,但仍然返回启动项目的路径.
我正在使用VS2010 C#.NET 3.5和应用程序设置(Settings.settings文件).我想要做的是为我的开发和生产环境设置不同的设置,而不必使用条件语句检查调试模式来丢弃我的代码.这个问题的常见方法是什么?
我首先在应用程序中使用EntityFramework数据库.我想以某种方式通知EntityCollection我的ViewModel中的更改.它没有直接支持INotifyCollectionChanged(为什么?)而且我没有成功找到另一个解决方案.
这是我的最新尝试,由于ListChanged事件似乎没有被提升,因此无效:
public class EntityCollectionObserver<T> : ObservableCollection<T>, INotifyCollectionChanged where T : class
{
public event NotifyCollectionChangedEventHandler CollectionChanged;
public EntityCollectionObserver(EntityCollection<T> entityCollection)
: base(entityCollection)
{
IBindingList l = ((IBindingList)((IListSource)entityCollection).GetList());
l.ListChanged += new ListChangedEventHandler(OnInnerListChanged);
}
private void OnInnerListChanged(object sender, ListChangedEventArgs e)
{
if (CollectionChanged != null)
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
Run Code Online (Sandbox Code Playgroud)
有没有人有任何想法我如何观察变化EntityCollection?
担
c# entity-framework inotifycollectionchanged entitycollection
在测试我正在开发的iPhone应用程序时,我可以采取某些步骤来引发错误,例如导致应用程序在加载时崩溃或在某些其他方面持续崩溃,即使我要杀死应用程序并再次加载它.
我认为,为了重置导致崩溃的状态的内部不稳定,我可以简单地重新安装应用程序.但是,重新安装后问题仍然存在,只能通过安装更新的版本来解决.
我只能假设一些应用程序数据被保留并重新使用,即使它正在被删除并重新安装(来自IPA).
有没有办法防止这种情况发生?比如做某种"干净安装"?
编辑:如果我重新安装并重新启动手机,那么问题就解决了.这是否指向应用程序代码的任何问题?
我有一个典型的多上下文CoreData堆栈设置 - 私有队列(附加到PSC)上的主MOC,它在主队列上有一个子节点,它用作应用程序的主上下文.最后,使用后台队列(为每个操作创建的新上下文)将批量导入操作(查找或创建)传递到第三个MOC.操作完成后,保存将传播到PSC.
我一直在遇到创建重复对象的问题,可能是因为这些操作同时执行.操作1将开始.如果对象不存在,则在操作1中创建.同时,操作2同时运行.因为操作1还没有完成,操作2不可能知道新创建的对象,所以它也将继续创建一个新对象,因此重复.
为了解决这个问题,我将所有find-or-create操作汇集到一个序列中,NSOperationQueue以确保一次执行所有操作:
- (void) performBlock: (void(^)(Player *player, NSManagedObjectContext *managedObjectContext)) operation onSuccess: (void(^)()) successCallback onError:(void(^)(id error)) errorCallback
{
//Add this operation to the NSOperationQueue to ensure that
//duplicate records are not created in a multi-threaded environment
[self.operationQueue addOperationWithBlock:^{
NSManagedObjectContext *managedObjectContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
[managedObjectContext setUndoManager:nil];
[managedObjectContext setParentContext:self.mainManagedObjectContext];
[managedObjectContext performBlockAndWait:^{
//Retrive a copy of the Player object attached to the new context
id player = [managedObjectContext objectWithID:[self.player objectID]];
//Execute the block operation
operation(player, managedObjectContext);
NSError *error …Run Code Online (Sandbox Code Playgroud) 当我尝试这样做时:
[Export(typeof(IMyService))]
[PartCreationPolicy(CreationPolicy.Shared)]
private MyService Service
{
get
{
var service = new MyService();
service.Configure();
return service;
}
}
Run Code Online (Sandbox Code Playgroud)
我收到编译错误: Attribute 'PartCreationPolicy' is not valid on this declaration type. It is only valid on 'class' declarations.
这是一个错误吗?我不明白为什么MEF会允许属性导出但不允许指定部件生命周期.
使用VS2010 RC.
c# ×5
ios ×2
.net ×1
assemblies ×1
core-data ×1
datatemplate ×1
datatrigger ×1
decorator ×1
func ×1
ienumerable ×1
linq-to-sql ×1
mef ×1
return ×1
wpf ×1
yield ×1