小编Ste*_*ven的帖子

在通用存储库上使用Decorator模式实现AOP

我正在尝试使用装饰器构建一个将面向方面编程应用于我的项目的原型.我的项目的某些部分将使用通用存储库(对于简单的CRUD),但最终我还将包含命令和查询处理程序(这些将执行特定任务,如ProcessCustomerOrders等).另外,我想在这里举例说明的横切关注点是安全和日志记录.

另外,我知道我的示例代码不是使用Decorator模式,而只是我为此原型提供上下文的代码示例.

我知道还有其他方法可以实现AOP(或横切关注点),例如代理或代码编织模式,但我不熟悉这些模式,因此不知道它们之间的权衡.

我在这里使用一个控制台应用程序,只是为了展示如果我以链式方式"新建"它们将会是什么样子.

我的问题是:

(1)如何使用Simple Injector(在bootstrap类中)连接它并保持顺序相同?

(2)这是装饰器模式的正确使用(因为我没有使用基本抽象或接口类或装饰器基础)?

(3)在没有注入两个不同版本的情况下,是否有一种干净的方法可以在同一个存储库中使用ILogger服务的多个实现(例如DatabaseLogger和ConsoleLogger)?

(4)实际的日志记录是在Repository方法中实现的,并且ILogger服务被注入到Repository类中,但有没有比硬连接记录器更好的方法呢?仍然使用Generic Repositories?

(5)如果我根据我如何使用存储库在这个原型使用代理服务器或代码编织图案?

此外,欢迎对此设计的一般批评.

原型代码:

public class Program
{
    public static void Main(string[] args)
    {
        var e = new Entity
        {
            Id = 1,
            Name = "Example Entity",
            Description = "Used by Decorators",
            RowGuild = Guid.NewGuid()
        };

        Controller controller = 
            new Controller(
                new GenericRepository<Entity>(
                    new ClientManagementContext(), 
                    new ConsoleLogger()
                ), 
                new WebUser()
            );

        controller.Create(e);
    }
}

public static class RepositoryBoostrapper
{
    public static void Bootstrap(Container container)
    {
        container.RegisterOpenGeneric(typeof(IGenericRepository<>), typeof(GenericRepository<>));
    }
}

public …
Run Code Online (Sandbox Code Playgroud)

aop design-patterns decorator repository-design simple-injector

4
推荐指数
1
解决办法
1799
查看次数

使用Simple Injector进行自我绑定

我正在尝试使用Simple Injector学习依赖注入,所以我创建了一个简单的程序.我查看了大量的例子,但没有找到类似的东西.Ninject有一个类似的例子,他们使用实现自我绑定实现

Bind<Samurai>().ToSelf();
Run Code Online (Sandbox Code Playgroud)

但我没有在简单的注射器中找到任何自我约束的例子.目前该程序运行良好,我得到了我正在寻找的预期结果,但这是正确的方法吗?该程序是最后一个代码示例.

接口

public interface ICar
{
    void Move(string direction, int distance);
    void Stop();
}
Run Code Online (Sandbox Code Playgroud)

类

public class Driver
{
    private readonly ICar _car = null;

    public Driver(ICar car)
    {
        this._car = car;
    }

    public void Drive(string direction, int distance)
    {
        _car.Move(direction, distance);
    }

    public void Brake()
    {
        _car.Stop();
    }
}
Run Code Online (Sandbox Code Playgroud)

实现

