小编Ant*_*ott的帖子

Linq2SQL"本地序列不能在LINQ to SQL中使用"错误

我有一段代码,它将内存列表与数据库中保存的一些数据相结合.这在我的单元测试中运行得很好(使用使用List的模拟Linq2SqlRepository).

    public IRepository<OrderItem> orderItems { get; set; }

    private List<OrderHeld> _releasedOrders = null;
    private List<OrderHeld> releasedOrders
    {
        get
        {
            if (_releasedOrders == null)
            {
                _releasedOrders = new List<nOrderHeld>();
            }
            return _releasedOrders;
        }
    }

    .....

    public int GetReleasedCount(OrderItem orderItem)
    {
        int? total =
            (
                from item in orderItems.All
                join releasedOrder in releasedOrders
                    on item.OrderID equals releasedOrder.OrderID
                where item.ProductID == orderItem.ProductID
                select new
                {
                    item.Quantity,
                }

            ).Sum(x => (int?)x.Quantity);

        return total.HasValue ? total.Value : 0;
    }
Run Code Online (Sandbox Code Playgroud)

当我针对数据库运行它时,我收到的错误我真的不明白.

异常信息:
    异常类型:System.NotSupportedException
    异常消息:除Contains()运算符外,本地序列不能用于查询运算符的LINQ to SQL实现. …

linq linq-to-sql

8
推荐指数
1
解决办法
8021
查看次数

如何实现通用的RepositoryFactory?

我正在尝试实现Generic Repository.这是我到目前为止所得到的......

public interface IRepositoryFactory
{
    IRepository<T> RepositoryOf<T>() where T : class;
}

public class EntityFrameworkRepositoryFactory : IRepositoryFactory
{
    private readonly IWindsorContainer _container;

    public EntityFrameworkRepositoryFactory(IWindsorContainer container)
    {
        _container = container;
    }

    public IRepository<T> RepositoryOf<T>() where T : class
    {
        var repository = _container.Resolve<IRepository<T>>();
        return repository;
    }
}
Run Code Online (Sandbox Code Playgroud)

RepositoryFactory由我的工作单元实现使用

public interface IUnitOfWork : IDisposable
{
    IRepository<T> RepositoryOf<T>() where T : class;
    void Commit();
}
Run Code Online (Sandbox Code Playgroud)

无论如何,我想问的问题是,RepositoryFactory实现依赖于IWindsorContainer是否正确?

我需要一种方法来询问任何类型的IRepository,所以我的安装程序代码执行此操作...

// Windsor Container
container.Register(
    Component.For<IWindsorContainer>()
        .Named("Container")
        .Instance(container)
    );
Run Code Online (Sandbox Code Playgroud)

这似乎与IoC的整个概念背道而驰,但随后可能要求存储库的整个想法无论如何都会这样做.

编辑(回复miensol的回答)

我已经使用Windsor在我的安装程序中使用以下代码为我创建存储库...

// Generic …
Run Code Online (Sandbox Code Playgroud)

c# design-patterns repository-pattern factory-pattern entity-framework-4

8
推荐指数
1
解决办法
2306
查看次数

什么是控制反转

可能重复:
什么是控制反转?

我理解依赖注入(DI)是什么(我认为!).它基本上是对象可能具有的依赖性的满足.我尝试考虑使用DI作为面向服务时编写的代码,并将我的代码定义为使用其他服务.

但是,我现在想知道在使用IoC时我们正在反转控制权.这是一个相当含糊的术语,它可能意味着一些事情.

但是,我认为由IoC框架处理的是创建对象(因此使用DI来满足依赖性)的责任.

应用程序仍然有责任询问它使用的对象(即 - 服务),区别在于它不知道(或关心)如何创建它.那么,为什么服务定位器被认为是反模式,如果它所做的就是要求服务呢?

我说得对吗?或者它是否意味着别的东西.另外,我是否正确分离了DI和IoC的职责?如果我有一个IoC框架,没有DI框架就无法运行.或者DI只是IoC框架的一个特性?

design-patterns dependency-injection inversion-of-control

8
推荐指数
1
解决办法
2499
查看次数

使用nuget包添加自定义构建步骤

我正在开发一个nuget包,它将设置当前项目以使用我公司的程序集版本标准.除了添加自定义构建步骤之外,我已经做到了我想做的所有事情(到目前为止).

从历史上看,这是通过直接编辑.csproj文件并在xml中添加几个新标记来手动完成的.这些是 ...

  • 物业集团
  • 目标

它实际上成功添加了它们,但我通过编辑xml而不是通过install.ps1中的$ project参数中的EnvDTE对象来完成它.但是我收到一条消息,询问我是否要丢弃我的更改.

$project.Save()在我进行更改之前,我已经添加到我的脚本中,并且摆脱了弹出窗口,我只是告诉我项目已经更改并询问我是否要重新加载它.哪个更好,但仍然不太完美.

有一个更好的方法吗?

envdte nuget

7
推荐指数
1
解决办法
2556
查看次数

如何以编程方式设置范围验证器的最大值?

