我们正在使用AngularJS,C#,ASP.Net Web API和Fluent NHibernate构建Web应用程序.我们决定使用DTO将数据传输到表示层(角度视图).我对DTO的一般结构和命名有些怀疑.这是一个例子来说明我的场景.假设我有一个名为Customer的域名实体,它看起来像:
public class Customer
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual Address Address { get; set; }
public virtual ICollection<Account> Accounts { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
现在,在我的视图/表示层中,我需要检索不同类型的Customer,如:
1)Id Id和Name 2)Id,Name和Address 3)Id,Name,Address和Accounts
我创建了一组DTO来实现这个目标:
public class CustomerEntry
{
public int Id { get; set; }
public string Name { get; set; }
}
public class CustomerWithAddress : CustomerEntry
{
public AddressDetails Address { get; set; }
} …Run Code Online (Sandbox Code Playgroud) 我需要处理从服务返回的记录列表.
然而,记录的处理算法完全基于记录上的某个字段而改变.
为了实现这一点,我已经定义了一个只有一个方法的IProcessor接口:
public interface IProcessor
{
ICollection<OutputEntity> Process(ICollection<InputEntity>> entities);
}
Run Code Online (Sandbox Code Playgroud)
我有两种IProcessor不同类型处理的具体实现.
问题是我需要同时使用所有实现IProcessor..所以我如何注入IProcessor我的Engine类来驱动整个事情:
public class Engine
{
public void ProcessRecords(IService service)
{
var records = service.GetRecords();
var type1Records = records.Where(x => x.SomeField== "Type1").ToList();
var type2Records = records.Where(x => x.SomeField== "Type2").ToList();
IProcessor processor1 = new Type1Processor();
processor.Process(type1Records);
IProcessor processor2 = new Type2Processor();
processor.Process(type2Records);
}
}
Run Code Online (Sandbox Code Playgroud)
这就是我目前正在做的事情......它看起来并不好看.
关于如何改进这种设计的任何想法......或许使用IoC?
我想知道如何在课堂上有条件地隐藏数据.例如,假设我有一个名为Car的类,它有三个字段:Engine,MeterReading和Mileage.
我还有其他三个实体:司机,机械师和乘客.现在我想要的是:
驱动程序应该只能访问里程(而不是引擎和MeterReading)
机械师应该只能访问引擎和里程(而不是MeterReading)
乘客应该只能访问MeterReading(而不是引擎和里程)
有什么可能是实现这个的最好方法..(没有基于if语句的整个逻辑)?
有什么想法吗?
谢谢.
我的asp.net Web应用程序可以在没有web.config的情况下运行吗?为了论证,我们可以说我没有连接到数据库或显式读取任何配置信息.我已经尝试过了,我能够在没有web.config的VS 2008中成功运行Web应用程序.
这让我想到了如何配置身份验证和会话模式的问题?machine.config和根web.config文件(在框架文件夹中)没有显式配置任何身份验证/会话模式.
有任何想法吗 ?
谢谢.
我正在尝试为一些遗留代码创建单元测试.我必须测试的一个类叫做FileDownloader,它只有以下一个方法:
public void Transmit(string fileName, HttpResponse response, DownloadFileType fileType, byte[] content)
{
response.Clear();
response.ClearHeaders();
response.ContentType = "application/xls";
response.AddHeader("content-disposition", "attachment; filename=" + HttpContext.Current.Server.UrlEncode(fileName));
response.BinaryWrite(content);
response.End();
response.Flush();
}
Run Code Online (Sandbox Code Playgroud)
我不允许重构这个代码(这本来是理想的!).
为了测试这个,我决定根据下面的文章创建一个假的HttpContext
有了这个,我可以在测试执行期间获得假的HttpContext,但是伪造HttpResponse存在问题.
以下是我的测试结果:
[SetUp]
public void SetUp()
{
mocks = new MockRepository();
FakeHttpContext.CreateFakeHttpContext();
}
[Test]
public void ShouldTransmitHttpResponseInTheSpecifiedFormat()
{
FileDownloader downloader = new FileDownloader();
string path = "..\\..\\Fakes\\DummyDownloadReportsTemplate.xls";
byte[] bytes = ReadByteArrayFromFile(path);
downloader.Transmit("test.xls", new HttpResponse(new StringWriter()), DownloadFileType.Excel, bytes);
}
Run Code Online (Sandbox Code Playgroud)
我正在将自定义创建的HTTPResponse对象传递给该方法.当它命中"response.BinaryWrite(content)"行时抛出以下异常:
System.Web.HttpException:使用自定义TextWriter时,OutputStream不可用.
我不确定我究竟应该在这里断言...因此在测试中没有断言.这是测试这种方法的正确方法......任何想法.请指教 ?
谢谢
c# ×4
asp.net ×1
conditional ×1
dto ×1
httpresponse ×1
mocking ×1
nhibernate ×1
nunit ×1
testing ×1
web-config ×1