public class Ferrari : ICar
{
    public void Move(string direction, int distance)
    {
        Console.WriteLine("I am driving {0} really fast for {1} miles", direction, distance);
    }
    public …
Run Code Online (Sandbox Code Playgroud)

c# console-application constructor-injection simple-injector

4
推荐指数
1
解决办法
3015
查看次数

快速将Color32 []数组复制到byte []数组

会是怎样一个快速的方法来拷贝/转换一个array的Color32[]值的byte[]缓冲? Color32是Unity 3D中包含的结构4 bytes, R, G, B and A respectively。我要完成的工作是将渲染的图像从整体通过管道发送到另一个应用程序(Windows Forms)。目前,我正在使用以下代码:

private static byte[] Color32ArrayToByteArray(Color32[] colors)
{
    int length = 4 * colors.Length;
    byte[] bytes = new byte[length];
    IntPtr ptr = Marshal.AllocHGlobal(length);
    Marshal.StructureToPtr(colors, ptr, true);
    Marshal.Copy(ptr, bytes, 0, length);
    Marshal.FreeHGlobal(ptr);
    return bytes;
}
Run Code Online (Sandbox Code Playgroud)

谢谢,抱歉,我是StackOverflow的新手。Marinescu Alexandru

c# struct copy marshalling unity-game-engine

4
推荐指数
1
解决办法
9051
查看次数

Unity.MVC4 lazy <T>在ASP.NET MVC应用程序中不起作用

我正在使用ASP.NET MVC 4应用程序.

家庭控制器的构造函数参数化为2参数(Iservice1 service1,Iservice2 service2)并非所有代码路径都使用任何Service(service1,service2),只在某些代码路径中我需要service1 instance/object或service2 instance/object.

我不想使用container.Resolve < <Lazy<IService1>>();

从这个链接(http://msdn.microsoft.com/en-us/library/dn178463(v=pandp.30).aspx)我明白unity.mvc 4使用unity 3,它具有Lazy loading支持,但是如何在ASP.NET MVC 4中执行此操作.

asp.net-mvc lazy-loading inversion-of-control unity-container deferred-loading

4
推荐指数
1
解决办法
1185
查看次数

SimpleInjector RegisterWebApiRequest vs RegisterPerWebRequest

最新版本的SimpleInjector引入了MVC和WebApi之间的区别.两个请求注册是否为同一个东西的别名?或者是否存在潜在的差异?

谢谢

dependency-injection simple-injector asp.net-web-api

4
推荐指数
1
解决办法
747
查看次数

Simple Injector - 注入容器属性

我想通过 SimpleInjector 注入 Container 属性。我没有找到 SimpleInjector 的任何功能。

然后我想将self Container注册到自己,但是Container没有接口。

我想要这个功能,因为我不通过构造函数传输 Container 对象 - 因为如果我可以使用注册对象的自动注入。

我的使用思路:

var container = new Container();
container.Options.AutowirePropertiesWithAttribute<InjectableProperty>();
container.Register<ISomething, Something>(Lifestyle.Singleton);
Run Code Online (Sandbox Code Playgroud)

ISomething:

public interface ISomething 
{
   void SomeMethod();
}
Run Code Online (Sandbox Code Playgroud)

东西类:

public class Something : ISomething 
{
    public void SomeMethod() 
    {
       var environment = _container.GetInstance<IEnvironment>();
       environment.DoSomething();
    }

    [InjectableProperty] // - maybe it is not possible (I don't know it)
    Container Container {get;set;}
}
Run Code Online (Sandbox Code Playgroud)

你有什么想法来实现这一目标吗?

非常感谢。

c# containers ioc-container simple-injector

4
推荐指数
1
解决办法
2137
查看次数

使用SimpleInjector为AccountController注册Identity Framework UserStore

我有一种情况,我想使用Identity框架中的自定义UserStore.所以我的控制器看起来像这样.

public AccountController()
: this(new UserManager<ApplicationUser>(new MyUserStore()))
{
}
public AccountController(UserManager<ApplicationUser> userManager)
{
      UserManager = userManager;
}
Run Code Online (Sandbox Code Playgroud)

现在Simpleinjector不允许多个构造函数.它可以允许用一个构造函数注册这些类.当情况与上述情况类似时,我该如何解决这个问题.使用Simpleinjector注册.

asp.net-mvc dependency-injection simple-injector asp.net-mvc-4 asp.net-identity

4
推荐指数
1
解决办法
2846
查看次数

Unity增加了导入精灵的大小

我将.png(800 x 294)尺寸37.2 KB导入统一但导入后显示它的大小为0.9MB,这比原来的大.我在导入设置中将最大尺寸更改为1024到512,将分辨率从原始尺寸降低到512 x 188,尺寸为94KB.如果没有任何图像分辨率变化,.png大小如何增加(从37.2K到0.9MB)?

sprite unity-game-engine

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

如何在autofac中混合装饰器?

我希望能够将装饰器与Autofac混合搭配.

例如,假设我有一个由Repository类实现的IRepository接口.
我可以拥有以下装饰器:RepositoryLocalCache,RepositoryDistributedCache,RepositorySecurity,RepositoryLogging ...,你得到了ideea.

基于配置设置,我想用所需的装饰器来装饰基本实现.那可以是无,一个或多个装饰器.

我熟悉注册一个装饰器的语法,或者按固定顺序注册它们的链,但我怎样才能使这个动态化呢?

.net c# dependency-injection ioc-container autofac

4
推荐指数
2
解决办法
2868
查看次数

RegisterPerWebRequest 已过时,我可以使用 Lifestyle.Scoped 吗?

我只是在检查我的理智。这是我使用 simpleinjector 3.3.2 的代码

Container.RegisterPerWebRequest<HttpContextBase>(() =>
{
  var context = HttpContext.Current;
  if (context == null && Container.IsVerifying) return new FakeHttpContext();
    return new HttpContextWrapper(context);
});


Container.Verify();
Run Code Online (Sandbox Code Playgroud)

...

    public class FakeHttpContext : HttpContextBase { }
Run Code Online (Sandbox Code Playgroud)

然而,RegisterPerWebRequest 现在被标记为已过时,我不能 100% 确定这是否是更改代码以反映新代码库的正确方法。

Container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();

//So we can inject HttpContextBase into any class
Container.Register<HttpContextBase>( () =>
{
  var context = HttpContext.Current;
  if (context == null && Container.IsVerifying)
    return new FakeHttpContext();

  return new HttpContextWrapper(context);
}, Lifestyle.Scoped);
Run Code Online (Sandbox Code Playgroud)

所以我的问题是“我应该使用Lifestyle.Scoped替换RegisterPerWebRequest吗?我仍然可以按原样使用代码吗?


从文档来看我应该做得正确

c# dependency-injection simple-injector

4
推荐指数
1
解决办法
1138
查看次数