我想在文本框上设置一个范围验证器,以防止有人订购的产品多于可用产品.我有可用的数量存储在数据库中,我将Ranged Validator的最大值属性数据绑定到数据库中的字段.

<asp:RangeValidator ID="RangeValidator1" runat="server" 
                    ControlToValidate="tbQuantity" Display="Dynamic" ErrorMessage = "Can't Order More Than Quantity." 
                    ForeColor="Red" MaximumValue='<%# Eval("Quantity") %>' MinimumValue="0"></asp:RangeValidator>
Run Code Online (Sandbox Code Playgroud)

但是,当我调试程序时,我得到一些意想不到的结果.

数量为17.并且1不会触发错误消息,但2-9会触发,而10-17不会触发它,但是18和更高触发它.我猜这与它正在比较字符串的事实有关,但我不确定如何将它改为比较数字.

c# validation

6
推荐指数
1
解决办法
7458
查看次数

C#泛型约束无法按预期工作

我有一种情况,我需要动态建立一个过滤器列表,以应用于对象列表.这些对象可以是实现包含我需要过滤的所有属性的接口的任何对象.

public interface IStuff
{
    bool SuitableForSomething { get; set; }
    bool SuitableForSomethingElse { get; set; }
}

public class SomeStuff : IStuff
{
    ...
}

public class SomeOtherStuff : IStuff
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

我有一个如此定义的标准列表......

public List<Expression<Func<IStuff, bool>>> Criteria { get; private set; } 
Run Code Online (Sandbox Code Playgroud)

并添加这样的标准......

Criteria.Add(x => x.SuitableForSomething);
Criteria.Add(x => x.SuitableForSomethingElse);
Run Code Online (Sandbox Code Playgroud)

然后我将标准应用于我的查询,就像这样......

var stuff= _stuffCache
    .GetAll()
    .AsQueryable()
    .ApplyCriteria(Criteria);
Run Code Online (Sandbox Code Playgroud)

它使用以下扩展方法......

