本文整理汇总了C#中Container.Register方法的典型用法代码示例。如果您正苦于以下问题:C# Container.Register方法的具体用法?C# Container.Register怎么用?C# Container.Register使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Container
的用法示例。
在下文中一共展示了Container.Register方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Configure
/// <summary>
/// Application specific configuration
/// This method should initialize any IoC resources utilized by your web service classes.
/// </summary>
/// <param name="container"></param>
public override void Configure(Container container)
{
JsConfig.EmitCamelCaseNames = true;
SetConfig(new HostConfig {
DebugMode = true,
Return204NoContentForEmptyResponse = true,
});
container.Register<IRedisClientsManager>(c =>
new RedisManagerPool("localhost:6379"));
container.Register(c => c.Resolve<IRedisClientsManager>().GetCacheClient());
container.Register<IDbConnectionFactory>(c => new OrmLiteConnectionFactory(
AppSettings.GetString("AppDb"), PostgreSqlDialect.Provider));
container.Register<IAuthRepository>(c =>
new OrmLiteAuthRepository(c.Resolve<IDbConnectionFactory>())
{
UseDistinctRoleTables = AppSettings.Get("UseDistinctRoleTables", true),
});
var authRepo = (OrmLiteAuthRepository)container.Resolve<IAuthRepository>();
authRepo.DropAndReCreateTables();
CreateUser(authRepo, 1, "test", "test", new List<string> { "TheRole" }, new List<string> { "ThePermission" });
CreateUser(authRepo, 2, "test2", "test2");
Plugins.Add(new PostmanFeature());
Plugins.Add(new CorsFeature(
allowOriginWhitelist: new[] { "http://localhost", "http://localhost:56500", "http://test.servicestack.net", "http://null.jsbin.com" },
allowCredentials: true,
allowedHeaders: "Content-Type, Allow, Authorization"));
Plugins.Add(new RequestLogsFeature {
RequestLogger = new RedisRequestLogger(container.Resolve<IRedisClientsManager>()),
});
Plugins.Add(new RazorFormat());
Plugins.Add(new AuthFeature(() => new CustomUserSession(),
new IAuthProvider[]
{
new BasicAuthProvider(AppSettings),
new CredentialsAuthProvider(AppSettings),
}));
Plugins.Add(new SwaggerFeature());
Plugins.Add(new ValidationFeature());
Plugins.Add(new AutoQueryFeature
{
MaxLimit = 100,
});
container.RegisterValidators(typeof(ThrowValidationValidator).Assembly);
JavaGenerator.AddGsonImport = true;
var nativeTypes = this.GetPlugin<NativeTypesFeature>();
nativeTypes.MetadataTypesConfig.ExportTypes.Add(typeof(DayOfWeek));
}
示例2: MultipleParameterGeneratesCode
public void MultipleParameterGeneratesCode()
{
// Arrange
IContainer container = new Container ();
container.Register<ISimpleObject, SimpleObject> ();
container.Register<IShallowDependent, ShallowDependent> ();
container.Register<IDeepDependent, DeepDependent> ();
container.Register<IMultipleParameterObject, MultipleParameterObject> ();
CodeGenerator codeGenerator = new CodeGenerator ();
MemoryStream memoryStream = new MemoryStream ();
// Act
codeGenerator.WriteToStream(LanguageEnum.Csharp, container, memoryStream, "SomeNamespace", "TestContainer");
memoryStream.Position = 0;
string charpString;
using (StreamReader reader = new StreamReader(memoryStream)) {
charpString = reader.ReadToEnd ();
}
TestContainer testContainer = new TestContainer ();
IMultipleParameterObject result = testContainer.Resolve<IMultipleParameterObject> ();
// Assert
Assert.IsNotNull (result);
Assert.IsNotNull (result.ShallowDependent);
Assert.IsNotNull (result.SimpleObject);
}
开发者ID:JamesRandall,项目名称:AccidentalFish.Xamarin.DependencyInjection,代码行数:26,代码来源:CsharpGeneratorTests.cs
示例3: Main
private static void Main(string[] args)
{
var container = new Container();
container.Register<ILogger, ConsoleLogger>();
container.Register<Mediator, Mediator>(true);
container.Register<SFXUtility, SFXUtility>(true, true);
var bType = typeof (Base);
foreach (
var type in
Assembly.GetAssembly(bType)
.GetTypes()
.OrderBy(type => type.Name)
.Where(type => type.IsClass && !type.IsAbstract && type.IsSubclassOf(bType)))
{
try
{
var tmpType = type;
container.Register(type, () => Activator.CreateInstance(tmpType, new object[] {container}), true,
true);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
//container.Register(typeof (Analytics),
// () => Activator.CreateInstance(typeof (Analytics), new object[] {container}), true, true);
}
示例4: InitializeIOC
private void InitializeIOC()
{
// Create the IOC container
IOC = new Container();
// Create the Default Factory
var controllerFactory = new MunqControllerFactory(IOC);
// set the controller factory
ControllerBuilder.Current.SetControllerFactory(controllerFactory);
// Register the dependencies
// Article3
// The dependencies to the concrete implementation should be externalized
//new AccountMembershipRegistrar().Register(IOC);
//new AuthenticationRegistrar().Register(IOC);
ConfigurationLoader.FindAndRegisterDependencies(IOC);
// Register the Controllers
IOC.Register<IController>("Home", ioc => new HomeController());
IOC.Register<IController>("Account",
ioc => new AccountController(ioc.Resolve<IFormsAuthentication>(),
ioc.Resolve<IMembershipService>())
);
}
示例5: Analyze_TwoSingletonRegistrationsForTheSameImplementation_ReturnsExpectedWarning
public void Analyze_TwoSingletonRegistrationsForTheSameImplementation_ReturnsExpectedWarning()
{
// Arrange
string expectedMessage1 =
"The registration for IFoo maps to the same implementation and lifestyle as the " +
"registration for IBar does. They both map to FooBar (Singleton).";
string expectedMessage2 =
"The registration for IBar maps to the same implementation and lifestyle as the " +
"registration for IFoo does. They both map to FooBar (Singleton).";
var container = new Container();
container.Register<IFoo, FooBar>(Lifestyle.Singleton);
container.Register<IBar, FooBar>(Lifestyle.Singleton);
container.Verify(VerificationOption.VerifyOnly);
// Act
var results = Analyzer.Analyze(container).OfType<TornLifestyleDiagnosticResult>().ToArray();
// Assert
Assert.AreEqual(2, results.Length, Actual(results));
Assert_ContainsDescription(results, expectedMessage1);
Assert_ContainsDescription(results, expectedMessage2);
}
示例6: Main
static void Main(string[] args)
{
Container container = new Container();
container.Register<IBillingProcessor, BillingProcessor>();
container.Register<ICustomer, Customer>();
container.Register<INotifier, Notifier>();
container.Register<ILogger, Logger>();
Console.WriteLine("Poor-Man's DI Container Example");
Console.WriteLine();
OrderInfo orderInfo = new OrderInfo()
{
CustomerName = "Miguel Castro",
Email = "[email protected]",
Product = "Laptop",
Price = 1200,
CreditCard = "1234567890"
};
Console.WriteLine("Production:");
Console.WriteLine();
Commerce commerce = container.CreateType<Commerce>();
commerce.ProcessOrder(orderInfo);
Console.WriteLine();
Console.WriteLine("Press [Enter] to exit...");
Console.ReadLine();
}
示例7: RegisterResolveArguments
public void RegisterResolveArguments()
{
var c = new Container();
c.Register<ITest1>(() => new Test1());
var t1 = c.Resolve<ITest1>();
Assert.IsNotNull(t1);
Assert.IsInstanceOfType(typeof(Test1), t1);
c.Register<ITest2>(() => new Test2());
var t2 = c.Resolve<ITest2>();
Assert.IsNotNull(t2);
Assert.IsInstanceOfType(typeof(Test2), t2);
c.Register<ITest3, ITest1, ITest2>((a1, a2) => new Test3(a1, a2));
var t3 = c.Resolve<ITest3>();
Assert.IsNotNull(t3);
Assert.IsInstanceOfType(typeof(Test3), t3);
Assert.IsNotNull(t3.Test1);
Assert.IsInstanceOfType(typeof(Test1), t3.Test1);
Assert.IsNotNull(t3.Test2);
Assert.IsInstanceOfType(typeof(Test2), t3.Test2);
}
示例8: Depot
static Depot()
{
Container = new Container();
Container.Register<ICache>(ObjectCacheKey, r => new DefaultCache()).Permanent();
Container.Register<IStringCache>(StringCacheKey, r => new StringCache()).Permanent();
Container.Register<IContentCache>(ContentCacheKey, r => new ContentCache()).Permanent();
ContentionTimeout = TimeSpan.FromSeconds(10);
}
示例9: TestCreatWithConstructorInjectionNotAllRegistered
public void TestCreatWithConstructorInjectionNotAllRegistered()
{
Container container = new Container();
container.Register<IRepository, RepositoryA>();
container.Register<ICustomController, CustomController>();
ICustomController controller = container.Resolve<ICustomController>();
Assert.IsNotNull(controller);
}
示例10: CanRebindLabmba
public void CanRebindLabmba()
{
var container = new Container();
container.Register<ITest>(()=> new TestImp());
container.Register<ITest>(()=> new TestDependent(null));
var impl = container.Resolve<ITest>();
Assert.NotNull(impl);
Assert.AreEqual(typeof(TestDependent), impl.GetType());
}
示例11: CreateKernel
private static IContainer CreateKernel()
{
var kernel = new Container();
kernel.Register<IStoryReader, ZipStoryReader>();
kernel.Register<IStoryTellerFactory, StoryTellerFactory>();
kernel.Register<EntryPointForm, EntryPointForm>();
return kernel;
}
示例12: TestAlreadyRegisteredTransient
public void TestAlreadyRegisteredTransient()
{
Container container = new Container();
container.Register<IComparable, DummyComparable>();
Assert.Throws(
typeof(TypeAlreadyRegisteredException),
() => container.Register<IComparable, DummyComparable>()
);
}
示例13: InitializeContainer
private static void InitializeContainer(Container container)
{
container.Register<IUserStore<ApplicationUser>,sdfasfasdfdf>();
container.Register<IUserRepository, UserToMemory>();
//#error Register your services here (remove this line).
// For instance:
// container.Register<IUserRepository, SqlUserRepository>(Lifestyle.Scoped);
}
示例14: Main
static void Main(string[] args)
{
var iocContainer = new Container();
iocContainer.Register<IPersonFactory, PersonFactory>();
iocContainer.Register<PersonService, PersonService>();
var personService = iocContainer.Resolve<PersonService>();
var person = personService.GetPerson();
Console.WriteLine("The persons name is {0} {1}", person.FirstName, person.LastName);
Console.ReadLine();
}
示例15: ResolveNamedInstanceTest
public void ResolveNamedInstanceTest()
{
var container = new Container();
var obj = new MockSimpleObject();
container.Register<MockSimpleObject>().To<MockSimpleObject>();
container.Register<MockSimpleObject>().Named("test").To<MockSimpleObject>().ConstructAs(j => obj);
var res = container.Resolve<MockSimpleObject>("test");
Assert.That(res, Is.SameAs(obj));
}