public static IQueryable<T> ApplyCriteria<T>(this IQueryable<T> stuff, List<Expression<Func<IStuff, bool>>> criteria) 
    where T : IStuff
{
    foreach (var expression in criteria)
    {
        stuff …
Run Code Online (Sandbox Code Playgroud)

c# generics

6
推荐指数
1
解决办法
299
查看次数

在 web.config 中使用 web.config 变量

我想在 web.config 中定义一个变量,我可以在 web.config 文件(和其他配置文件)中的多个位置使用该变量。通过例子来解释可能更容易......

网络配置

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="AuthServiceEndPoint" value="any_old_name_i_like"/>
    </appSettings>
    <system.web>

    ...

    <system.serviceModel>
        <client>
            <endpoint
                address="net.tcp://localhost/AuthService"
                binding="netTcpBinding"
                contract="MyServices.Contracts.IAuthService"
                name="#{AppSettings.AuthServiceEndPoint}"
                bindingConfiguration="netTcpBindingConfig"
            />

        </client>
    </system.serviceModel>
</configuration>
Run Code Online (Sandbox Code Playgroud)

温莎配置文件

<?xml version="1.0" encoding="utf-8" ?>
<castle>
    <components>

        ...

        <component
            id="AuthProvider"
            service="MyServices.Client.IAuthProvider, MyServices.Client"
            type="MyServices.Client.AuthProvider, MyServices.Client"
            lifestyle="transient">
            <parameters>
                <endpoint>#{AppSettings.AuthServiceEndPoint}</endpoint>
            </parameters>
        </component>

    </components>
</castle>
Run Code Online (Sandbox Code Playgroud)

这可能吗?


编辑(更多信息)

我已经能够从 Windsor.config 文件访问 AppSettings(该文件实际上由温莎城堡和自定义 XmlInterpreter 处理)。

真正的问题是我可以在 web.config 中执行此操作吗?

<?xml version="1.0"?>
<configuration>
    <appSettings>
        <add key="AuthServiceEndPoint" value="any_old_name_i_like"/>
    </appSettings>
    <system.web>

    ...

    <system.serviceModel>
        <client>
            <endpoint
                address="net.tcp://localhost/AuthService"
                binding="netTcpBinding"
                contract="MyServices.Contracts.IAuthService"
                name="#{AppSettings.AuthServiceEndPoint}"
                bindingConfiguration="netTcpBindingConfig"
            /> …
Run Code Online (Sandbox Code Playgroud)

asp.net web-config

5
推荐指数
1
解决办法
6812
查看次数

Wix 仅在卸载时自定义操作

我在让自定义操作仅在使用 Wix 卸载服务时运行时遇到问题。

<CustomAction Id='InstallService'
              FileKey='Service.exe'
              ExeCommand='install'
              Execute='immediate'
              Return='check'
              Impersonate='yes'>NOT Installed</CustomAction>

<CustomAction Id='UninstallService'
              FileKey='Service.exe'
              ExeCommand='uninstall'
              Execute='immediate'
              Return='check'
              Impersonate='yes'>Installed AND NOT REINSTALL</CustomAction>

<InstallExecuteSequence>
  <Custom Action='UninstallService' After='StopServices'/>
  <Custom Action='InstallService' Before='StartServices'/>
</InstallExecuteSequence>
Run Code Online (Sandbox Code Playgroud)

这是组件...

  <Component Id="ProductComponent">
    <File Id="MyService.exe"
          Name="MyService.exe"
          Source="..\MyService\bin\Release\MyService.exe"
          Vital="yes"
          KeyPath="yes"
          DiskId="1"/>

    ...

    <ServiceControl Id='ServiceControl'
                    Name='MyService'
                    Start='install'
                    Stop='both'/>
  </Component>
Run Code Online (Sandbox Code Playgroud)

当我运行安装程序时,出现错误。查看事件日志我发现这个......

产品:MyService - 错误 1721。此 Windows Installer 程序包有问题。无法运行完成此安装所需的程序。请联系您的支持人员或软件包供应商。操作:UninstallService,位置:C:\Program Files (x86)\MyService\MyService.exe,命令:uninstall

我也尝试过这个...

<CustomAction Id='UninstallService'
              FileKey='Service.exe'
              ExeCommand='uninstall'
              Execute='immediate'
              Return='check'
              Impersonate='yes'>Installed AND NOT UPGRADINGPRODUCTCODE</CustomAction>
Run Code Online (Sandbox Code Playgroud)

注意:我使用自定义操作来安装/卸载服务,因为我使用了TopShelf.NET

wix

5
推荐指数
1
解决办法
1万
查看次数

MVC2项目中的resx文件的"运行自定义工具"(来自外部应用程序/脚本)

我试图让我的MVC项目的本地化与我们现有的基础设施一起使用来编辑字符串资源.我们将所有资源字符串存储在数据库表中,并具有用于编辑它们的前端Web UI,以及生成.resx文件的导出应用程序.这一切都很好,但是我对使用MVC2和VS2010的新项目有点困难.

我已经问了另一个问题,答案几乎让我在那里,但并不完全.

我现在已根据许多人的建议将资源更改为Resources文件夹(而不是App_GlobalResources).并针对我的.resx文件进行以下设置...

Build Action             = Embedded Resource
Copy to Output Directory = Do not copy
Custom Tool              = PublicResXFileCodeGenerator
Custom Tool Namespace    = Resources
File Name                = MyApp.resx

我已经更改了我的导出应用程序以使用以下参数运行resgen.exe工具...

string args = string.Format("/publicClass \"{0}\" /str:cs,Resources,{1},\"{2}\"", resourceFile, Path.GetFileNameWithoutExtension(resourceFile), csFilename);
Run Code Online (Sandbox Code Playgroud)

...当我最初将.resx文件添加到我的项目时,生成一个几乎相同的.designer.cs文件.唯一的区别是

生成的.designer.cs文件与我从导出应用程序中运行resgen.exe工具时获得的文件略有不同.

当我第一次将.resx文件添加到我的Resources文件夹时,这是VS2010生成的代码...

public static global::System.Resources.ResourceManager ResourceManager {
    get {
        if (object.ReferenceEquals(resourceMan, null)) {
            global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Resources.MyApp", typeof(MyApp).Assembly);
            resourceMan = temp;
        }
        return resourceMan;
    }
}
Run Code Online (Sandbox Code Playgroud)

...运行resgen.exe工具的不同之处在于,它将MyCompany.MyApp的前缀添加到ResourceManager的构造函数中的命名空间

new global::System.Resources.ResourceManager("MyCompany.MyApp.Resources.MyApp", typeof(MyApp).Assembly);
Run Code Online (Sandbox Code Playgroud)

现在,这对我来说似乎是resgen.exe工具中的一个错误,因为我已经告诉它我的资源的命名空间是Resources …

resx visual-studio-2010 asp.net-mvc-2

3
推荐指数
1
解决办法
7901
查看次数

私有范围的变量仅用于属性内

是否有可能有一个变量,它是一个类中的实例变量,但只能由特定的属性访问?

我经常创造像这样的"自我创造"属性......

private IWGSLocation _location;
public IWGSLocation Location
{
    get
    {
        _location = _location ?? new WGSLocation();
        _location.Latitude = Latitude.GetValueOrDefault(0);
        _location.Longitude = Longitude.GetValueOrDefault(0);
        return _location;
    }
}
Run Code Online (Sandbox Code Playgroud)

这意味着WGSLocation每次访问该属性时,我都不会重新创建一个新的(或者我需要的任何其他类型的对象,创建起来可能很昂贵,或者可能只需要创建一次).缺点是我的类可以访问_location变量.但我真的不想要它,所以如果有任何方法有一个只能在属性本身内使用的实例变量?

我正在思考这些问题......

public IWGSLocation Location
{
    get
    {
        WGSLocation _location = _location ?? new WGSLocation();
        _location.Latitude = Latitude.GetValueOrDefault(0);
        _location.Longitude = Longitude.GetValueOrDefault(0);
        return _location;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# properties

3
推荐指数
1
解决办法
189
查看次数